Updated MenuButton objects
This commit is contained in:
parent
3774b7f8c0
commit
5426dd9e1c
4 changed files with 61 additions and 199 deletions
|
@ -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')
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue