Updated MenuButton objects

This commit is contained in:
netkas 2024-10-04 00:45:09 -04:00
parent 3774b7f8c0
commit 5426dd9e1c
4 changed files with 61 additions and 199 deletions

View file

@ -4,84 +4,53 @@
namespace TgBotLib\Objects; namespace TgBotLib\Objects;
use InvalidArgumentException;
use TgBotLib\Enums\Types\MenuButtonType;
use TgBotLib\Interfaces\ObjectTypeInterface; 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
{ {
/** protected MenuButtonType $type;
* @var string
*/
private $type;
/**
* @var string|null
*/
private $text;
/**
* @var WebAppInfo|null
*/
private $web_app;
/** /**
* Type of the button, must be web_app * Type of the button, must be web_app
* *
* @return string * @return MenuButtonType
*/ */
public function getType(): string public function getType(): MenuButtonType
{ {
return $this->type; return $this->type;
} }
/** /**
* Text on the button * @inheritDoc
*
* @return string|null
*/ */
public function getText(): ?string public abstract function toArray(): array;
{
return $this->text;
}
/** /**
* Description of the Web App that will be launched when the user presses the button. The Web App will be able * @inheritDoc
* to send an arbitrary message on behalf of the user using the method answerWebAppQuery.
*
* @return WebAppInfo|null
*/ */
public function getWebApp(): ?WebAppInfo public static function fromArray(?array $data): ?MenuButton
{ {
return $this->web_app; if($data === null)
} {
return null;
}
/** if(!isset($data['type']))
* Returns an array representation of the object {
* throw new InvalidArgumentException('MenuButton expected property type');
* @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
];
}
/** return match(MenuButtonType::tryFrom($data['type']))
* Constructs object from an array representation {
* MenuButtonType::COMMANDS => MenuButtonCommands::fromArray($data),
* @param array $data MenuButtonType::DEFAULT => MenuButtonDefault::fromArray($data),
* @return MenuButton MenuButtonType::WEB_APP => MenuButtonWebApp::fromArray($data),
*/ default => throw new InvalidArgumentException('Unexpected MenuButton Type')
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;
} }
} }

View file

@ -4,65 +4,34 @@
namespace TgBotLib\Objects\MenuButton; namespace TgBotLib\Objects\MenuButton;
use TgBotLib\Enums\Types\MenuButtonType;
use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\MenuButton; use TgBotLib\Objects\MenuButton;
class MenuButtonCommands implements ObjectTypeInterface class MenuButtonCommands extends MenuButton implements ObjectTypeInterface
{ {
/** /**
* @var string * @inheritDoc
*/
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[]
*/ */
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType(), 'type' => $this->type->value,
]; ];
} }
/** /**
* Constructs object from an array representation * @inheritDoc
*
* @param array $data
* @return MenuButtonCommands
*/ */
public static function fromArray(array $data): self public static function fromArray(?array $data): ?MenuButtonCommands
{ {
if($data === null)
{
return null;
}
$object = new self(); $object = new self();
$object->type = MenuButtonType::COMMANDS;
$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();
return $object; return $object;
} }
} }

View file

@ -4,65 +4,34 @@
namespace TgBotLib\Objects\MenuButton; namespace TgBotLib\Objects\MenuButton;
use TgBotLib\Enums\Types\MenuButtonType;
use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\MenuButton; use TgBotLib\Objects\MenuButton;
class MenuButtonDefault implements ObjectTypeInterface class MenuButtonDefault extends MenuButton implements ObjectTypeInterface
{ {
/** /**
* @var string * @inheritDoc
*/
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
*/ */
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType(), 'type' => $this->type->value,
]; ];
} }
/** /**
* Constructs object from an array representation * @inheritDoc
*
* @param array $data
* @return MenuButtonDefault
*/ */
public static function fromArray(array $data): self public static function fromArray(?array $data): ?MenuButtonDefault
{ {
if($data === null)
{
return null;
}
$object = new self(); $object = new self();
$object->type = MenuButtonType::DEFAULT;
$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();
return $object; return $object;
} }
} }

View file

@ -8,32 +8,10 @@
use TgBotLib\Objects\MenuButton; use TgBotLib\Objects\MenuButton;
use TgBotLib\Objects\WebAppInfo; use TgBotLib\Objects\WebAppInfo;
class MenuButtonWebApp implements ObjectTypeInterface class MenuButtonWebApp extends MenuButton implements ObjectTypeInterface
{ {
/** private string $text;
* @var string private WebAppInfo $web_app;
*/
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;
}
/** /**
* Text on the button * Text on the button
@ -58,49 +36,26 @@
} }
/** /**
* Returns an array representation of the object * @inheritDoc
*
* @return array
*/ */
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->type, 'type' => $this->type->value,
'text' => $this->text, '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 * @inheritDoc
*
* @param array $data
* @return MenuButtonWebApp
*/ */
public static function fromArray(array $data): self public static function fromArray(?array $data): MenuButtonWebApp
{ {
$object = new self(); $object = new self();
$object->type = $data['type'] ?? null; $object->type = $data['type'] ?? null;
$object->text = $data['text'] ?? null; $object->text = $data['text'] ?? null;
$object->web_app = isset($data['web_app']) ? WebAppInfo::fromArray($data['web_app']) : 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();
return $object; return $object;
} }