From 5426dd9e1c0ebf548e68bde3ca375391463f6dee Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 4 Oct 2024 00:45:09 -0400 Subject: [PATCH] Updated MenuButton objects --- src/TgBotLib/Objects/MenuButton.php | 87 ++++++------------- .../Objects/MenuButton/MenuButtonCommands.php | 55 +++--------- .../Objects/MenuButton/MenuButtonDefault.php | 55 +++--------- .../Objects/MenuButton/MenuButtonWebApp.php | 63 ++------------ 4 files changed, 61 insertions(+), 199 deletions(-) diff --git a/src/TgBotLib/Objects/MenuButton.php b/src/TgBotLib/Objects/MenuButton.php index b4d1e4f..d19aee4 100644 --- a/src/TgBotLib/Objects/MenuButton.php +++ b/src/TgBotLib/Objects/MenuButton.php @@ -4,84 +4,53 @@ namespace TgBotLib\Objects; + use InvalidArgumentException; + use TgBotLib\Enums\Types\MenuButtonType; use TgBotLib\Interfaces\ObjectTypeInterface; + use TgBotLib\Objects\MenuButton\MenuButtonCommands; + use TgBotLib\Objects\MenuButton\MenuButtonDefault; + use TgBotLib\Objects\MenuButton\MenuButtonWebApp; - class MenuButton implements ObjectTypeInterface + abstract class MenuButton implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string|null - */ - private $text; - - /** - * @var WebAppInfo|null - */ - private $web_app; + protected MenuButtonType $type; /** * Type of the button, must be web_app * - * @return string + * @return MenuButtonType */ - public function getType(): string + public function getType(): MenuButtonType { return $this->type; } /** - * Text on the button - * - * @return string|null + * @inheritDoc */ - public function getText(): ?string - { - return $this->text; - } + public abstract function toArray(): array; /** - * Description of the Web App that will be launched when the user presses the button. The Web App will be able - * to send an arbitrary message on behalf of the user using the method answerWebAppQuery. - * - * @return WebAppInfo|null + * @inheritDoc */ - public function getWebApp(): ?WebAppInfo + public static function fromArray(?array $data): ?MenuButton { - return $this->web_app; - } + if($data === null) + { + return null; + } - /** - * Returns an array representation of the object - * - * @return array - */ - public function toArray(): array - { - return [ - 'type' => $this->type, - 'text' => $this->text, - 'web_app' => ($this->web_app instanceof ObjectTypeInterface) ? $this->web_app->toArray() : null - ]; - } + if(!isset($data['type'])) + { + throw new InvalidArgumentException('MenuButton expected property type'); + } - /** - * Constructs object from an array representation - * - * @param array $data - * @return MenuButton - */ - public static function fromArray(array $data): self - { - $object = new self(); - - $object->type = $data['type'] ?? null; - $object->text = $data['text'] ?? null; - $object->web_app = ($data['web_app'] ?? null) ? WebAppInfo::fromArray($data['web_app']) : null; - - return $object; + return match(MenuButtonType::tryFrom($data['type'])) + { + MenuButtonType::COMMANDS => MenuButtonCommands::fromArray($data), + MenuButtonType::DEFAULT => MenuButtonDefault::fromArray($data), + MenuButtonType::WEB_APP => MenuButtonWebApp::fromArray($data), + default => throw new InvalidArgumentException('Unexpected MenuButton Type') + }; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/MenuButton/MenuButtonCommands.php b/src/TgBotLib/Objects/MenuButton/MenuButtonCommands.php index 2fdab2c..a1c3882 100644 --- a/src/TgBotLib/Objects/MenuButton/MenuButtonCommands.php +++ b/src/TgBotLib/Objects/MenuButton/MenuButtonCommands.php @@ -4,65 +4,34 @@ namespace TgBotLib\Objects\MenuButton; + use TgBotLib\Enums\Types\MenuButtonType; use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\MenuButton; - class MenuButtonCommands implements ObjectTypeInterface + class MenuButtonCommands extends MenuButton implements ObjectTypeInterface { /** - * @var string - */ - private $type; - - /** - * Type of the button, must be commands - * - * @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->getType(), + 'type' => $this->type->value, ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return MenuButtonCommands + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?MenuButtonCommands { + if($data === null) + { + return null; + } + $object = new self(); - - $object->type = $data['type'] ?? null; - - return $object; - } - - /** - * Constructs object from MenuButton - * - * @param MenuButton $menuButton - * @return MenuButtonCommands - */ - public static function fromMenuButton(MenuButton $menuButton): MenuButtonCommands - { - $object = new self(); - - $object->type = $menuButton->getType(); - + $object->type = MenuButtonType::COMMANDS; return $object; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/MenuButton/MenuButtonDefault.php b/src/TgBotLib/Objects/MenuButton/MenuButtonDefault.php index 6e4d0c1..64f28b7 100644 --- a/src/TgBotLib/Objects/MenuButton/MenuButtonDefault.php +++ b/src/TgBotLib/Objects/MenuButton/MenuButtonDefault.php @@ -4,65 +4,34 @@ namespace TgBotLib\Objects\MenuButton; + use TgBotLib\Enums\Types\MenuButtonType; use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\MenuButton; - class MenuButtonDefault implements ObjectTypeInterface + class MenuButtonDefault extends MenuButton implements ObjectTypeInterface { /** - * @var string - */ - private $type; - - /** - * Type of the button, must be default - * - * @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->getType(), + 'type' => $this->type->value, ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return MenuButtonDefault + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): ?MenuButtonDefault { + if($data === null) + { + return null; + } + $object = new self(); - - $object->type = $data['type'] ?? null; - - return $object; - } - - /** - * Constructs object from MenuButton - * - * @param MenuButton $menuButton - * @return MenuButtonDefault - */ - public static function fromMenuButton(MenuButton $menuButton): MenuButtonDefault - { - $object = new self(); - - $object->type = $menuButton->getType(); - + $object->type = MenuButtonType::DEFAULT; return $object; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/MenuButton/MenuButtonWebApp.php b/src/TgBotLib/Objects/MenuButton/MenuButtonWebApp.php index a23d79f..ae6fce3 100644 --- a/src/TgBotLib/Objects/MenuButton/MenuButtonWebApp.php +++ b/src/TgBotLib/Objects/MenuButton/MenuButtonWebApp.php @@ -8,32 +8,10 @@ use TgBotLib\Objects\MenuButton; use TgBotLib\Objects\WebAppInfo; - class MenuButtonWebApp implements ObjectTypeInterface + class MenuButtonWebApp extends MenuButton implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $text; - - /** - * @var WebAppInfo - */ - private $web_app; - - /** - * Type of the button, must be web_app - * - * @return string - */ - public function getType(): string - { - return $this->type; - } + private string $text; + private WebAppInfo $web_app; /** * Text on the button @@ -58,49 +36,26 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'text' => $this->text, - 'web_app' => ($this->web_app instanceof ObjectTypeInterface) ? $this->web_app->toArray() : null + 'web_app' => $this->web_app?->toArray() ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return MenuButtonWebApp + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(?array $data): MenuButtonWebApp { $object = new self(); - $object->type = $data['type'] ?? null; $object->text = $data['text'] ?? null; - $object->web_app = isset($data['web_app']) ? WebAppInfo::fromArray($data['web_app']) : null; - - return $object; - } - - /** - * Constructs object from MenuButton - * - * @param MenuButton $menuButton - * @return MenuButtonWebApp - */ - public static function fromMenuButton(MenuButton $menuButton): MenuButtonWebApp - { - $object = new self(); - - $object->type = $menuButton->getType(); - $object->text = $menuButton->getText(); - $object->web_app = $menuButton->getWebApp(); + $object->web_app = isset($data['web_app']) ? WebAppInfo::fromArray($data['web_app'] ?? []) : null; return $object; }