From 79278e77482da4365cf7a5e47d152d5e516226fe Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 2 Oct 2024 00:32:13 -0400 Subject: [PATCH] Updated BotCommandScope --- src/TgBotLib/Objects/BotCommandScope.php | 94 ++++++------------- .../BotCommandScopeAllChatAdministrators.php | 40 +------- .../BotCommandScopeAllGroupChats.php | 46 +-------- .../BotCommandScopeAllPrivateChats.php | 48 ++-------- .../BotCommandScope/BotCommandScopeChat.php | 50 ++-------- .../BotCommandScopeChatAdministrators.php | 45 +-------- .../BotCommandScopeChatMember.php | 57 ++--------- .../BotCommandScopeDefault.php | 48 ++-------- 8 files changed, 66 insertions(+), 362 deletions(-) diff --git a/src/TgBotLib/Objects/BotCommandScope.php b/src/TgBotLib/Objects/BotCommandScope.php index e28f91e..71c43f1 100644 --- a/src/TgBotLib/Objects/BotCommandScope.php +++ b/src/TgBotLib/Objects/BotCommandScope.php @@ -4,100 +4,60 @@ namespace TgBotLib\Objects; + use InvalidArgumentException; + use TgBotLib\Enums\Types\BotCommandScopeType; use TgBotLib\Interfaces\ObjectTypeInterface; + use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllChatAdministrators; + use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllGroupChats; + use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllPrivateChats; use TgBotLib\Objects\BotCommandScope\BotCommandScopeChat; use TgBotLib\Objects\BotCommandScope\BotCommandScopeChatAdministrators; use TgBotLib\Objects\BotCommandScope\BotCommandScopeChatMember; + use TgBotLib\Objects\BotCommandScope\BotCommandScopeDefault; - class BotCommandScope implements ObjectTypeInterface + abstract class BotCommandScope implements ObjectTypeInterface { /** - * @var string + * @var BotCommandScopeType */ - private $type; - - /** - * @var string|int|null - */ - private $chat_id; - - /** - * @var int|null - */ - private $user_id; + protected $type; /** * Scope type, one of “default”, “all_private_chats”, “all_group_chats”, “all_chat_administrators”, “chat”, * “chat_administrators”, “chat_member” * - * @return string + * @return BotCommandScopeType */ - public function getType(): string + public function getType(): BotCommandScopeType { return $this->type; } /** - * Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) - * - * @return int|string|null + * @inheritDoc */ - public function getChatId(): int|string|null - { - return $this->chat_id; - } + public abstract function toArray(): array; /** - * Unique identifier of the target user - * - * @return int|null - */ - public function getUserId(): ?int - { - return $this->user_id; - } - - /** - * Returns an array representation of the object - * - * @return array - */ - public function toArray(): array - { - return [ - 'type' => $this->type, - 'chat_id' => $this->chat_id, - 'user_id' => $this->user_id, - ]; - } - - /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ public static function fromArray(array $data): ObjectTypeInterface { - if(isset($data['type'])) + if(!isset($data['type'])) { - switch($data['type']) - { - case 'chat': - return BotCommandScopeChat::fromArray($data); - case 'chat_administrators': - return BotCommandScopeChatAdministrators::fromArray($data); - case 'chat_member': - return BotCommandScopeChatMember::fromArray($data); - } + throw new InvalidArgumentException('BotCommandScope expected type'); } - $object = new self(); - - $object->type = $data['type'] ?? null; - $object->chat_id = $data['chat_id'] ?? null; - $object->user_id = $data['user_id'] ?? null; - - return $object; + return match(BotCommandScopeType::tryFrom($data['type'])) + { + BotCommandScopeType::DEFAULT => BotCommandScopeDefault::fromArray($data), + BotCommandScopeType::ALL_PRIVATE_CHATS => BotCommandScopeAllPrivateChats::fromArray($data), + BotCommandScopeType::ALL_CHAT_GROUPS => BotCommandScopeAllGroupChats::fromArray($data), + BotCommandScopeType::ALL_CHAT_ADMINISTRATORS => BotCommandScopeChatAdministrators::fromArray($data), + BotCommandScopeType::CHAT => BotCommandScopeChat::fromArray($data), + BotCommandScopeType::CHAT_ADMINISTRATORS => BotCommandScopeAllChatAdministrators::fromArray($data), + BotCommandScopeType::CHAT_MEMBER => BotCommandScopeChatMember::fromArray($data), + default => throw new InvalidArgumentException('Unexpected match value') + }; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllChatAdministrators.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllChatAdministrators.php index 4e6d066..8280dcf 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllChatAdministrators.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllChatAdministrators.php @@ -8,28 +8,13 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeAllChatAdministrators implements ObjectTypeInterface + class BotCommandScopeAllChatAdministrators extends BotCommandScope implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - /** * @var int|string */ private $chat_id; - /** - * Scope type, must be all_chat_administrators - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - /** * Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) * @@ -48,7 +33,7 @@ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'chat_id' => $this->chat_id ]; } @@ -59,29 +44,12 @@ * @param array $data * @return BotCommandScopeAllChatAdministrators */ - public static function fromArray(array $data): self + public static function fromArray(array $data): BotCommandScopeAllChatAdministrators { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::ALL_CHAT_ADMINISTRATORS; + $object->type = BotCommandScopeType::ALL_CHAT_ADMINISTRATORS; $object->chat_id = $data['chat_id'] ?? null; return $object; } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return static - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): self - { - $object = new self(); - - $object->type = $botCommandScope->getType(); - $object->chat_id = $botCommandScope->getChatId(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllGroupChats.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllGroupChats.php index 4a271f2..cf759d4 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllGroupChats.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllGroupChats.php @@ -8,61 +8,25 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeAllGroupChats implements ObjectTypeInterface + class BotCommandScopeAllGroupChats extends BotCommandScope implements ObjectTypeInterface { /** - * @var string - */ - private $type; - - /** - * Scope type, must be all_group_chats - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type + 'type' => $this->type->value ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeAllGroupChats + * @inheritDoc */ public static function fromArray(array $data): self { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::ALL_CHAT_GROUPS; - - return $object; - } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return BotCommandScopeAllGroupChats - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): BotCommandScopeAllGroupChats - { - $object = new self(); - - $object->type = $botCommandScope->getType(); + $object->type = BotCommandScopeType::ALL_CHAT_GROUPS; return $object; } diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllPrivateChats.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllPrivateChats.php index ae4a06e..b383bfc 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllPrivateChats.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeAllPrivateChats.php @@ -8,61 +8,25 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeAllPrivateChats implements ObjectTypeInterface + class BotCommandScopeAllPrivateChats extends BotCommandScope implements ObjectTypeInterface { /** - * @var string - */ - private $type; - - /** - * Scope type, must be all_private_chats - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type + 'type' => $this->type->value ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeAllPrivateChats + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): BotCommandScopeAllPrivateChats { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::ALL_PRIVATE_CHATS; - - return $object; - } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return BotCommandScopeAllPrivateChats - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): BotCommandScopeAllPrivateChats - { - $object = new self(); - - $object->type = $botCommandScope->getType(); + $object->type = BotCommandScopeType::ALL_PRIVATE_CHATS; return $object; } diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChat.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChat.php index 7ef019a..2cbb929 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChat.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChat.php @@ -8,27 +8,12 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeChat implements ObjectTypeInterface + class BotCommandScopeChat extends BotCommandScope implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - /** * @var int|string */ - private $chat_id; - - /** - * Scope type, must be chat - * - * @return string - */ - public function getType(): string - { - return $this->type; - } + private int|string $chat_id; /** * Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) @@ -41,48 +26,25 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'chat_id' => $this->chat_id ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeChat + * @inheritDoc */ public static function fromArray(array $data): self { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::CHAT; + $object->type = BotCommandScopeType::CHAT; $object->chat_id = $data['chat_id'] ?? null; return $object; } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return static - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): self - { - $object = new self(); - - $object->type = $botCommandScope->getType(); - $object->chat_id = $botCommandScope->getChatId(); - - return $object; - } - } \ No newline at end of file diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatAdministrators.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatAdministrators.php index aad1046..dbb678a 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatAdministrators.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatAdministrators.php @@ -8,61 +8,26 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeChatAdministrators implements ObjectTypeInterface + class BotCommandScopeChatAdministrators extends BotCommandScope implements ObjectTypeInterface { - /** - * @var string - */ - private $type; /** - * Scope type, must be chat_administrators - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type + 'type' => $this->type->value ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeChatAdministrators + * @inheritDoc */ public static function fromArray(array $data): self { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::CHAT_ADMINISTRATORS; - - return $object; - } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return BotCommandScopeChatAdministrators - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): BotCommandScopeChatAdministrators - { - $object = new self(); - - $object->type = $botCommandScope->getType(); + $object->type = BotCommandScopeType::CHAT_ADMINISTRATORS; return $object; } diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatMember.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatMember.php index 26920c2..c95f40d 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatMember.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeChatMember.php @@ -8,32 +8,10 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeChatMember implements ObjectTypeInterface + class BotCommandScopeChatMember extends BotCommandScope implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string|int - */ - private $chat_id; - - /** - * @var int - */ - private $user_id; - - /** - * Scope type, must be chat_member - * - * @return string - */ - public function getType(): string - { - return $this->type; - } + private string|int $chat_id; + private int $user_id; /** * Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) @@ -56,48 +34,27 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'chat_id' => $this->chat_id, 'user_id' => $this->user_id ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeChatMember + * @inheritDoc */ public static function fromArray(array $data): self { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::CHAT_MEMBER; + $object->type = BotCommandScopeType::CHAT_MEMBER; $object->chat_id = $data['chat_id'] ?? null; $object->user_id = $data['user_id'] ?? null; return $object; } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return static - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): self - { - $object = new self(); - $object->type = $botCommandScope->getType(); - $object->chat_id = $botCommandScope->getChatId(); - $object->user_id = $botCommandScope->getUserId(); - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeDefault.php b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeDefault.php index 79d0e1f..9ffe53e 100644 --- a/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeDefault.php +++ b/src/TgBotLib/Objects/BotCommandScope/BotCommandScopeDefault.php @@ -8,61 +8,25 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\BotCommandScope; - class BotCommandScopeDefault implements ObjectTypeInterface + class BotCommandScopeDefault extends BotCommandScope implements ObjectTypeInterface { /** - * @var string - */ - private $type; - - /** - * Scope type, must be default - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Returns an array representation of the object - * - * @return string[] + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type + 'type' => $this->type->value ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return BotCommandScopeDefault + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): BotCommandScopeDefault { $object = new self(); - - $object->type = $data['type'] ?? BotCommandScopeType::DEFAULT; - - return $object; - } - - /** - * Constructs object from BotCommandScope - * - * @param BotCommandScope $botCommandScope - * @return BotCommandScopeDefault - */ - public static function fromBotCommandScope(BotCommandScope $botCommandScope): BotCommandScopeDefault - { - $object = new self(); - - $object->type = $botCommandScope->getType(); + $object->type = BotCommandScopeType::DEFAULT; return $object; }