From 2405873510b734549892c68133b8dcc63f755fa2 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 4 Oct 2024 00:47:04 -0400 Subject: [PATCH] Updated MessageOrigin objects --- src/TgBotLib/Objects/MessageOrigin.php | 17 ++++++++--------- .../MessageOrigin/MessageOriginChannel.php | 12 ++++++++---- .../Objects/MessageOrigin/MessageOriginChat.php | 12 ++++++++---- .../MessageOrigin/MessageOriginHiddenUser.php | 12 ++++++++---- .../Objects/MessageOrigin/MessageOriginUser.php | 12 ++++++++---- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/TgBotLib/Objects/MessageOrigin.php b/src/TgBotLib/Objects/MessageOrigin.php index 161666d..3ec1601 100644 --- a/src/TgBotLib/Objects/MessageOrigin.php +++ b/src/TgBotLib/Objects/MessageOrigin.php @@ -35,21 +35,20 @@ abstract class MessageOrigin implements ObjectTypeInterface } /** - * Converts the object to an array representation. - * - * @return array The array representation of the object. + * @inheritDoc */ public abstract function toArray(): array; /** - * Converts an associative array to a MessageOrigin object. - * - * @param array $data The data representing the message origin. - * @return MessageOrigin The constructed MessageOrigin object. - * @throws InvalidArgumentException If the 'type' key is missing or an unknown type is provided. + * @inheritDoc */ - public static function fromArray(array $data): MessageOrigin + public static function fromArray(?array $data): ?MessageOrigin { + if($data === null) + { + return null; + } + if(!isset($data['type'])) { throw new InvalidArgumentException('Message origin type is required'); diff --git a/src/TgBotLib/Objects/MessageOrigin/MessageOriginChannel.php b/src/TgBotLib/Objects/MessageOrigin/MessageOriginChannel.php index d83b1f4..63aec9f 100644 --- a/src/TgBotLib/Objects/MessageOrigin/MessageOriginChannel.php +++ b/src/TgBotLib/Objects/MessageOrigin/MessageOriginChannel.php @@ -49,8 +49,8 @@ class MessageOriginChannel extends MessageOrigin implements ObjectTypeInterface public function toArray(): array { return [ - 'type' => $this->getType()->value, - 'date' => $this->getDate(), + 'type' => $this->type->value, + 'date' => $this->date, 'chat' => $this->chat->toArray(), 'message_id' => $this->message_id, 'author_signature' => $this->author_signature, @@ -60,10 +60,14 @@ class MessageOriginChannel extends MessageOrigin implements ObjectTypeInterface /** * @inheritDoc */ - public static function fromArray(array $data): MessageOrigin + public static function fromArray(?array $data): ?MessageOrigin { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->type = MessageOriginType::CHANNEL; $object->date = $data['date']; $object->chat = Chat::fromArray($data['chat']); diff --git a/src/TgBotLib/Objects/MessageOrigin/MessageOriginChat.php b/src/TgBotLib/Objects/MessageOrigin/MessageOriginChat.php index a97e948..83c6e4a 100644 --- a/src/TgBotLib/Objects/MessageOrigin/MessageOriginChat.php +++ b/src/TgBotLib/Objects/MessageOrigin/MessageOriginChat.php @@ -38,8 +38,8 @@ class MessageOriginChat extends MessageOrigin implements ObjectTypeInterface public function toArray(): array { return [ - 'type' => $this->getType()->value, - 'date' => $this->getDate(), + 'type' => $this->type->value, + 'date' => $this->date, 'sender_chat' => $this->sender_chat->toArray(), 'author_signature' => $this->author_signature, ]; @@ -48,10 +48,14 @@ class MessageOriginChat extends MessageOrigin implements ObjectTypeInterface /** * @inheritDoc */ - public static function fromArray(array $data): MessageOrigin + public static function fromArray(?array $data): ?MessageOrigin { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->type = MessageOriginType::CHAT; $object->date = $data['date']; $object->sender_chat = Chat::fromArray($data['sender_chat']); diff --git a/src/TgBotLib/Objects/MessageOrigin/MessageOriginHiddenUser.php b/src/TgBotLib/Objects/MessageOrigin/MessageOriginHiddenUser.php index e260c6b..a8a02c1 100644 --- a/src/TgBotLib/Objects/MessageOrigin/MessageOriginHiddenUser.php +++ b/src/TgBotLib/Objects/MessageOrigin/MessageOriginHiddenUser.php @@ -26,8 +26,8 @@ class MessageOriginHiddenUser extends MessageOrigin implements ObjectTypeInterfa public function toArray(): array { return [ - 'type' => $this->getType()->value, - 'date' => $this->getDate(), + 'type' => $this->type->value, + 'date' => $this->date, 'sender_user_name' => $this->sender_user_name ]; } @@ -35,10 +35,14 @@ class MessageOriginHiddenUser extends MessageOrigin implements ObjectTypeInterfa /** * @inheritDoc */ - public static function fromArray(array $data): MessageOrigin + public static function fromArray(?array $data): ?MessageOrigin { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->type = MessageOriginType::HIDDEN_USER; $object->date = $data['date']; $object->sender_user_name = $data['sender_user_name']; diff --git a/src/TgBotLib/Objects/MessageOrigin/MessageOriginUser.php b/src/TgBotLib/Objects/MessageOrigin/MessageOriginUser.php index e3fcf1b..7e77ca7 100644 --- a/src/TgBotLib/Objects/MessageOrigin/MessageOriginUser.php +++ b/src/TgBotLib/Objects/MessageOrigin/MessageOriginUser.php @@ -27,8 +27,8 @@ class MessageOriginUser extends MessageOrigin implements ObjectTypeInterface public function toArray(): array { return [ - 'type' => $this->getType()->value, - 'date' => $this->getDate(), + 'type' => $this->type->value, + 'date' => $this->date, 'sender_user' => $this->sender_user->toArray() ]; } @@ -36,10 +36,14 @@ class MessageOriginUser extends MessageOrigin implements ObjectTypeInterface /** * @inheritDoc */ - public static function fromArray(array $data): MessageOrigin + public static function fromArray(?array $data): ?MessageOrigin { - $object = new self(); + if($data === null) + { + return null; + } + $object = new self(); $object->type = MessageOriginType::USER; $object->date = $data['date']; $object->sender_user = User::fromArray($data['sender_user']);