From 6f75038bf58462324b54fbe93c3242180864ba45 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 4 Oct 2024 19:54:04 -0400 Subject: [PATCH] Updated Passport objects --- .../Objects/Passport/EncryptedCredentials.php | 34 ++--- .../Passport/EncryptedPassportElement.php | 131 ++++-------------- .../Objects/Passport/PassportData.php | 42 ++---- .../Objects/Passport/PassportFile.php | 40 ++---- 4 files changed, 65 insertions(+), 182 deletions(-) diff --git a/src/TgBotLib/Objects/Passport/EncryptedCredentials.php b/src/TgBotLib/Objects/Passport/EncryptedCredentials.php index f2a54fe..d45fb60 100644 --- a/src/TgBotLib/Objects/Passport/EncryptedCredentials.php +++ b/src/TgBotLib/Objects/Passport/EncryptedCredentials.php @@ -7,20 +7,9 @@ class EncryptedCredentials implements ObjectTypeInterface { - /** - * @var string - */ - private $data; - - /** - * @var string - */ - private $hash; - - /** - * @var string - */ - private $secret; + private string $data; + private string $hash; + private string $secret; /** * Base64-encoded encrypted JSON-serialized data with unique user's payload, data hashes and secrets required @@ -55,9 +44,7 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -69,15 +56,16 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return EncryptedCredentials + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?EncryptedCredentials { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->data = $data['data'] ?? null; $object->hash = $data['hash'] ?? null; $object->secret = $data['secret'] ?? null; diff --git a/src/TgBotLib/Objects/Passport/EncryptedPassportElement.php b/src/TgBotLib/Objects/Passport/EncryptedPassportElement.php index 3c32e3f..d27d76e 100644 --- a/src/TgBotLib/Objects/Passport/EncryptedPassportElement.php +++ b/src/TgBotLib/Objects/Passport/EncryptedPassportElement.php @@ -7,55 +7,31 @@ class EncryptedPassportElement implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string|null - */ - private $data; - - /** - * @var string|null - */ - private $phone_number; - - /** - * @var string|null - */ - private $email; - + private string $type; + private ?string $data; + private ?string $phone_number; + private ?string $email; /** * @var PassportFile[]|null */ - private $files; - + private ?array $files; /** * @var PassportFile[]|null */ - private $front_side; - + private ?array $front_side; /** * @var PassportFile[]|null */ - private $reverse_side; - + private ?array $reverse_side; /** * @var PassportFile[]|null */ - private $selfie; - + private ?array $selfie; /** * @var PassportFile[]|null */ - private $translation; - - /** - * @var string - */ - private $hash; + private ?array $translation; + private string $hash; /** * Element type. One of “personal_details”, “passport”, “driver_license”, “identity_card”, “internal_passport”, @@ -180,9 +156,7 @@ } /** - * Returns an array representation of the object. - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -191,84 +165,35 @@ 'data' => $this->data, 'phone_number' => $this->phone_number, 'email' => $this->email, - 'files' => is_array($this->files) ? array_map(function ($file) - { - if($file instanceof PassportFile) - { - return $file->toArray(); - } - return $file; - }, $this->files) : null, - 'front_side' => is_array($this->front_side) ? array_map(function ($file) - { - if($file instanceof PassportFile) - { - return $file->toArray(); - } - return $file; - }, $this->front_side) : null, - 'reverse_side' => is_array($this->reverse_side) ? array_map(function ($file) - { - if($file instanceof PassportFile) - { - return $file->toArray(); - } - return $file; - }, $this->reverse_side) : null, - 'selfie' => is_array($this->selfie) ? array_map(function ($file) - { - if($file instanceof PassportFile) - { - return $file->toArray(); - } - return $file; - }, $this->selfie) : null, - 'translation' => is_array($this->translation) ? array_map(function ($file) - { - if($file instanceof PassportFile) - { - return $file->toArray(); - } - return $file; - }, $this->translation) : null, + 'files' => is_null($this->files) ? null : array_map(fn(PassportFile $item) => $item->toArray(), $this->files), + 'front_side' => is_null($this->front_side) ? null : array_map(fn(PassportFile $item) => $item->toArray(), $this->front_side), + 'reverse_side' => is_null($this->reverse_side) ? null : array_map(fn(PassportFile $item) => $item->toArray(), $this->reverse_side), + 'selfie' => is_null($this->selfie) ? null : array_map(fn(PassportFile $item) => $item->toArray(), $this->selfie), + 'translation' => is_null($this->translation) ? null : array_map(fn(PassportFile $item) => $item->toArray(), $this->translation), 'hash' => $this->hash ]; } /** - * Constructs object from an array representation. - * - * @param array $data - * @return EncryptedPassportElement + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?EncryptedPassportElement { - $object = new static(); + if($data === null) + { + return null; + } + $object = new static(); $object->type = $data['type']; $object->data = $data['data'] ?? null; $object->phone_number = $data['phone_number'] ?? null; $object->email = $data['email'] ?? null; - $object->files = isset($data['files']) ? array_map(function (array $file) - { - return PassportFile::fromArray($file); - }, $data['files'] ?? []) : null; - $object->front_side = isset($data['front_side']) ? array_map(function (array $file) - { - return PassportFile::fromArray($file); - }, $data['front_side'] ?? []) : null; - $object->reverse_side = isset($data['reverse_side']) ? array_map(function (array $file) - { - return PassportFile::fromArray($file); - }, $data['reverse_side'] ?? []) : null; - $object->selfie = isset($data['selfie']) ? array_map(function (array $file) - { - return PassportFile::fromArray($file); - }, $data['selfie'] ?? []) : null; - $object->translation = isset($data['translation']) ? array_map(function (array $file) - { - return PassportFile::fromArray($file); - }, $data['translation'] ?? []) : null; + $object->files = isset($data['files']) ? array_map(fn(array $items) => PassportFile::fromArray($items), $data['files'] ?? []) : null; + $object->front_side = isset($data['front_side']) ? array_map(fn(array $items) => PassportFile::fromArray($items), $data['front_side'] ?? []) : null; + $object->reverse_side = isset($data['reverse_side']) ? array_map(fn(array $items) => PassportFile::fromArray($items), $data['reverse_side'] ?? []) : null; + $object->selfie = isset($data['selfie']) ? array_map(fn(array $items) => PassportFile::fromArray($items), $data['selfie'] ?? []) : null; + $object->translation = isset($data['translation']) ? array_map(fn(array $items) => PassportFile::fromArray($items), $data['translation'] ?? []) : null; $object->hash = $data['hash']; return $object; diff --git a/src/TgBotLib/Objects/Passport/PassportData.php b/src/TgBotLib/Objects/Passport/PassportData.php index a583ebc..539a401 100644 --- a/src/TgBotLib/Objects/Passport/PassportData.php +++ b/src/TgBotLib/Objects/Passport/PassportData.php @@ -10,12 +10,8 @@ /** * @var EncryptedPassportElement[] */ - private $data; - - /** - * @var EncryptedCredentials - */ - private $credentials; + private array $data; + private EncryptedCredentials $credentials; /** * Array with information about documents and other Telegram Passport elements that was shared with the bot @@ -38,39 +34,29 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'data' => array_map(function ($element) - { - if($element instanceof ObjectTypeInterface) - { - return $element->toArray(); - } - return $element; - }, $this->data), - 'credentials' => $this->credentials->toArray(), + 'data' => is_null($this->data) ? null : array_map(fn(EncryptedPassportElement $item) => $item->toArray(), $this->data), + 'credentials' => $this->credentials?->toArray(), ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return PassportData + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?PassportData { - $object = new self(); - $object->data = array_map(function (array $element) + if($data === null) { - return EncryptedPassportElement::fromArray($element); - }, $data['data']); - $object->credentials = EncryptedCredentials::fromArray($data['credentials']); + return null; + } + + $object = new self(); + $object->data = isset($data['data']) ? array_map(fn(array $items) => EncryptedPassportElement::fromArray($items), $data['data'] ?? []) : null; + $object->credentials = isset($data['credentials']) ? EncryptedCredentials::fromArray($data['credentials']) : null; return $object; } diff --git a/src/TgBotLib/Objects/Passport/PassportFile.php b/src/TgBotLib/Objects/Passport/PassportFile.php index 32db049..a4a0a0a 100644 --- a/src/TgBotLib/Objects/Passport/PassportFile.php +++ b/src/TgBotLib/Objects/Passport/PassportFile.php @@ -7,25 +7,10 @@ class PassportFile implements ObjectTypeInterface { - /** - * @var string - */ - private $file_id; - - /** - * @var string - */ - private $file_unique_id; - - /** - * @var int - */ - private $file_size; - - /** - * @var int - */ - private $file_date; + private string $file_id; + private string $file_unique_id; + private int $file_size; + private int $file_date; /** * Identifier for this file, which can be used to download or reuse the file @@ -69,9 +54,7 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -84,15 +67,16 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return PassportFile + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?PassportFile { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->file_id = $data['file_id'] ?? null; $object->file_unique_id = $data['file_unique_id'] ?? null; $object->file_size = $data['file_size'] ?? null;