From c4c307fcf17ded43a2b99249a6dd7ea39b350474 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 30 Sep 2024 13:04:23 -0400 Subject: [PATCH] Add message origin classes --- .../Enums/Types/MessageOriginType.php | 11 +++ src/TgBotLib/Methods/SendMessage.php | 30 ++++++++ .../Objects/Telegram/MessageOrigin.php | 67 +++++++++++++++++ .../MessageOrigin/MessageOriginChannel.php | 75 +++++++++++++++++++ .../MessageOrigin/MessageOriginChat.php | 62 +++++++++++++++ .../MessageOrigin/MessageOriginHiddenUser.php | 49 ++++++++++++ .../MessageOrigin/MessageOriginUser.php | 49 ++++++++++++ 7 files changed, 343 insertions(+) create mode 100644 src/TgBotLib/Enums/Types/MessageOriginType.php create mode 100644 src/TgBotLib/Methods/SendMessage.php create mode 100644 src/TgBotLib/Objects/Telegram/MessageOrigin.php create mode 100644 src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChannel.php create mode 100644 src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChat.php create mode 100644 src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginHiddenUser.php create mode 100644 src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginUser.php diff --git a/src/TgBotLib/Enums/Types/MessageOriginType.php b/src/TgBotLib/Enums/Types/MessageOriginType.php new file mode 100644 index 0000000..0686ca7 --- /dev/null +++ b/src/TgBotLib/Enums/Types/MessageOriginType.php @@ -0,0 +1,11 @@ +value, $parameters))); + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/MessageOrigin.php b/src/TgBotLib/Objects/Telegram/MessageOrigin.php new file mode 100644 index 0000000..3fee3a5 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/MessageOrigin.php @@ -0,0 +1,67 @@ +type; + } + + /** + * Retrieves the date. + * + * @return int The date value. + */ + public function getDate(): int + { + return $this->date; + } + + /** + * Converts the object to an array representation. + * + * @return array The array representation of the object. + */ + 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. + */ + public static function fromArray(array $data): MessageOrigin + { + if(!isset($data['type'])) + { + throw new InvalidArgumentException('Message origin type is required'); + } + + return match (MessageOriginType::tryFrom($data['type'])) + { + MessageOriginType::USER => MessageOriginUser::fromArray($data), + MessageOriginType::CHAT => MessageOriginChat::fromArray($data), + MessageOriginType::HIDDEN_USER => MessageOriginHiddenUser::fromArray($data), + MessageOriginType::CHANNEL => MessageOriginChannel::fromArray($data), + default => throw new InvalidArgumentException('Unknown message origin type'), + }; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChannel.php b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChannel.php new file mode 100644 index 0000000..815aadb --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChannel.php @@ -0,0 +1,75 @@ +chat; + } + + /** + * Unique message identifier inside the chat + * + * @return int + */ + public function getMessageId(): int + { + return $this->message_id; + } + + /** + * Optional. Signature of the original post author + * + * @return string|null + */ + public function getAuthorSignature(): ?string + { + return $this->author_signature; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->getType()->value, + 'date' => $this->getDate(), + 'chat' => $this->chat->toArray(), + 'message_id' => $this->message_id, + 'author_signature' => $this->author_signature, + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): MessageOrigin + { + $object = new self(); + + $object->type = MessageOriginType::CHANNEL; + $object->date = $data['date']; + $object->chat = Chat::fromArray($data['chat']); + $object->message_id = $data['message_id']; + $object->author_signature = $data['author_signature'] ?? null; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChat.php b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChat.php new file mode 100644 index 0000000..aafaf5d --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginChat.php @@ -0,0 +1,62 @@ +sender_chat; + } + + /** + * Optional. Signature of the original post author + * + * @return string|null + */ + public function getAuthorSignature(): ?string + { + return $this->author_signature; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->getType()->value, + 'date' => $this->getDate(), + 'sender_chat' => $this->sender_chat->toArray(), + 'author_signature' => $this->author_signature, + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): MessageOrigin + { + $object = new self(); + + $object->type = MessageOriginType::CHAT; + $object->date = $data['date']; + $object->sender_chat = Chat::fromArray($data['sender_chat']); + $object->author_signature = $data['author_signature'] ?? null; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginHiddenUser.php b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginHiddenUser.php new file mode 100644 index 0000000..bc2421b --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginHiddenUser.php @@ -0,0 +1,49 @@ +sender_user_name; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->getType()->value, + 'date' => $this->getDate(), + 'sender_user_name' => $this->sender_user_name + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): MessageOrigin + { + $object = new self(); + + $object->type = MessageOriginType::HIDDEN_USER; + $object->date = $data['date']; + $object->sender_user_name = $data['sender_user_name']; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginUser.php b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginUser.php new file mode 100644 index 0000000..1b84d72 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/MessageOrigin/MessageOriginUser.php @@ -0,0 +1,49 @@ +sender_user; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->getType()->value, + 'date' => $this->getDate(), + 'sender_user' => $this->sender_user->toArray() + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): MessageOrigin + { + $object = new self(); + + $object->type = MessageOriginType::USER; + $object->date = $data['date']; + $object->sender_user = User::fromArray($data['sender_user']); + + return $object; + } +} \ No newline at end of file