diff --git a/src/TgBotLib/Objects/InlineKeyboardButton.php b/src/TgBotLib/Objects/Inline/InlineKeyboardButton.php similarity index 98% rename from src/TgBotLib/Objects/InlineKeyboardButton.php rename to src/TgBotLib/Objects/Inline/InlineKeyboardButton.php index efe85ab..517f04e 100644 --- a/src/TgBotLib/Objects/InlineKeyboardButton.php +++ b/src/TgBotLib/Objects/Inline/InlineKeyboardButton.php @@ -2,11 +2,14 @@ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects; + namespace TgBotLib\Objects\Inline; use InvalidArgumentException; use TgBotLib\Classes\Validate; use TgBotLib\Interfaces\ObjectTypeInterface; + use TgBotLib\Objects\CallbackGame; + use TgBotLib\Objects\LoginUrl; + use TgBotLib\Objects\WebAppInfo; class InlineKeyboardButton implements ObjectTypeInterface { diff --git a/src/TgBotLib/Objects/InlineKeyboardMarkup.php b/src/TgBotLib/Objects/Inline/InlineKeyboardMarkup.php similarity index 98% rename from src/TgBotLib/Objects/InlineKeyboardMarkup.php rename to src/TgBotLib/Objects/Inline/InlineKeyboardMarkup.php index 012bd2a..e0bd3c7 100644 --- a/src/TgBotLib/Objects/InlineKeyboardMarkup.php +++ b/src/TgBotLib/Objects/Inline/InlineKeyboardMarkup.php @@ -2,7 +2,7 @@ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects; + namespace TgBotLib\Objects\Inline; use TgBotLib\Interfaces\ObjectTypeInterface; diff --git a/src/TgBotLib/Objects/InlineQuery.php b/src/TgBotLib/Objects/Inline/InlineQuery.php similarity index 97% rename from src/TgBotLib/Objects/InlineQuery.php rename to src/TgBotLib/Objects/Inline/InlineQuery.php index 9e84c74..950ecb7 100644 --- a/src/TgBotLib/Objects/InlineQuery.php +++ b/src/TgBotLib/Objects/Inline/InlineQuery.php @@ -2,9 +2,11 @@ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects; + namespace TgBotLib\Objects\Inline; use TgBotLib\Interfaces\ObjectTypeInterface; + use TgBotLib\Objects\Location; + use TgBotLib\Objects\User; class InlineQuery implements ObjectTypeInterface { diff --git a/src/TgBotLib/Objects/Inline/InlineQueryResult.php b/src/TgBotLib/Objects/Inline/InlineQueryResult.php new file mode 100644 index 0000000..5d7d350 --- /dev/null +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult.php @@ -0,0 +1,78 @@ +type; + } + + /** + * Unique identifier for this result, 1-64 Bytes + * + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * Sets the Unique Identifier for this result, must be 1-64 characters + * + * @param string $id The ID to set + * @throws InvalidArgumentException If the size of the ID is empty or over 64 characters long + * @return $this + */ + public function setId(string $id): InlineQueryResult + { + if(!Validate::length($id, 1, 64)) + { + throw new InvalidArgumentException(sprintf('id must be between 1 and 64 characters long, got %s characters', $id)); + } + + $this->id = $id; + return $this; + } + + /** + * @inheritDoc + */ + public abstract function toArray(): array; + + /** + * @inheritDoc + */ + public static function fromArray(array $data): ObjectTypeInterface + { + // TODO: Implement this + } + } \ No newline at end of file diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultArticle.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultArticle.php similarity index 62% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultArticle.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultArticle.php index 9098b15..90e5584 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultArticle.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultArticle.php @@ -1,122 +1,26 @@ type = 'article'; - } - - /** - * The Type of the result must be article - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 Bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of the 'id' field. - * Unique identifier for this result, 1-64 Bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultArticle - { - if(!Validate::length($id, 1, 64)) - { - throw new InvalidArgumentException('id should be between 1-64 characters'); - } - - $this->id = $id; - return $this; - } + private string $title; + private InputMessageContent $input_message_content; + private ?InlineKeyboardMarkup $reply_markup; + private ?string $url; + private bool $hide_url; + private ?string $description; + private ?string $thumbnail_url; + private ?int $thumbnail_width; + private ?int $thumbnail_height; /** * Title of the result @@ -144,9 +48,9 @@ /** * Content of the message to be sent * - * @return InputContactMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -155,10 +59,10 @@ * Sets the value of the 'input_message_content' field. * Content of the message to be sent * - * @param InputContactMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|null $input_message_content): InlineQueryResultArticle + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultArticle { $this->input_message_content = $input_message_content; return $this; @@ -336,18 +240,16 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'title' => $this->title, - 'input_message_content' => ($this->input_message_content instanceof ObjectTypeInterface) ? $this->input_message_content->toArray() : null, - 'reply_markup' => ($this->reply_markup instanceof ObjectTypeInterface) ? $this->reply_markup->toArray() : null, + 'input_message_content' => $this->input_message_content?->toArray(), + 'reply_markup' => $this->reply_markup?->toArray(), 'url' => $this->url, 'hide_url' => $this->hide_url, 'description' => $this->description, @@ -358,20 +260,16 @@ } /** - * Constructs the object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultArticle { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::ARTICLE; $object->id = $data['id'] ?? null; $object->title = $data['title'] ?? null; - $object->input_message_content = InputMessageContent::fromArray($data['input_message_content'] ?? []); - $object->reply_markup = InlineKeyboardMarkup::fromArray($data['reply_markup'] ?? []); + $object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null; + $object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; $object->url = $data['url'] ?? null; $object->hide_url = $data['hide_url'] ?? null; $object->description = $data['description'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultAudio.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultAudio.php similarity index 67% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultAudio.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultAudio.php index 194d5f5..c668023 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultAudio.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultAudio.php @@ -1,123 +1,30 @@ type = 'audio'; - } - - /** - * Type of the result, must be audio - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of 'id' property - * Unique identifier for this result, 1-64 bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultAudio - { - if(!Validate::length($id, 1, 64)) - { - throw new InvalidArgumentException(sprintf('id must be between 1 and 64 characters long, got %s characters', $id)); - } - - $this->id = $id; - return $this; - } + private ?array $caption_entities; + private ?string $performer; + private ?int $audio_duration; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; /** * A valid URL for the audio file @@ -362,9 +269,9 @@ /** * Optional. Content of the message to be sent instead of the audio * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -373,32 +280,28 @@ * Sets the value of 'input_message_content' property * Optional. Content of the message to be sent instead of the audio * - * @param InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): InlineQueryResultAudio + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultAudio { $this->input_message_content = $input_message_content; return $this; } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'audio_url' => $this->audio_url, 'title' => $this->title, 'caption' => $this->caption, 'parse_mode' => $this->parse_mode, - 'caption_entities' => ($this->caption_entities) ? array_map(static function (MessageEntity $messageEntity) { - return $messageEntity->toArray(); - }, $this->caption_entities) : null, + 'caption_entities' => array_map(fn(MessageEntity $messageEntity) => $messageEntity->toArray(), $this->caption_entities), 'performer' => $this->performer, 'audio_duration' => $this->audio_duration, 'reply_markup' => ($this->reply_markup) ? $this->reply_markup->toArray() : null, @@ -407,24 +310,18 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultAudio { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::AUDIO; $object->id = $data['id'] ?? null; $object->audio_url = $data['audio_url'] ?? null; $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = ($data['caption_entities']) ? array_map(static function (array $messageEntity) { - return MessageEntity::fromArray($messageEntity); - }, $data['caption_entities']) : null; + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $messageEntity) => MessageEntity::fromArray($messageEntity), $data['caption_entities']) : null; $object->performer = $data['performer'] ?? null; $object->audio_duration = $data['audio_duration'] ?? null; $object->reply_markup = ($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultContact.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultContact.php similarity index 64% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultContact.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultContact.php index f2cc816..7cefc2a 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultContact.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultContact.php @@ -3,121 +3,27 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; use TgBotLib\Classes\Validate; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; use TgBotLib\Objects\InputMessageContent; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; - use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; - class InlineQueryResultContact implements ObjectTypeInterface + class InlineQueryResultContact extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $phone_number; - - /** - * @var string - */ - private $first_name; - - /** - * @var string|null - */ - private $last_name; - - /** - * @var string|null - */ - private $vcard; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * @var string|null - */ - private $thumbnail_url; - - /** - * @var int|null - */ - private $thumbnail_width; - - /** - * @var int|null - */ - private $thumbnail_height; - - /** - * InlineQueryResultContact constructor. - */ - public function __construct() - { - $this->type = 'contact'; - } - - /** - * Type of the result must be contact - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 Bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of 'id' property - * Unique identifier for this result, 1-64 Bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): self - { - if(!Validate::length($id, 1, 64)) - { - throw new InvalidArgumentException('id should be between 1-64 characters'); - } - - $this->id = $id; - return $this; - } + private string $phone_number; + private string $first_name; + private ?string $last_name; + private ?string $vcard; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; + private ?string $thumbnail_url; + private ?int $thumbnail_width; + private ?int $thumbnail_height; /** * Contact's phone number @@ -242,9 +148,9 @@ /** * Optional. Content of the message to be sent instead of the contact * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -253,10 +159,10 @@ * Sets the value of 'input_message_content' property * Optional. Content of the message to be sent instead of the contact * - * @param InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): self + public function setInputMessageContent(?InputMessageContent $input_message_content): self { $this->input_message_content = $input_message_content; return $this; @@ -332,21 +238,19 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'phone_number' => $this->phone_number, 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'vcard' => $this->vcard, - 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null, - 'input_message_content' => ($this->input_message_content instanceof ObjectTypeInterface) ? $this->input_message_content->toArray() : null, + 'reply_markup' => $this->reply_markup?->toArray(), + 'input_message_content' => $this->input_message_content?->toArray(), 'thumbnail_url' => $this->thumbnail_url, 'thumbnail_width' => $this->thumbnail_width, 'thumbnail_height' => $this->thumbnail_height @@ -354,15 +258,12 @@ } /** - * Constructs an object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultContact { $object = new self(); - + $object->type = InlineQueryResultType::CONTACT; $object->id = $data['id'] ?? null; $object->phone_number = $data['phone_number'] ?? null; $object->first_name = $data['first_name'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultDocument.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultDocument.php similarity index 70% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultDocument.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultDocument.php index 96bd7d4..8000e04 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultDocument.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultDocument.php @@ -3,118 +3,35 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; use TgBotLib\Classes\Validate; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultDocument implements ObjectTypeInterface + class InlineQueryResultDocument extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $title; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $title; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var string - */ - private $document_url; - - /** - * @var string - */ - private $mime_type; - - /** - * @var string|null - */ - private $description; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * @var string|null - */ - private $thumbnail_url; - - /** - * @var int|null - */ - private $thumbnail_width; - - /** - * @var int|null - */ - private $thumbnail_height; - - /** - * InlineQueryResultDocument constructor. - */ - public function __construct() - { - $this->type = 'document'; - } - - /** - * Type of the result, must be document - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } + private ?array $caption_entities; + private string $document_url; + private string $mime_type; + private ?string $description; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; + private ?string $thumbnail_url; + private ?int $thumbnail_width; + private ?int $thumbnail_height; /** * Title for the result @@ -344,9 +261,9 @@ /** * Optional. Content of the message to be sent instead of the file * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -355,10 +272,10 @@ * Sets the value of the 'input_message_content' field. * Optional. Content of the message to be sent instead of the file * - * @param InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): InlineQueryResultDocument + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultDocument { $this->input_message_content = $input_message_content; return $this; @@ -439,31 +356,22 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'title' => $this->title, 'caption' => $this->caption, 'parse_mode' => $this->parse_mode, - 'caption_entities' => (function (array $data) { - $result = []; - foreach ($data as $item) { - $result[] = $item->toArray(); - } - - return $result; - })($this->caption_entities ?? []), + 'caption_entities' => array_map(fn(MessageEntity $entity) => $entity->toArray(), $this->caption_entities), 'document_url' => $this->document_url, 'mime_type' => $this->mime_type, 'description' => $this->description, - 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null, - 'input_message_content' => ($this->input_message_content instanceof InputVenueMessageContent) ? $this->input_message_content->toArray() : null, + 'reply_markup' => $this->reply_markup?->toArray(), + 'input_message_content' => $this->input_message_content?->toArray(), 'thumbnail_url' => $this->thumbnail_url, 'thumbnail_width' => $this->thumbnail_width, 'thumbnail_height' => $this->thumbnail_height, @@ -471,33 +379,22 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultDocument { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::DOCUMENT; $object->id = $data['id'] ?? null; $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = (function (array $data) { - $result = []; - foreach ($data as $item) { - $result[] = MessageEntity::fromArray($item); - } - - return $result; - })($data['caption_entities'] ?? []); + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $entity) => MessageEntity::fromArray($entity), $data['caption_entities']) : null; $object->document_url = $data['document_url'] ?? null; $object->mime_type = $data['mime_type'] ?? null; $object->description = $data['description'] ?? null; - $object->reply_markup = ($data['reply_markup'] ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null); - $object->input_message_content = ($data['input_message_content'] ? InputVenueMessageContent::fromArray($data['input_message_content']) : null); + $object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; + $object->input_message_content = isset($data['input_message_content']) ? InputVenueMessageContent::fromArray($data['input_message_content']) : null; $object->thumbnail_url = $data['thumbnail_url'] ?? null; $object->thumbnail_width = $data['thumbnail_width'] ?? null; $object->thumbnail_height = $data['thumbnail_height'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGame.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGame.php similarity index 60% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGame.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGame.php index 8e51885..27f57b7 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGame.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGame.php @@ -3,52 +3,17 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; - class InlineQueryResultGame implements ObjectTypeInterface + class InlineQueryResultGame extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $game_short_name; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * Type of the result must be game - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } + private string $game_short_name; + private ?InlineKeyboardMarkup $reply_markup; /** * Sets the value of the 'id' field @@ -109,34 +74,28 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'game_short_name' => $this->game_short_name, - 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null + 'reply_markup' => $this->reply_markup?->toArray() ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultGame { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::GAME; $object->id = $data['id'] ?? null; $object->game_short_name = $data['game_short_name'] ?? null; - $object->reply_markup = $data['reply_markup'] ?? null; + $object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; return $object; } diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGif.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGif.php similarity index 63% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGif.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGif.php index 527bce6..41e7029 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultGif.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultGif.php @@ -3,138 +3,35 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; use TgBotLib\Classes\Validate; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Enums\Types\ThumbnailMimeType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; - use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultGif implements ObjectTypeInterface + class InlineQueryResultGif extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $gif_url; - - /** - * @var int|null - */ - private $gif_width; - - /** - * @var int|null - */ - private $gif_height; - - /** - * @var int|null - */ - private $gif_duration; - - /** - * @var string|null - */ - private $thumbnail_url; - - /** - * @var string|null - * @see ThumbnailMimeType - */ - private $thumbnail_mime_type; - - /** - * @var string|null - */ - private $title; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $gif_url; + private ?int $gif_width; + private ?int $gif_height; + private ?int $gif_duration; + private ?string $thumbnail_url; + private ?ThumbnailMimeType $thumbnail_mime_type; + private ?string $title; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * InlineQueryResultGif constructor. - */ - public function __construct() - { - $this->type = 'gif'; - } - - /** - * The Type of the result must be gif - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of the 'id' field. - * Unique identifier for this result, 1-64 bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultGif - { - if(!Validate::length($id, 1, 64)) - { - throw new InvalidArgumentException(); - } - - $this->id = $id; - return $this; - } + private ?array $caption_entities; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; /** * A valid URL for the GIF file. File size must not exceed 1MB @@ -276,9 +173,9 @@ /** * Optional. MIME type of the thumbnail must be one of “image/jpeg,” “image/gif”, or “video/mp4”. Defaults to “image/jpeg” * - * @return string|null + * @return ThumbnailMimeType|null */ - public function getThumbnailMimeType(): ?string + public function getThumbnailMimeType(): ?ThumbnailMimeType { return $this->thumbnail_mime_type; } @@ -287,10 +184,10 @@ * Sets the value of the 'thumbnail_mime_type' field. * Optional. MIME type of the thumbnail must be one of “image/jpeg,” “image/gif”, or “video/mp4”. Defaults to “image/jpeg” * - * @param string|null $thumbnail_mime_type + * @param string|ThumbnailMimeType|null $thumbnail_mime_type * @return $this */ - public function setThumbnailMimeType(?string $thumbnail_mime_type): InlineQueryResultGif + public function setThumbnailMimeType(string|ThumbnailMimeType|null $thumbnail_mime_type): InlineQueryResultGif { if(is_null($thumbnail_mime_type)) { @@ -298,6 +195,16 @@ return $this; } + if(is_string($thumbnail_mime_type)) + { + $thumbnail_mime_type = ThumbnailMimeType::tryFrom($thumbnail_mime_type); + } + + if($thumbnail_mime_type === null) + { + throw new InvalidArgumentException('Unexpected type for ThumbnailMimeType'); + } + $this->thumbnail_mime_type = $thumbnail_mime_type; return $this; } @@ -437,9 +344,9 @@ /** * Optional. Content of the message to be sent instead of the GIF animation * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -448,25 +355,22 @@ * Sets the value of the 'input_message_content' field. * Optional. Content of the message to be sent instead of the GIF animation * - * @param InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): InlineQueryResultGif + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultGif { $this->input_message_content = $input_message_content; return $this; } - /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type ?? null, + 'type' => $this->type->value, 'id' => $this->id ?? null, 'gif_url' => $this->gif_url ?? null, 'gif_width' => $this->gif_width ?? null, @@ -477,36 +381,19 @@ 'title' => $this->title ?? null, 'caption' => $this->caption ?? null, 'parse_mode' => $this->parse_mode ?? null, - 'caption_entities' => (static function (array $captionEntities) - { - $result = []; - foreach($captionEntities as $captionEntity) - { - $result[] = $captionEntity->toArray(); - } - return $result; - })($this->caption_entities ?? []), - 'reply_markup' => (static function (InlineKeyboardMarkup|null $replyMarkup) { - return $replyMarkup?->toArray(); - })($this->reply_markup ?? null), - 'input_message_content' => (static function (ObjectTypeInterface|null $inputMessageContent) { - return $inputMessageContent?->toArray(); - })($this->input_message_content ?? null), + 'caption_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities), + 'reply_markup' => $this->reply_markup?->toArray(), + 'input_message_content' => $this->input_message_content?->toArray() ]; } /** - * Constructs an object from an array representation - * - * @param array $data - * @return ObjectTypeInterface - * @noinspection DuplicatedCode + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultGif { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::GIF; $object->id = $data['id'] ?? null; $object->gif_url = $data['gif_url'] ?? null; $object->gif_width = $data['gif_width'] ?? null; @@ -517,37 +404,9 @@ $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - - $object->caption_entities = (static function (array $captionEntities) - { - $result = []; - foreach($captionEntities as $captionEntity) - { - $result[] = MessageEntity::fromArray($captionEntity); - } - - return $result; - })($data['caption_entities'] ?? []); - - $object->reply_markup = (static function (array|null $replyMarkup) - { - if($replyMarkup !== null) - { - return InlineKeyboardMarkup::fromArray($replyMarkup); - } - - return null; - })($data['reply_markup'] ?? null); - - $object->input_message_content = (static function (array|null $inputMessageContent) - { - if($inputMessageContent !== null) - { - return InputVenueMessageContent::fromArray($inputMessageContent); - } - - return null; - })($data['input_message_content'] ?? null); + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null; + $object->reply_markup = isset($data['reply_markup']) ? array_map(fn(array $items) => InlineKeyboardMarkup::fromArray($items), $data['reply_markup']) : null; + $object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null; return $object; } diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultLocation.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultLocation.php similarity index 70% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultLocation.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultLocation.php index a6413a4..c3b7812 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultLocation.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultLocation.php @@ -2,133 +2,30 @@ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; - use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; - class InlineQueryResultLocation implements ObjectTypeInterface + class InlineQueryResultLocation extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var float - */ - private $latitude; - - /** - * @var float - */ - private $longitude; - - /** - * @var string - */ - private $title; - - /** - * @var float|null - */ - private $horizontal_accuracy; - - /** - * @var int|null - */ - private $live_period; - - /** - * @var int|null - */ - private $heading; - - /** - * @var int|null - */ - private $proximity_alert_radius; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * @var string|null - */ - private $thumbnail_url; - - /** - * @var int|null - */ - private $thumbnail_width; - - /** - * @var int|null - */ - private $thumbnail_height; - - /** - * InlineQueryResultLocation constructor. - */ - public function __construct() - { - $this->type = 'location'; - } - - /** - * Type of the result must be location - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 Bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets a unique identifier for this result, 1-64 Bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultLocation - { - if(strlen($id) > 64) - { - throw new InvalidArgumentException('id should be less than 64 characters'); - } - - $this->id = $id; - return $this; - } + private float $latitude; + private float $longitude; + private string $title; + private ?float $horizontal_accuracy; + private ?int $live_period; + private ?int $heading; + private ?int $proximity_alert_radius; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; + private ?string $thumbnail_url; + private ?int $thumbnail_width; + private ?int $thumbnail_height; /** * Location latitude in degrees @@ -331,9 +228,9 @@ /** * Optional. Content of the message to be sent instead of the location * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -341,10 +238,10 @@ /** * Sets the content of the message to be sent instead of the location * - * @param InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(null|InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent $input_message_content): InlineQueryResultLocation + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultLocation { $this->input_message_content = $input_message_content; return $this; @@ -417,14 +314,12 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'latitude' => $this->latitude, 'longitude' => $this->longitude, @@ -442,16 +337,12 @@ } /** - * Constructs the object from an array representation. - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultLocation { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::LOCATION; $object->id = $data['id'] ?? null; $object->latitude = $data['latitude'] ?? null; $object->longitude = $data['longitude'] ?? null; @@ -460,8 +351,8 @@ $object->live_period = $data['live_period'] ?? null; $object->heading = $data['heading'] ?? null; $object->proximity_alert_radius = $data['proximity_alert_radius'] ?? null; - $object->reply_markup = ($data['reply_markup'] ?? null) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; - $object->input_message_content = ($data['input_message_content'] ?? null) ? InputContactMessageContent::fromArray($data['input_message_content']) : null; + $object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; + $object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null; $object->thumbnail_url = $data['thumbnail_url'] ?? null; $object->thumbnail_width = $data['thumbnail_width'] ?? null; $object->thumbnail_height = $data['thumbnail_height'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultMpeg4Gif.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultMpeg4Gif.php similarity index 70% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultMpeg4Gif.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultMpeg4Gif.php index 4ce74fe..84ad592 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultMpeg4Gif.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultMpeg4Gif.php @@ -3,128 +3,33 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultMpeg4Gif implements ObjectTypeInterface + class InlineQueryResultMpeg4Gif extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $mpeg4_url; - - /** - * @var int|null - */ - private $mpeg4_width; - - /** - * @var int|null - */ - private $mpeg4_height; - - /** - * @var int|null - */ - private $mpeg4_duration; - - /** - * @var string - */ - private $thumbnail_url; - - /** - * @var string|null - */ - private $thumbnail_mime_type; - - /** - * @var string|null - */ - private $title; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $mpeg4_url; + private ?int $mpeg4_width; + private ?int $mpeg4_height; + private ?int $mpeg4_duration; + private string $thumbnail_url; + private ?string $thumbnail_mime_type; + private ?string $title; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * InlineQueryResultMpeg4Gif constructor. - */ - public function __construct() - { - $this->type = 'mpeg4_gif'; - } - - /** - * Type of the result, must be mpeg4_gif - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of the 'id' field - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultMpeg4Gif - { - $this->id = $id; - return $this; - } + private ?array $caption_entities; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; /** * A valid URL for the MPEG4 file. File size must not exceed 1MB @@ -371,9 +276,9 @@ /** * Optional. Content of the message to be sent instead of the video animation * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -381,19 +286,17 @@ /** * Optional. Content of the message to be sent instead of the video animation * - * @param InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): InlineQueryResultMpeg4Gif + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultMpeg4Gif { $this->input_message_content = $input_message_content; return $this; } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -409,9 +312,7 @@ 'title' => $this->title ?? null, 'caption' => $this->caption ?? null, 'parse_mode' => $this->parse_mode ?? null, - 'caption_entities' => ($this->caption_entities ?? null) ? array_map(static function (MessageEntity $item) { - return $item->toArray(); - }, $this->caption_entities) : null, + 'caption_entities' => is_null($this->caption_entities) ? array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities) : null, 'reply_markup' => ($this->reply_markup ?? null) ? $this->reply_markup->toArray() : null, 'input_message_content' => ($this->input_message_content ?? null) ? $this->input_message_content->toArray() : null, ]; @@ -419,30 +320,23 @@ } /** - * Constructs an object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultMpeg4Gif { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::MPEG_4_GIF; $object->id = $data['id'] ?? null; $object->mpeg4_url = $data['mpeg4_url'] ?? null; $object->mpeg4_width = $data['mpeg4_width'] ?? null; $object->mpeg4_height = $data['mpeg4_height'] ?? null; $object->mpeg4_duration = $data['mpeg4_duration'] ?? null; - /** @noinspection DuplicatedCode */ $object->thumbnail_url = $data['thumbnail_url'] ?? null; $object->thumbnail_mime_type = $data['thumbnail_mime_type'] ?? null; $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = array_map(static function ($item) { - return MessageEntity::fromArray($item); - }, $data['caption_entities'] ?? []); + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null; $object->reply_markup = ($data['reply_markup'] ?? null) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; $object->input_message_content = ($data['input_message_content'] ?? null) ? InputVenueMessageContent::fromArray($data['input_message_content']) : null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultPhoto.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultPhoto.php similarity index 66% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultPhoto.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultPhoto.php index 8aa7675..4164dfc 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultPhoto.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultPhoto.php @@ -3,129 +3,33 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; - use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultPhoto implements ObjectTypeInterface + class InlineQueryResultPhoto extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $photo_url; - - /** - * @var string - */ - private $thumbnail_url; - - /** - * @var int|null - */ - private $photo_width; - - /** - * @var int|null - */ - private $photo_height; - - /** - * @var string|null - */ - private $title; - - /** - * @var string|null - */ - private $description; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $photo_url; + private string $thumbnail_url; + private ?int $photo_width; + private ?int $photo_height; + private ?string $title; + private ?string $description; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * InlineQueryResultPhoto constructor. - */ - public function __construct() - { - $this->type = 'photo'; - } - - /** - * Type of the result must be photo - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the value of the 'id' field - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultPhoto - { - if(mb_strlen($id) > 64) - { - throw new InvalidArgumentException('id length must not exceed 64 characters'); - } - - $this->id = $id; - return $this; - } + private ?array $caption_entities; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputTextMessageContent $input_message_content; /** * A valid URL of the photo. Photo must be in JPEG format. Photo size must not exceed 5MB @@ -356,9 +260,9 @@ /** * Optional. Content of the message to be sent instead of the photo * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -366,19 +270,17 @@ /** * Sets the value of the 'input_message_content' field * - * @param InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): InlineQueryResultPhoto + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultPhoto { $this->input_message_content = $input_message_content; return $this; } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -393,24 +295,18 @@ 'description' => $this->description ?? null, 'caption' => $this->caption ?? null, 'parse_mode' => $this->parse_mode ?? null, - 'caption_entities' => ($this->caption_entities !== null) ? array_map(static function (MessageEntity $messageEntity) { - return $messageEntity->toArray(); - }, $this->caption_entities) : null, + 'caption_entities' => isset($data['caption_entities']) ? array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities) : null, 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null, ]; } /** - * Constructs an object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultPhoto { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::PHOTO; $object->id = $data['id'] ?? null; $object->photo_url = $data['photo_url'] ?? null; $object->thumbnail_url = $data['thumbnail_url'] ?? null; @@ -420,9 +316,7 @@ $object->description = $data['description'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = ($data['caption_entities'] !== null) ? array_map(static function (array $messageEntity) { - return MessageEntity::fromArray($messageEntity); - }, $data['caption_entities']) : null; + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null; $object->reply_markup = ($data['reply_markup'] !== null) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null; $object->input_message_content = $data['input_message_content'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVenue.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVenue.php similarity index 69% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVenue.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVenue.php index d64fd0a..865bd9e 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVenue.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVenue.php @@ -2,138 +2,30 @@ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; - use InvalidArgumentException; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; + use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; - class InlineQueryResultVenue implements ObjectTypeInterface + class InlineQueryResultVenue extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var float - */ - private $latitude; - - /** - * @var float - */ - private $longitude; - - /** - * @var string - */ - private $title; - - /** - * @var string - */ - private $address; - - /** - * @var string|null - */ - private $foursquare_id; - - /** - * @var string|null - */ - private $foursquare_type; - - /** - * @var string|null - */ - private $google_place_id; - - /** - * @var string|null - */ - private $google_place_type; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * @var string|null - */ - private $thumbnail_url; - - /** - * @var int|null - */ - private $thumbnail_width; - - /** - * @var int|null - */ - private $thumbnail_height; - - /** - * InlineQueryResultVenue constructor. - */ - public function __construct() - { - $this->type = 'venue'; - } - - /** - * Type of the result, must be venue - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 Bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets id. - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultVenue - { - if(strlen($id) > 64) - { - throw new InvalidArgumentException('id must be between 1 and 64 characters'); - } - - $this->id = $id; - return $this; - } + private float $latitude; + private float $longitude; + private string $title; + private string $address; + private ?string $foursquare_id; + private ?string $foursquare_type; + private ?string $google_place_id; + private ?string $google_place_type; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; + private ?string $thumbnail_url; + private ?int $thumbnail_width; + private ?int $thumbnail_height; /** * Latitude of the venue location in degrees @@ -338,9 +230,9 @@ /** * Optional. Content of the message to be sent instead of the venue * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -348,10 +240,10 @@ /** * Sets the content of the message to be sent instead of the venue * - * @param InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(null|InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent $input_message_content): InlineQueryResultVenue + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultVenue { $this->input_message_content = $input_message_content; return $this; @@ -424,14 +316,12 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'latitude' => $this->latitude, 'longitude' => $this->longitude, @@ -441,8 +331,8 @@ 'foursquare_type' => $this->foursquare_type, 'google_place_id' => $this->google_place_id, 'google_place_type' => $this->google_place_type, - 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null, - 'input_message_content' => ($this->input_message_content instanceof InputVenueMessageContent) ? $this->input_message_content->toArray() : null, + 'reply_markup' => $this->reply_markup?->toArray(), + 'input_message_content' => $this->input_message_content?->toArray(), 'thumbnail_url' => $this->thumbnail_url, 'thumbnail_width' => $this->thumbnail_width, 'thumbnail_height' => $this->thumbnail_height @@ -450,16 +340,12 @@ } /** - * Constructs an object from an array representation - * - * @param array $data - * @return ObjectTypeInterface - * @noinspection DuplicatedCode + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultVenue { $object = new self(); - + $object->type = InlineQueryResultType::VENUE; $object->id = $data['id'] ?? null; $object->latitude = $data['latitude'] ?? null; $object->longitude = $data['longitude'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVideo.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVideo.php similarity index 71% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVideo.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVideo.php index 6292a03..70c3a90 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVideo.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVideo.php @@ -3,140 +3,34 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; use TgBotLib\Objects\InputMessageContent; - use TgBotLib\Objects\InputMessageContent\InputContactMessageContent; - use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent; - use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent; - use TgBotLib\Objects\InputMessageContent\InputTextMessageContent; - use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultVideo implements ObjectTypeInterface + class InlineQueryResultVideo extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $video_url; - - /** - * @var string - */ - private $mime_type; - - /** - * @var string - */ - private $thumbnail_url; - - /** - * @var string - */ - private $title; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $video_url; + private string $mime_type; + private string $thumbnail_url; + private string $title; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var int|null - */ - private $video_width; - - /** - * @var int|null - */ - private $video_height; - - /** - * @var int|null - */ - private $video_duration; - - /** - * @var string|null - */ - private $description; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null - */ - private $input_message_content; - - /** - * InlineQueryResultVideo constructor. - */ - public function __construct() - { - $this->type = 'video'; - } - - /** - * Type of the result, must be video - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the unique identifier for this result, 1-64 bytes - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultVideo - { - if(strlen($id) > 64) - { - throw new InvalidArgumentException('ID must be between 1 and 64 characters'); - } - - $this->id = $id; - return $this; - } + private ?array $caption_entities; + private ?int $video_width; + private ?int $video_height; + private ?int $video_duration; + private ?string $description; + private ?InlineKeyboardMarkup $reply_markup; + private ?InputMessageContent $input_message_content; /** * A valid URL for the embedded video player or video file @@ -416,9 +310,9 @@ * Optional. Content of the message to be sent instead of the video. This field is required if * InlineQueryResultVideo is used to send an HTML-page as a result (e.g., a YouTube video). * - * @return InputContactMessageContent|InputInvoiceMessageContent|InputLocationMessageContent|InputTextMessageContent|InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -426,19 +320,17 @@ /** * Sets the content of the message to be sent instead of the video. This field is required if * - * @param InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(InputVenueMessageContent|InputTextMessageContent|InputContactMessageContent|InputLocationMessageContent|InputInvoiceMessageContent|null $input_message_content): static + public function setInputMessageContent(?InputMessageContent $input_message_content): static { $this->input_message_content = $input_message_content; return $this; } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { @@ -451,9 +343,7 @@ 'title' => $this->title, 'caption' => $this->caption, 'parse_mode' => $this->parse_mode, - 'caption_entities' => ($this->caption_entities ?? null) ? array_map(static function (MessageEntity $item) { - return $item->toArray(); - }, $this->caption_entities) : null, + 'caption_entities' => is_null($this->caption_entities) ? null : array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities), 'video_width' => $this->video_width, 'video_height' => $this->video_height, 'video_duration' => $this->video_duration, @@ -464,16 +354,12 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface + * @inheritDoc */ public static function fromArray(array $data): ObjectTypeInterface { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::VIDEO; $object->id = $data['id'] ?? null; $object->video_url = $data['video_url'] ?? null; $object->mime_type = $data['mime_type'] ?? null; @@ -481,9 +367,7 @@ $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = isset($data['caption_entities']) ? array_map(static function ($item) { - return MessageEntity::fromArray($item); - }, $data['caption_entities']) : null; + $object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null; $object->video_width = $data['video_width'] ?? null; $object->video_height = $data['video_height'] ?? null; $object->video_duration = $data['video_duration'] ?? null; diff --git a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVoice.php b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVoice.php similarity index 57% rename from src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVoice.php rename to src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVoice.php index 8630cfe..d5f5647 100644 --- a/src/TgBotLib/Objects/InlineQueryResult/InlineQueryResultVoice.php +++ b/src/TgBotLib/Objects/Inline/InlineQueryResult/InlineQueryResultVoice.php @@ -3,111 +3,30 @@ /** @noinspection PhpUnused */ /** @noinspection PhpMissingFieldTypeInspection */ - namespace TgBotLib\Objects\InlineQueryResult; + namespace TgBotLib\Objects\Inline\InlineQueryResult; use InvalidArgumentException; + use TgBotLib\Enums\Types\InlineQueryResultType; use TgBotLib\Interfaces\ObjectTypeInterface; - use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineKeyboardMarkup; + use TgBotLib\Objects\Inline\InlineQueryResult; use TgBotLib\Objects\InputMessageContent; use TgBotLib\Objects\MessageEntity; - class InlineQueryResultVoice implements ObjectTypeInterface + class InlineQueryResultVoice extends InlineQueryResult implements ObjectTypeInterface { - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $id; - - /** - * @var string - */ - private $voice_url; - - /** - * @var string - */ - private $title; - - /** - * @var string|null - */ - private $caption; - - /** - * @var string|null - */ - private $parse_mode; - + private string $voice_url; + private string $title; + private ?string $caption; + private ?string $parse_mode; /** * @var MessageEntity[]|null */ - private $caption_entities; - - /** - * @var int|null - */ - private $voice_duration; - - /** - * @var InlineKeyboardMarkup|null - */ - private $reply_markup; - - /** - * @var \TgBotLib\Objects\InputMessageContent\InputContactMessageContent|\TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent|\TgBotLib\Objects\InputMessageContent\InputLocationMessageContent|\TgBotLib\Objects\InputMessageContent\InputTextMessageContent|\TgBotLib\Objects\InputMessageContent\InputVenueMessageContent|null - */ + private ?array $caption_entities; + private ?int $voice_duration; + private ?InlineKeyboardMarkup $reply_markup; private $input_message_content; - /** - * InlineQueryResultVoice constructor. - */ - public function __construct() - { - $this->type = 'voice'; - } - - /** - * The Type of the result must be voice - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Unique identifier for this result, 1-64 bytes - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - /** - * Sets the id of the result. - * - * @param string $id - * @return $this - */ - public function setId(string $id): InlineQueryResultVoice - { - if(strlen($id) > 64) - { - throw new InvalidArgumentException('id must be between 1 and 64 characters'); - } - - $this->id = $id; - return $this; - } - /** * A valid URL for the voice recording * @@ -272,9 +191,9 @@ /** * Optional. Content of the message to be sent instead of the voice recording * - * @return \TgBotLib\Objects\InputMessageContent\InputContactMessageContent|\TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent|\TgBotLib\Objects\InputMessageContent\InputLocationMessageContent|\TgBotLib\Objects\InputMessageContent\InputTextMessageContent|\TgBotLib\Objects\InputMessageContent\InputVenueMessageContent|null + * @return InputMessageContent|null */ - public function getInputMessageContent(): \TgBotLib\Objects\InputMessageContent\InputContactMessageContent|\TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent|\TgBotLib\Objects\InputMessageContent\InputLocationMessageContent|\TgBotLib\Objects\InputMessageContent\InputTextMessageContent|\TgBotLib\Objects\InputMessageContent\InputVenueMessageContent|null + public function getInputMessageContent(): ?InputMessageContent { return $this->input_message_content; } @@ -282,36 +201,28 @@ /** * Sets the input_message_content of the result. * - * @param \TgBotLib\Objects\InputMessageContent\InputContactMessageContent|\TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent|\TgBotLib\Objects\InputMessageContent\InputLocationMessageContent|\TgBotLib\Objects\InputMessageContent\InputTextMessageContent|\TgBotLib\Objects\InputMessageContent\InputVenueMessageContent $input_message_content + * @param InputMessageContent|null $input_message_content * @return $this */ - public function setInputMessageContent(\TgBotLib\Objects\InputMessageContent\InputContactMessageContent|\TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent|\TgBotLib\Objects\InputMessageContent\InputLocationMessageContent|\TgBotLib\Objects\InputMessageContent\InputTextMessageContent|\TgBotLib\Objects\InputMessageContent\InputVenueMessageContent $input_message_content): InlineQueryResultVoice + public function setInputMessageContent(?InputMessageContent $input_message_content): InlineQueryResultVoice { $this->input_message_content = $input_message_content; return $this; } /** - * Returns an array representation of the object. - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'type' => $this->type, + 'type' => $this->type->value, 'id' => $this->id, 'voice_url' => $this->voice_url, 'title' => $this->title, 'caption' => $this->caption, 'parse_mode' => $this->parse_mode, - 'caption_entities' => (static function (array $data) { - $result = []; - foreach ($data as $item) { - $result[] = $item->toArray(); - } - return $result; - })($this->caption_entities ?? null), + 'caption_entities' => is_null($this->caption_entities) ? null : array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities), 'voice_duration' => $this->voice_duration, 'reply_markup' => ($this->reply_markup instanceof InlineKeyboardMarkup) ? $this->reply_markup->toArray() : null, 'input_message_content' => ($this->input_message_content instanceof ObjectTypeInterface) ? $this->input_message_content->toArray() : null, @@ -319,30 +230,18 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ObjectTypeInterface - * @noinspection DuplicatedCode + * @inheritDoc */ - public static function fromArray(array $data): ObjectTypeInterface + public static function fromArray(array $data): InlineQueryResultVoice { $object = new self(); - - $object->type = $data['type'] ?? null; + $object->type = InlineQueryResultType::VOICE; $object->id = $data['id'] ?? null; $object->voice_url = $data['voice_url'] ?? null; $object->title = $data['title'] ?? null; $object->caption = $data['caption'] ?? null; $object->parse_mode = $data['parse_mode'] ?? null; - $object->caption_entities = (static function (array $data) { - $result = []; - foreach ($data as $item) - { - $result[] = MessageEntity::fromArray($item); - } - return $result; - })($data['caption_entities'] ?? []); + $object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']); $object->voice_duration = $data['voice_duration'] ?? null; $object->reply_markup = InlineKeyboardMarkup::fromArray($data['reply_markup'] ?? []); $object->input_message_content = InputMessageContent::fromArray($data['input_message_content'] ?? []); diff --git a/src/TgBotLib/Objects/InlineQueryResult.php b/src/TgBotLib/Objects/InlineQueryResult.php deleted file mode 100644 index d8799fc..0000000 --- a/src/TgBotLib/Objects/InlineQueryResult.php +++ /dev/null @@ -1,67 +0,0 @@ - InlineQueryResultArticle::fromArray($data), - InlineQueryResultType::PHOTO => InlineQueryResultPhoto::fromArray($data), - InlineQueryResultType::GIF => InlineQueryResultGif::fromArray($data), - InlineQueryResultType::MPEG_4_GIF => InlineQueryResultMpeg4Gif::fromArray($data), - InlineQueryResultType::VIDEO => InlineQueryResultVideo::fromArray($data), - InlineQueryResultType::AUDIO => InlineQueryResultAudio::fromArray($data), - InlineQueryResultType::VOICE => InlineQueryResultVoice::fromArray($data), - InlineQueryResultType::DOCUMENT => InlineQueryResultDocument::fromArray($data), - InlineQueryResultType::LOCATION => InlineQueryResultLocation::fromArray($data), - InlineQueryResultType::VENUE => InlineQueryResultVenue::fromArray($data), - InlineQueryResultType::CONTACT => InlineQueryResultContact::fromArray($data), - InlineQueryResultType::GAME => InlineQueryResultGame::fromArray($data), - default => throw new InvalidArgumentException(sprintf('The type of the InlineQueryResult is invalid, got "%s", expected one of "%s"', $data['type'], implode('", "', InlineQueryResultType::ALL))), - }; - } - } \ No newline at end of file