From f6fb481d0d41360b695dfac2e851b3bfc5213530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sun, 28 Aug 2016 00:25:44 +0200 Subject: [PATCH 01/31] Initial ugly commit of the refactored Entities. --- src/Entities/Audio.php | 127 +-- src/Entities/CallbackQuery.php | 121 +-- src/Entities/Chat.php | 198 +--- src/Entities/ChatMember.php | 60 +- src/Entities/ChosenInlineResult.php | 117 +-- src/Entities/Contact.php | 92 +- src/Entities/Document.php | 111 +-- ...utMessageContent.php => EditedMessage.php} | 5 +- src/Entities/Entity.php | 145 ++- src/Entities/File.php | 72 +- src/Entities/InlineKeyboardButton.php | 45 +- src/Entities/InlineKeyboardMarkup.php | 5 + src/Entities/InlineQuery.php | 117 +-- src/Entities/InlineQuery/InlineEntity.php | 36 + .../InlineQuery/InlineQueryResultArticle.php | 73 ++ .../InlineQuery/InlineQueryResultAudio.php | 64 ++ .../InlineQueryResultCachedAudio.php | 55 ++ .../InlineQueryResultCachedDocument.php | 64 ++ .../InlineQueryResultCachedGif.php | 61 ++ .../InlineQueryResultCachedMpeg4Gif.php | 61 ++ .../InlineQueryResultCachedPhoto.php | 27 + .../InlineQueryResultCachedSticker.php | 27 + .../InlineQueryResultCachedVideo.php | 27 + .../InlineQueryResultCachedVoice.php | 27 + .../InlineQuery/InlineQueryResultContact.php | 27 + .../InlineQuery/InlineQueryResultDocument.php | 27 + .../InlineQuery/InlineQueryResultGif.php | 27 + .../InlineQuery/InlineQueryResultLocation.php | 27 + .../InlineQuery/InlineQueryResultMpeg4Gif.php | 27 + .../InlineQuery/InlineQueryResultPhoto.php | 27 + .../InlineQuery/InlineQueryResultVenue.php | 27 + .../InlineQuery/InlineQueryResultVideo.php | 27 + .../InlineQuery/InlineQueryResultVoice.php | 27 + src/Entities/InlineQueryResult.php | 94 -- src/Entities/InlineQueryResultArticle.php | 166 ---- src/Entities/InlineQueryResultAudio.php | 102 --- src/Entities/InlineQueryResultCachedAudio.php | 49 - .../InlineQueryResultCachedDocument.php | 102 --- src/Entities/InlineQueryResultCachedGif.php | 87 -- .../InlineQueryResultCachedMpeg4Gif.php | 82 -- src/Entities/InlineQueryResultCachedPhoto.php | 98 -- .../InlineQueryResultCachedSticker.php | 49 - src/Entities/InlineQueryResultCachedVideo.php | 102 --- src/Entities/InlineQueryResultCachedVoice.php | 102 --- src/Entities/InlineQueryResultContact.php | 135 --- src/Entities/InlineQueryResultDocument.php | 171 ---- src/Entities/InlineQueryResultGif.php | 135 --- src/Entities/InlineQueryResultLocation.php | 138 --- src/Entities/InlineQueryResultMpeg4Gif.php | 135 --- src/Entities/InlineQueryResultPhoto.php | 151 ---- src/Entities/InlineQueryResultVenue.php | 175 ---- src/Entities/InlineQueryResultVideo.php | 184 ---- src/Entities/InlineQueryResultVoice.php | 86 -- src/Entities/InputContactMessageContent.php | 52 -- src/Entities/InputLocationMessageContent.php | 45 - .../InputContactMessageContent.php | 39 + .../InputLocationMessageContent.php | 35 + .../InputMessageContent.php | 8 + .../InputTextMessageContent.php | 39 + .../InputVenueMessageContent.php | 45 + src/Entities/InputTextMessageContent.php | 48 - src/Entities/InputVenueMessageContent.php | 72 -- src/Entities/Location.php | 57 +- src/Entities/Message.php | 843 +++--------------- src/Entities/MessageEntity.php | 117 +-- src/Entities/PhotoSize.php | 97 +- src/Entities/ReplyToMessage.php | 2 +- src/Entities/Sticker.php | 139 +-- src/Entities/Update.php | 200 +---- src/Entities/User.php | 105 +-- src/Entities/UserProfilePhotos.php | 77 +- src/Entities/Venue.php | 140 +-- src/Entities/Video.php | 157 +--- src/Entities/Voice.php | 92 +- 74 files changed, 1579 insertions(+), 5153 deletions(-) rename src/Entities/{InputMessageContent.php => EditedMessage.php} (83%) create mode 100644 src/Entities/InlineQuery/InlineEntity.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultArticle.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultAudio.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedAudio.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedDocument.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedGif.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedSticker.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedVideo.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultCachedVoice.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultContact.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultDocument.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultGif.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultLocation.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultPhoto.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultVenue.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultVideo.php create mode 100644 src/Entities/InlineQuery/InlineQueryResultVoice.php delete mode 100644 src/Entities/InlineQueryResult.php delete mode 100644 src/Entities/InlineQueryResultArticle.php delete mode 100644 src/Entities/InlineQueryResultAudio.php delete mode 100644 src/Entities/InlineQueryResultCachedAudio.php delete mode 100644 src/Entities/InlineQueryResultCachedDocument.php delete mode 100644 src/Entities/InlineQueryResultCachedGif.php delete mode 100644 src/Entities/InlineQueryResultCachedMpeg4Gif.php delete mode 100644 src/Entities/InlineQueryResultCachedPhoto.php delete mode 100644 src/Entities/InlineQueryResultCachedSticker.php delete mode 100644 src/Entities/InlineQueryResultCachedVideo.php delete mode 100644 src/Entities/InlineQueryResultCachedVoice.php delete mode 100644 src/Entities/InlineQueryResultContact.php delete mode 100644 src/Entities/InlineQueryResultDocument.php delete mode 100644 src/Entities/InlineQueryResultGif.php delete mode 100644 src/Entities/InlineQueryResultLocation.php delete mode 100644 src/Entities/InlineQueryResultMpeg4Gif.php delete mode 100644 src/Entities/InlineQueryResultPhoto.php delete mode 100644 src/Entities/InlineQueryResultVenue.php delete mode 100644 src/Entities/InlineQueryResultVideo.php delete mode 100644 src/Entities/InlineQueryResultVoice.php delete mode 100644 src/Entities/InputContactMessageContent.php delete mode 100644 src/Entities/InputLocationMessageContent.php create mode 100644 src/Entities/InputMessageContent/InputContactMessageContent.php create mode 100644 src/Entities/InputMessageContent/InputLocationMessageContent.php create mode 100644 src/Entities/InputMessageContent/InputMessageContent.php create mode 100644 src/Entities/InputMessageContent/InputTextMessageContent.php create mode 100644 src/Entities/InputMessageContent/InputVenueMessageContent.php delete mode 100644 src/Entities/InputTextMessageContent.php delete mode 100644 src/Entities/InputVenueMessageContent.php diff --git a/src/Entities/Audio.php b/src/Entities/Audio.php index 75aea53a5..ac1f650ca 100644 --- a/src/Entities/Audio.php +++ b/src/Entities/Audio.php @@ -10,122 +10,19 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Audio + * + * @link https://core.telegram.org/bots/api#audio + * + * @method string getFileId() Unique identifier for this file + * @method int getDuration() Duration of the audio in seconds as defined by sender + * @method string getPerformer() Optional. Performer of the audio as defined by sender or by audio tags + * @method string getTitle() Optional. Title of the audio as defined by sender or by audio tags + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Audio extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var mixed|null - */ - protected $performer; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Audio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - - $this->performer = isset($data['performer']) ? $data['performer'] : null; - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get performer - * - * @return mixed|null - */ - public function getPerformer() - { - return $this->performer; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } diff --git a/src/Entities/CallbackQuery.php b/src/Entities/CallbackQuery.php index 1222215d2..e90815025 100644 --- a/src/Entities/CallbackQuery.php +++ b/src/Entities/CallbackQuery.php @@ -10,114 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class CallbackQuery. + * + * @link https://core.telegram.org/bots/api#callbackquery + * + * @method string getId() Unique identifier for this query + * @method User getFrom() Sender + * @method Message getMessage() Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old + * @method string getInlineMessageId() Optional. Identifier of the message sent via the bot in inline mode, that originated the query + * @method string getData() Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field + */ class CallbackQuery extends Entity { /** - * @var mixed|null - */ - protected $id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - protected $message; - - /** - * @var mixed|null - */ - protected $inline_message_id; - - /** - * @var mixed|null - */ - protected $data; - - /** - * CallbackQuery constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->message = isset($data['message']) ? $data['message'] : null; - if (!empty($this->message)) { - $this->message = new Message($this->message, $this->getBotName()); - } - - $this->inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null; - - $this->data = isset($data['data']) ? $data['data'] : null; - if (empty($this->data)) { - throw new TelegramException('data is empty!'); - } - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getMessage() - { - return $this->message; - } - - /** - * Get inline message id - * - * @return mixed|null - */ - public function getInlineMessageId() - { - return $this->inline_message_id; - } - - /** - * Get data - * - * @return mixed|null + * {@inheritdoc} */ - public function getData() + public function subEntities() { - return $this->data; + return [ + 'from' => User::class, + 'message' => Message::class, + ]; } } diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index b69bb7a37..9bbf58880 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -10,198 +10,98 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Chat + * + * @link https://core.telegram.org/bots/api#chat + * + * @property int $id Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @property string $type Type of chat, can be either "private", "group", "supergroup" or "channel" + * @property string $title Optional. Title, for channels and group chats + * @property string $username Optional. Username, for private chats, supergroups and channels if available + * @property string $first_name Optional. First name of the other party in a private chat + * @property string $last_name Optional. Last name of the other party in a private chat + * @method int getId() Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method string getType() Type of chat, can be either "private ", "group", "supergroup" or "channel" + * @method string getTitle() Optional. Title, for channels and group chats + * @method string getUsername() Optional. Username, for private chats, supergroups and channels if available + * @method string getFirstName() Optional. First name of the other party in a private chat + * @method string getLastName() Optional. Last name of the other party in a private chat + */ class Chat extends Entity { - /** - * @var mixed|null - */ - protected $id; - /** - * @var null - */ - protected $type; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $username; - - /** - * @var mixed|null - */ - protected $first_name; + public function __construct($data) + { + parent::__construct($data); - /** - * @var mixed|null - */ - protected $last_name; + if (!$this->getType()) { + if ($this->getId() > 0) { + $this->type = 'private'; + } elseif ($this->getId() < 0) { + $this->type = 'group'; + } + } + } /** - * Chat constructor. + * Try mention * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * @return string|null */ - public function __construct(array $data) + public function tryMention() { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } + if ($this->isPrivateChat()) { + if ($this->username === null) { + if ($this->last_name !== null) { + return $this->first_name . ' ' . $this->last_name; + } - if (isset($data['type'])) { - $this->type = $data['type']; - } else { - if ($this->id > 0) { - $this->type = 'private'; - } elseif ($this->id < 0) { - $this->type = 'group'; - } else { - $this->type = null; + return $this->first_name; } + + return '@' . $this->username; } - $this->title = isset($data['title']) ? $data['title'] : null; - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->username = isset($data['username']) ? $data['username'] : null; + return $this->getTitle(); } /** - * Check if is group chat + * Check if this is a group chat * * @return bool */ public function isGroupChat() { - if ($this->type == 'group' || $this->id < 0) { - return true; - } - return false; + return $this->type === 'group' || $this->id < 0; } /** - * Check if is private chat + * Check if this is a private chat * * @return bool */ public function isPrivateChat() { - if ($this->type == 'private') { - return true; - } - return false; + return $this->type === 'private'; } /** - * Check if is super group + * Check if this is a super group * * @return bool */ public function isSuperGroup() { - if ($this->type == 'supergroup') { - return true; - } - return false; + return $this->type === 'supergroup'; } /** - * Check if is channel + * Check if this is a channel * * @return bool */ public function isChannel() { - if ($this->type == 'channel') { - return true; - } - return false; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get type - * - * @return null - */ - public function getType() - { - return $this->type; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get username - * - * @return mixed|null - */ - public function getUsername() - { - return $this->username; - } - - /** - * Try mention - * - * @return mixed|null|string - */ - public function tryMention() - { - if ($this->isPrivateChat()) { - if (is_null($this->username)) { - if (!is_null($this->last_name)) { - return $this->first_name . ' ' . $this->last_name; - } - return $this->first_name; - } - return '@' . $this->username; - } - return $this->getTitle(); + return $this->type === 'channel'; } } diff --git a/src/Entities/ChatMember.php b/src/Entities/ChatMember.php index 6211afdb2..26493334e 100644 --- a/src/Entities/ChatMember.php +++ b/src/Entities/ChatMember.php @@ -10,57 +10,23 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class ChatMember + * + * @link https://core.telegram.org/bots/api#chatmember + * + * @method User getUser() Information about the user. + * @method string getStatus() The member's status in the chat. Can be "creator", "administrator", "member", "left" or "kicked" + */ class ChatMember extends Entity { /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $user; - - /** - * @var mixed|null - */ - protected $status; - - /** - * ChatMember constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->user = isset($data['user']) ? $data['user'] : null; - if (empty($this->user)) { - throw new TelegramException('user is empty!'); - } - $this->user = new User($data['user']); - - $this->status = isset($data['status']) ? $data['status'] : null; - if ($this->status === '') { - throw new TelegramException('status is empty!'); - } - } - - /** - * Get user - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getUser() - { - return $this->user; - } - - /** - * Get status - * - * @return mixed|null + * {@inheritdoc} */ - public function getStatus() + public function subEntities() { - return $this->status; + return [ + 'user' => User::class, + ]; } } diff --git a/src/Entities/ChosenInlineResult.php b/src/Entities/ChosenInlineResult.php index 89756fe4b..6a1a199fd 100644 --- a/src/Entities/ChosenInlineResult.php +++ b/src/Entities/ChosenInlineResult.php @@ -10,110 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class ChosenInlineResult + * + * @link https://core.telegram.org/bots/api#choseninlineresult + * + * @method string getResultId() The unique identifier for the result that was chosen + * @method User getFrom() The user that chose the result + * @method Location getLocation() Optional. Sender location, only for bots that require user location + * @method string getInlineMessageId() Optional. Identifier of the sent inline message. Available only if there is an inline keyboard attached to the message. Will be also received in callback queries and can be used to edit the message. + * @method string getQuery() The query that was used to obtain the result + */ class ChosenInlineResult extends Entity { /** - * @var mixed|null - */ - protected $result_id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $inline_message_id; - - /** - * @var mixed|null - */ - protected $query; - - /** - * ChosenInlineResult constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->result_id = isset($data['result_id']) ? $data['result_id'] : null; - if (empty($this->result_id)) { - throw new TelegramException('result_id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - } - - $this->inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null; - $this->query = isset($data['query']) ? $data['query'] : null; - } - - /** - * Ger result id - * - * @return mixed|null - */ - public function getResultId() - { - return $this->result_id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get inline message id - * - * @return mixed|null - */ - public function getInlineMessageId() - { - return $this->inline_message_id; - } - - /** - * Get query - * - * @return mixed|null + * {@inheritdoc} */ - public function getQuery() + protected function subEntities() { - return $this->query; + return [ + 'from' => User::class, + 'location' => Location::class, + ]; } } diff --git a/src/Entities/Contact.php b/src/Entities/Contact.php index ab74d598f..62d064421 100644 --- a/src/Entities/Contact.php +++ b/src/Entities/Contact.php @@ -10,89 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Contact + * + * @link https://core.telegram.org/bots/api#contact + * + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * @method int getUserId() Optional. Contact's user identifier in Telegram + */ class Contact extends Entity { - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $user_id; - - /** - * Contact constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->user_id = isset($data['user_id']) ? $data['user_id'] : null; - } - - /** - * Get phone number - * - * @return mixed|null - */ - public function getPhoneNumber() - { - return $this->phone_number; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - /** - * Get user id - * - * @return mixed|null - */ - public function getUserId() - { - return $this->user_id; - } } diff --git a/src/Entities/Document.php b/src/Entities/Document.php index c3c8bac4a..e532ee4b0 100644 --- a/src/Entities/Document.php +++ b/src/Entities/Document.php @@ -10,105 +10,26 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Document + * + * @link https://core.telegram.org/bots/api#document + * + * @method string getFileId() Unique file identifier + * @method PhotoSize getThumb() Optional. Document thumbnail as defined by sender + * @method string getFileName() Optional. Original filename as defined by sender + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Document extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $file_name; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Document constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (!empty($this->thumb)) { - $this->thumb = new PhotoSize($this->thumb); - } - - $this->file_name = isset($data['file_name']) ? $data['file_name'] : null; - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get file name - * - * @return mixed|null - */ - public function getFileName() - { - return $this->file_name; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/InputMessageContent.php b/src/Entities/EditedMessage.php similarity index 83% rename from src/Entities/InputMessageContent.php rename to src/Entities/EditedMessage.php index e4140a362..ba8c5af13 100644 --- a/src/Entities/InputMessageContent.php +++ b/src/Entities/EditedMessage.php @@ -10,7 +10,10 @@ namespace Longman\TelegramBot\Entities; -class InputMessageContent extends Entity +/** + * {@inheritdoc} + */ +class EditedMessage extends Message { } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 3f6c95191..c00b7b240 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -10,8 +10,14 @@ namespace Longman\TelegramBot\Entities; +use Exception; use ReflectionObject; +/** + * Class Entity + * + * This is the base class for all entities. + */ class Entity { /** @@ -29,6 +35,23 @@ public function getBotName() return $this->bot_name; } + /** + * Entity constructor. + * + * @todo Get rid of the $bot_name, it shouldn't be here! + * + * @param $data + * @param string $bot_name + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct($data, $bot_name = '') + { + $this->assignMemberVariables($data); + $this->validate(); + $this->bot_name = $bot_name; + } + /** * Perform to json * @@ -36,21 +59,19 @@ public function getBotName() */ public function toJson() { - $fields = $this->reflect($this); - $json = json_encode($fields); - - return $json; + return json_encode($this->reflect($this)); } /** * Reflect * * @param null $object + * * @return array */ public function reflect($object = null) { - if ($object == null) { + if ($object === null) { $object = $this; } @@ -61,7 +82,7 @@ public function reflect($object = null) foreach ($properties as $property) { $name = $property->getName(); - if ($name == 'bot_name') { + if ($name === 'bot_name') { continue; } @@ -107,13 +128,14 @@ public function reflect($object = null) } else { $property->setAccessible(true); $value = $property->getValue($object); - if (is_null($value)) { + if (null === $value) { continue; } $fields[$name] = $value; } } } + return $fields; } @@ -126,4 +148,113 @@ public function __toString() { return $this->toJson(); } + + /** + * Helper to set member variables + * + * @param array $data + */ + protected function assignMemberVariables(array $data) + { + foreach ($data as $key => $value) { + $this->$key = $value; + } + } + + /** + * Get the list of the properties that are themselves Entities + * + * @return array + */ + protected function subEntities() + { + return []; + } + + /** + * Perform any special entity validation + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + protected function validate() + { + } + + /** + * Get a property from the current Entity + * + * @param mixed $property + * @param mixed $default + * + * @return mixed + */ + public function getProperty($property, $default = null) + { + if (isset($this->$property)) { + return $this->$property; + } + + return $default; + } + + /** + * Return the variable for the called getter + * + * @param $method + * @param $args + * + * @return mixed|null + */ + public function __call($method, $args) + { + $action = substr($method, 0, 3); + if ($action === 'get') { + //Convert method to snake_case (which is the name of the property) + $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); + $property = $this->getProperty($property_name); + + if ($property !== null) { + //Get all sub-Entities of the current Entity + $sub_entities = $this->subEntities(); + + if (isset($sub_entities[$property_name])) { + return new $sub_entities[$property_name]($property); + } + + return $property; + } + } + + return null; + } + + /** + * Return an array of nice objects from an array of object arrays + * + * This method is used to generate pretty object arrays + * mainly for PhotoSize and Entities object arrays. + * + * @param string $class + * @param string $property + * + * @return array + */ + protected function makePrettyObjectArray($class, $property) + { + $new_objects = []; + + try { + if ($objects = $this->getProperty($property)) { + foreach ($objects as $object) { + if (!empty($object)) { + $new_objects[] = new $class($object); + } + } + } + } catch (Exception $e) { + $new_objects = []; + } + + return $new_objects; + } } diff --git a/src/Entities/File.php b/src/Entities/File.php index 24edb9fc7..0d4836806 100644 --- a/src/Entities/File.php +++ b/src/Entities/File.php @@ -10,70 +10,16 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class File + * + * @link https://core.telegram.org/bots/api#file + * + * @method string getFileId() Unique identifier for this file + * @method int getFileSize() Optional. File size, if known + * @method string getFilePath() Optional. File path. Use https://api.telegram.org/file/bot/ to get the file. + */ class File extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * @var mixed|null - */ - protected $file_path; - - /** - * File constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - - $this->file_path = isset($data['file_path']) ? $data['file_path'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } - /** - * Get file path - * - * @return mixed|null - */ - public function getFilePath() - { - return $this->file_path; - } } diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index 18a077745..ae1f0da8b 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -12,45 +12,26 @@ use Longman\TelegramBot\Exception\TelegramException; +/** + * Class InlineKeyboardButton + * + * @link https://core.telegram.org/bots/api#inlinekeyboardbutton + * + * @method string getText() Label text on the button + * @method string getUrl() Optional. HTTP url to be opened when button is pressed + * @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes + * @method string getSwitchInlineQuery() Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted. + */ class InlineKeyboardButton extends Entity { /** - * @var mixed|null - */ - protected $text; - - /** - * @var string - */ - protected $url; - - /** - * @var mixed + * {@inheritdoc} */ - protected $callback_data; - - /** - * @var mixed - */ - protected $switch_inline_query; - - /** - * InlineKeyboardButton constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data = []) + protected function validate() { - $this->text = isset($data['text']) ? $data['text'] : null; - if (empty($this->text)) { - throw new TelegramException('text is empty!'); - } - $num_params = 0; foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { - if (!empty($data[$param])) { - $this->$param = $data[$param]; + if (!empty($this->getProperty($param))) { $num_params++; } } diff --git a/src/Entities/InlineKeyboardMarkup.php b/src/Entities/InlineKeyboardMarkup.php index 8f405b23c..799ebcf9b 100644 --- a/src/Entities/InlineKeyboardMarkup.php +++ b/src/Entities/InlineKeyboardMarkup.php @@ -12,6 +12,11 @@ use Longman\TelegramBot\Exception\TelegramException; +/** + * Class InlineKeyboardMarkup + * + * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup + */ class InlineKeyboardMarkup extends Entity { /** diff --git a/src/Entities/InlineQuery.php b/src/Entities/InlineQuery.php index 52e1a2899..6794140f1 100644 --- a/src/Entities/InlineQuery.php +++ b/src/Entities/InlineQuery.php @@ -10,110 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class InlineQuery + * + * @link https://core.telegram.org/bots/api#inlinequery + * + * @method string getId() Unique identifier for this query + * @method User getFrom() Sender + * @method Location getLocation() Optional. Sender location, only for bots that request user location + * @method string getQuery() Text of the query (up to 512 characters) + * @method string getOffset() Offset of the results to be returned, can be controlled by the bot + */ class InlineQuery extends Entity { /** - * @var mixed|null - */ - protected $id; - - /** - * @var \Longman\TelegramBot\Entities\User - */ - protected $from; - - /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $query; - - /** - * @var mixed|null - */ - protected $offset; - - /** - * InlineQuery constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (empty($this->from)) { - throw new TelegramException('from is empty!'); - } - $this->from = new User($this->from); - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - } - - $this->query = isset($data['query']) ? $data['query'] : null; - $this->offset = isset($data['offset']) ? $data['offset'] : null; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get from - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get query - * - * @return mixed|null - */ - public function getQuery() - { - return $this->query; - } - - /** - * Get offset - * - * @return mixed|null + * {@inheritdoc} */ - public function getOffset() + protected function subEntities() { - return $this->offset; + return [ + 'from' => User::class, + 'location' => Location::class, + ]; } } diff --git a/src/Entities/InlineQuery/InlineEntity.php b/src/Entities/InlineQuery/InlineEntity.php new file mode 100644 index 000000000..24c03284e --- /dev/null +++ b/src/Entities/InlineQuery/InlineEntity.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\Entity; + +abstract class InlineEntity extends Entity +{ + /** + * Magic method to set properties dynamically + * + * @param $method + * @param $args + * + * @return \Longman\TelegramBot\Entities\InlineQuery\InlineEntity + */ + public function __call($method, $args) + { + $action = substr($method, 0, 3); + if ($action === 'set') { + //Convert method to snake_case (which is the name of the property) + $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); + $this->$property_name = $args[0]; + } + + return $this; + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultArticle.php b/src/Entities/InlineQuery/InlineQueryResultArticle.php new file mode 100644 index 000000000..d8abff49c --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultArticle.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultArticle + * + * @link https://core.telegram.org/bots/api#inlinequeryresultarticle + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'input_message_content' => , + * 'reply_markup' => , + * 'url' => '', + * 'hide_url' => true, + * 'description' => '', + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be article + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method string getTitle() Title of the result + * @method InputMessageContent getInputMessageContent() Content of the message to be sent + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method string getUrl() Optional. URL of the result + * @method bool getHideUrl() Optional. Pass True, if you don't want the URL to be shown in the message + * @method string getDescription() Optional. Short description of the result + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setTitle(string $title) Title of the result + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Content of the message to be sent + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setUrl(string $url) Optional. URL of the result + * @method $this setHideUrl(bool $hide_url) Optional. Pass True, if you don't want the URL to be shown in the message + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ +class InlineQueryResultArticle extends InlineEntity +{ + /** + * InlineQueryResultArticle constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'article'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultAudio.php b/src/Entities/InlineQuery/InlineQueryResultAudio.php new file mode 100644 index 000000000..00890d77b --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultAudio.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultAudio + * + * @link https://core.telegram.org/bots/api#inlinequeryresultaudio + * + * + * $data = [ + * 'id' => '', + * 'audio_url' => '', + * 'title' => '', + * 'performer' => '', + * 'audio_duration' => 123, + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be audio + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getAudioUrl() A valid URL for the audio file + * @method string getTitle() Title + * @method string getPerformer() Optional. Performer + * @method int getAudioDuration() Optional. Audio duration in seconds + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setAudioUrl(string $audio_url) A valid URL for the audio file + * @method $this setTitle(string $title) Title + * @method $this setPerformer(string $performer) Optional. Performer + * @method $this setAudioDuration(int $audio_duration) Optional. Audio duration in seconds + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ +class InlineQueryResultAudio extends InlineEntity +{ + /** + * InlineQueryResultAudio constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'audio'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php new file mode 100644 index 000000000..062a93601 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedAudio + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedaudio + * + * + * $data = [ + * 'id' => '', + * 'audio_file_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be audio + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getAudioFileId() A valid file identifier for the audio file + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setAudioFileId(string $audio_file_id) A valid file identifier for the audio file + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ +class InlineQueryResultCachedAudio extends InlineEntity +{ + /** + * InlineQueryResultCachedAudio constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'audio'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php new file mode 100644 index 000000000..6ddcfb6a6 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedDocument + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcacheddocument + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'document_file_id' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be document + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getTitle() Title for the result + * @method string getDocumentFileId() A valid file identifier for the file + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the document to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setTitle(string $title) Title for the result + * @method $this setDocumentFileId(string $document_file_id) A valid file identifier for the file + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file + */ +class InlineQueryResultCachedDocument extends InlineEntity +{ + /** + * InlineQueryResultCachedDocument constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'document'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php new file mode 100644 index 000000000..5923ff9ef --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedGif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedgif + * + * + * $data = [ + * 'id' => '', + * 'gif_file_id' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getGifFileId() A valid file identifier for the GIF file + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setGifFileId(string $gif_file_id) A valid file identifier for the GIF file + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ +class InlineQueryResultCachedGif extends InlineEntity +{ + /** + * InlineQueryResultCachedGif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php new file mode 100644 index 000000000..13b16ec5d --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedMpeg4Gif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedmpeg4gif + * + * + * $data = [ + * 'id' => '', + * 'mpeg4_file_id' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method String getType() Type of the result, must be mpeg4_gif + * @method String getId() Unique identifier for this result, 1-64 bytes + * @method String getMpeg4FileId() A valid file identifier for the MP4 file + * @method String getTitle() Optional. Title for the result + * @method String getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setMpeg4FileId(string $mpeg4_file_id) A valid file identifier for the MP4 file + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation + */ +class InlineQueryResultCachedMpeg4Gif extends InlineEntity +{ + /** + * InlineQueryResultCachedMpeg4Gif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'mpeg4_gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php new file mode 100644 index 000000000..41041680b --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultCachedPhoto extends InlineEntity +{ + /** + * InlineQueryResultCachedPhoto constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'photo'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php new file mode 100644 index 000000000..d46caac40 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultCachedSticker extends InlineEntity +{ + /** + * InlineQueryResultCachedSticker constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'sticker'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php new file mode 100644 index 000000000..414ab0266 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultCachedVideo extends InlineEntity +{ + /** + * InlineQueryResultCachedVideo constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'video'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php new file mode 100644 index 000000000..6b947bb8e --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultCachedVoice extends InlineEntity +{ + /** + * InlineQueryResultCachedVoice constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'voice'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultContact.php b/src/Entities/InlineQuery/InlineQueryResultContact.php new file mode 100644 index 000000000..c0198c687 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultContact.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultContact extends InlineEntity +{ + /** + * InlineQueryResultContact constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'contact'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultDocument.php b/src/Entities/InlineQuery/InlineQueryResultDocument.php new file mode 100644 index 000000000..c08c63374 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultDocument.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultDocument extends InlineEntity +{ + /** + * InlineQueryResultDocument constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'document'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultGif.php b/src/Entities/InlineQuery/InlineQueryResultGif.php new file mode 100644 index 000000000..8389d98ab --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultGif.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultGif extends InlineEntity +{ + /** + * InlineQueryResultGif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultLocation.php b/src/Entities/InlineQuery/InlineQueryResultLocation.php new file mode 100644 index 000000000..27b5b6c80 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultLocation.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultLocation extends InlineEntity +{ + /** + * InlineQueryResultLocation constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'location'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php new file mode 100644 index 000000000..3d968d611 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultMpeg4Gif extends InlineEntity +{ + /** + * InlineQueryResultMpeg4Gif constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'mpeg4_gif'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultPhoto.php b/src/Entities/InlineQuery/InlineQueryResultPhoto.php new file mode 100644 index 000000000..1f9b837f8 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultPhoto.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultPhoto extends InlineEntity +{ + /** + * InlineQueryResultPhoto constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'photo'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVenue.php b/src/Entities/InlineQuery/InlineQueryResultVenue.php new file mode 100644 index 000000000..4d7a58255 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVenue.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultVenue extends InlineEntity +{ + /** + * InlineQueryResultVenue constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'venue'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVideo.php b/src/Entities/InlineQuery/InlineQueryResultVideo.php new file mode 100644 index 000000000..50c7d0ff1 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVideo.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultVideo extends InlineEntity +{ + /** + * InlineQueryResultVideo constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'video'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQuery/InlineQueryResultVoice.php b/src/Entities/InlineQuery/InlineQueryResultVoice.php new file mode 100644 index 000000000..eaf971987 --- /dev/null +++ b/src/Entities/InlineQuery/InlineQueryResultVoice.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InlineQuery; + +class InlineQueryResultVoice extends InlineEntity +{ + /** + * InlineQueryResultVoice constructor + * + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public function __construct(array $data = []) + { + $data['type'] = 'voice'; + parent::__construct($data); + } +} diff --git a/src/Entities/InlineQueryResult.php b/src/Entities/InlineQueryResult.php deleted file mode 100644 index 1f19a13fe..000000000 --- a/src/Entities/InlineQueryResult.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResult extends Entity -{ - /** - * @var string - */ - protected $type; - - /** - * @var mixed|null - */ - protected $id; - - /** - * @var mixed|null - */ - protected $input_message_content; - - /** - * @var mixed|null - */ - protected $reply_markup; - - /** - * InlineQueryResult constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->type = null; - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->input_message_content = isset($data['input_message_content']) ? $data['input_message_content'] : null; - $this->reply_markup = isset($data['reply_markup']) ? $data['reply_markup'] : null; - } - - /** - * Get type - * - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get input message content - * - * @return mixed|null - */ - public function getInputMessageContent() - { - return $this->input_message_content; - } - - /** - * Get reply markup - * - * @return mixed|null - */ - public function getReplyMarkup() - { - return $this->reply_markup; - } -} diff --git a/src/Entities/InlineQueryResultArticle.php b/src/Entities/InlineQueryResultArticle.php deleted file mode 100644 index 1ce74ad32..000000000 --- a/src/Entities/InlineQueryResultArticle.php +++ /dev/null @@ -1,166 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultArticle extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $input_message_content; - - /** - * @var mixed|null - */ - protected $url; - - /** - * @var mixed|null - */ - protected $hide_url; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultArticle constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'article'; - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->input_message_content = isset($data['input_message_content']) ? $data['input_message_content'] : null; - if (empty($this->input_message_content)) { - throw new TelegramException('input_message_content is empty!'); - } - - $this->url = isset($data['url']) ? $data['url'] : null; - $this->hide_url = isset($data['hide_url']) ? $data['hide_url'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get input message content - * - * @return mixed|null - */ - public function getInputMessageContent() - { - return $this->input_message_content; - } - - /** - * Get url - * - * @return mixed|null - */ - public function getUrl() - { - return $this->url; - } - - /** - * Get hide url - * - * @return mixed|null - */ - public function getHideUrl() - { - return $this->hide_url; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultAudio.php b/src/Entities/InlineQueryResultAudio.php deleted file mode 100644 index c00aa411c..000000000 --- a/src/Entities/InlineQueryResultAudio.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultAudio extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $audio_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $performer; - - /** - * @var mixed|null - */ - protected $audio_duration; - - /** - * InlineQueryResultAudio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'audio'; - - $this->audio_url = isset($data['audio_url']) ? $data['audio_url'] : null; - if (empty($this->audio_url)) { - throw new TelegramException('audio_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->performer = isset($data['performer']) ? $data['performer'] : null; - $this->audio_duration = isset($data['audio_duration']) ? $data['audio_duration'] : null; - } - - /** - * Get audio url - * - * @return mixed|null - */ - public function getAudioUrl() - { - return $this->audio_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get performer - * - * @return mixed|null - */ - public function getPerformer() - { - return $this->performer; - } - - /** - * Get audio duration - * - * @return mixed|null - */ - public function getAudioDuration() - { - return $this->audio_duration; - } -} diff --git a/src/Entities/InlineQueryResultCachedAudio.php b/src/Entities/InlineQueryResultCachedAudio.php deleted file mode 100644 index f7e884eca..000000000 --- a/src/Entities/InlineQueryResultCachedAudio.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedAudio extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $audio_file_id; - - /** - * InlineQueryResultCachedAudio constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'audio'; - - $this->audio_file_id = isset($data['audio_file_id']) ? $data['audio_file_id'] : null; - if (empty($this->audio_file_id)) { - throw new TelegramException('audio_file_id is empty!'); - } - } - - /** - * Get audio file id - * - * @return mixed|null - */ - public function getAudioFileId() - { - return $this->audio_file_id; - } -} diff --git a/src/Entities/InlineQueryResultCachedDocument.php b/src/Entities/InlineQueryResultCachedDocument.php deleted file mode 100644 index d45f89ecf..000000000 --- a/src/Entities/InlineQueryResultCachedDocument.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedDocument extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $document_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedDocument constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'document'; - - $this->document_file_id = isset($data['document_file_id']) ? $data['document_file_id'] : null; - if (empty($this->document_file_id)) { - throw new TelegramException('document_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get document file id - * - * @return mixed|null - */ - public function getDocumentFileId() - { - return $this->document_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedGif.php b/src/Entities/InlineQueryResultCachedGif.php deleted file mode 100644 index c314e2c05..000000000 --- a/src/Entities/InlineQueryResultCachedGif.php +++ /dev/null @@ -1,87 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedGif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $gif_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedGif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'gif'; - - $this->gif_file_id = isset($data['gif_file_id']) ? $data['gif_file_id'] : null; - if (empty($this->gif_file_id)) { - throw new TelegramException('gif_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get gif file id - * - * @return mixed|null - */ - public function getGifFileId() - { - return $this->gif_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQueryResultCachedMpeg4Gif.php deleted file mode 100644 index 02aee0fea..000000000 --- a/src/Entities/InlineQueryResultCachedMpeg4Gif.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedMpeg4Gif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $mpeg4_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedMpeg4Gif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'mpeg4_gif'; - - $this->mpeg4_file_id = isset($data['mpeg4_file_id']) ? $data['mpeg4_file_id'] : null; - if (empty($this->mpeg4_file_id)) { - throw new TelegramException('mpeg4_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get mp4 file id - * - * @return mixed|null - */ - public function getMpeg4FileId() - { - return $this->mpeg4_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQueryResultCachedPhoto.php deleted file mode 100644 index 32f4d7603..000000000 --- a/src/Entities/InlineQueryResultCachedPhoto.php +++ /dev/null @@ -1,98 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedPhoto extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $photo_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedPhoto constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->photo_file_id = isset($data['photo_file_id']) ? $data['photo_file_id'] : null; - if (empty($this->photo_file_id)) { - throw new TelegramException('photo_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get photo file id - * - * @return mixed|null - */ - public function getPhotoFileId() - { - return $this->photo_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedSticker.php b/src/Entities/InlineQueryResultCachedSticker.php deleted file mode 100644 index d1f1a7db4..000000000 --- a/src/Entities/InlineQueryResultCachedSticker.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedSticker extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $sticker_file_id; - - /** - * InlineQueryResultCachedSticker constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'sticker'; - - $this->photo_file_id = isset($data['sticker_file_id']) ? $data['sticker_file_id'] : null; - if (empty($this->sticker_file_id)) { - throw new TelegramException('sticker_file_id is empty!'); - } - } - - /** - * Get sticker file id - * - * @return mixed|null - */ - public function getStickerFileId() - { - return $this->sticker_file_id; - } -} diff --git a/src/Entities/InlineQueryResultCachedVideo.php b/src/Entities/InlineQueryResultCachedVideo.php deleted file mode 100644 index 9ade9d5f6..000000000 --- a/src/Entities/InlineQueryResultCachedVideo.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedVideo extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $video_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedVideo constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->video_file_id = isset($data['video_file_id']) ? $data['video_file_id'] : null; - if (empty($this->video_file_id)) { - throw new TelegramException('video_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get video file id - * - * @return mixed|null - */ - public function getVideoFileId() - { - return $this->video_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultCachedVoice.php b/src/Entities/InlineQueryResultCachedVoice.php deleted file mode 100644 index ef9c9822b..000000000 --- a/src/Entities/InlineQueryResultCachedVoice.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultCachedVoice extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $voice_file_id; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultCachedVoice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'voice'; - - $this->voice_file_id = isset($data['voice_file_id']) ? $data['voice_file_id'] : null; - if (empty($this->voice_file_id)) { - throw new TelegramException('voice_file_id is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get voice file id - * - * @return mixed|null - */ - public function getVoiceFileId() - { - return $this->voice_file_id; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultContact.php b/src/Entities/InlineQueryResultContact.php deleted file mode 100644 index db5f4ed94..000000000 --- a/src/Entities/InlineQueryResultContact.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultContact extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultContact constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'contact'; - - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get phone number - * - * @return mixed|null - */ - public function getPhoneNumber() - { - return $this->phone_number; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultDocument.php b/src/Entities/InlineQueryResultDocument.php deleted file mode 100644 index c4e1ca5b8..000000000 --- a/src/Entities/InlineQueryResultDocument.php +++ /dev/null @@ -1,171 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultDocument extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var mixed|null - */ - protected $document_url; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultDocument constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'document'; - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->caption = isset($data['caption']) ? $data['caption'] : null; - - $this->document_url = isset($data['document_url']) ? $data['document_url'] : null; - if (empty($this->document_url)) { - throw new TelegramException('document_url is empty!'); - } - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - if (empty($this->mime_type)) { - throw new TelegramException('mime_type is empty!'); - } - - $this->description = isset($data['description']) ? $data['description'] : null; - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get document url - * - * @return mixed|null - */ - public function getDocumentUrl() - { - return $this->document_url; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultGif.php b/src/Entities/InlineQueryResultGif.php deleted file mode 100644 index 2f3df845e..000000000 --- a/src/Entities/InlineQueryResultGif.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultGif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $gif_url; - - /** - * @var mixed|null - */ - protected $gif_width; - - /** - * @var mixed|null - */ - protected $gif_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultGif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'gif'; - - $this->gif_url = isset($data['gif_url']) ? $data['gif_url'] : null; - if (empty($this->gif_url)) { - throw new TelegramException('gif_url is empty!'); - } - - $this->gif_width = isset($data['gif_width']) ? $data['gif_width'] : null; - $this->gif_height = isset($data['gif_height']) ? $data['gif_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get gif url - * - * @return mixed|null - */ - public function getGifUrl() - { - return $this->gif_url; - } - - /** - * Get gif width - * - * @return mixed|null - */ - public function getGifWidth() - { - return $this->gif_width; - } - - /** - * Get gif height - * - * @return mixed|null - */ - public function getGifHeight() - { - return $this->gif_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultLocation.php b/src/Entities/InlineQueryResultLocation.php deleted file mode 100644 index 22ece50f9..000000000 --- a/src/Entities/InlineQueryResultLocation.php +++ /dev/null @@ -1,138 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultLocation extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultLocation constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'location'; - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQueryResultMpeg4Gif.php deleted file mode 100644 index 7884317eb..000000000 --- a/src/Entities/InlineQueryResultMpeg4Gif.php +++ /dev/null @@ -1,135 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultMpeg4Gif extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $mpeg4_url; - - /** - * @var mixed|null - */ - protected $mpeg4_width; - - /** - * @var mixed|null - */ - protected $mpeg4_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultMpeg4Gif constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'mpeg4_gif'; - - $this->mpeg4_url = isset($data['mpeg4_url']) ? $data['mpeg4_url'] : null; - if (empty($this->mpeg4_url)) { - throw new TelegramException('mpeg4_url is empty!'); - } - - $this->mpeg4_width = isset($data['mpeg4_width']) ? $data['mpeg4_width'] : null; - $this->mpeg4_height = isset($data['mpeg4_height']) ? $data['mpeg4_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get mp4 url - * - * @return mixed|null - */ - public function getMpeg4Url() - { - return $this->mpeg4_url; - } - - /** - * Get mp4 width - * - * @return mixed|null - */ - public function getMpeg4Width() - { - return $this->mpeg4_width; - } - - /** - * Get mp4 height - * - * @return mixed|null - */ - public function getMpeg4Height() - { - return $this->mpeg4_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultPhoto.php b/src/Entities/InlineQueryResultPhoto.php deleted file mode 100644 index a6bdd2b1d..000000000 --- a/src/Entities/InlineQueryResultPhoto.php +++ /dev/null @@ -1,151 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultPhoto extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $photo_url; - - /** - * @var mixed|null - */ - protected $photo_width; - - /** - * @var mixed|null - */ - protected $photo_height; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $description; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * InlineQueryResultPhoto constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'photo'; - - $this->photo_url = isset($data['photo_url']) ? $data['photo_url'] : null; - if (empty($this->photo_url)) { - throw new TelegramException('photo_url is empty!'); - } - - $this->photo_width = isset($data['photo_width']) ? $data['photo_width'] : null; - $this->photo_height = isset($data['photo_height']) ? $data['photo_height'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - } - - /** - * Get photo url - * - * @return mixed|null - */ - public function getPhotoUrl() - { - return $this->photo_url; - } - - /** - * Get photo width - * - * @return mixed|null - */ - public function getPhotoWidth() - { - return $this->photo_width; - } - - /** - * Get photo height - * - * @return mixed|null - */ - public function getPhotoHeight() - { - return $this->photo_height; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } -} diff --git a/src/Entities/InlineQueryResultVenue.php b/src/Entities/InlineQueryResultVenue.php deleted file mode 100644 index c88f5ef73..000000000 --- a/src/Entities/InlineQueryResultVenue.php +++ /dev/null @@ -1,175 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVenue extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $thumb_width; - - /** - * @var mixed|null - */ - protected $thumb_height; - - /** - * InlineQueryResultVenue constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'venue'; - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - $this->thumb_width = isset($data['thumb_width']) ? $data['thumb_width'] : null; - $this->thumb_height = isset($data['thumb_height']) ? $data['thumb_height'] : null; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get address - * - * @return mixed|null - */ - public function getAddress() - { - return $this->address; - } - - /** - * Get forsquare id - * - * @return mixed|null - */ - public function getFoursquareId() - { - return $this->foursquare_id; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get thumb width - * - * @return mixed|null - */ - public function getThumbWidth() - { - return $this->thumb_width; - } - - /** - * Get thumb height - * - * @return mixed|null - */ - public function getThumbHeight() - { - return $this->thumb_height; - } -} diff --git a/src/Entities/InlineQueryResultVideo.php b/src/Entities/InlineQueryResultVideo.php deleted file mode 100644 index 1d6be2e06..000000000 --- a/src/Entities/InlineQueryResultVideo.php +++ /dev/null @@ -1,184 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVideo extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $video_url; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $thumb_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var mixed|null - */ - protected $video_width; - - /** - * @var mixed|null - */ - protected $video_height; - - /** - * @var mixed|null - */ - protected $video_duration; - - /** - * @var mixed|null - */ - protected $description; - - /** - * InlineQueryResultVideo constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'video'; - - $this->video_url = isset($data['video_url']) ? $data['video_url'] : null; - if (empty($this->video_url)) { - throw new TelegramException('video_url is empty!'); - } - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - if (empty($this->mime_type)) { - throw new TelegramException('mime_type is empty!'); - } - $this->thumb_url = isset($data['thumb_url']) ? $data['thumb_url'] : null; - if (empty($this->thumb_url)) { - throw new TelegramException('thumb_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - $this->caption = isset($data['caption']) ? $data['caption'] : null; - $this->video_width = isset($data['video_width']) ? $data['video_width'] : null; - $this->video_height = isset($data['video_height']) ? $data['video_height'] : null; - $this->video_duration = isset($data['video_duration']) ? $data['video_duration'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - } - - /** - * Get video url - * - * @return mixed|null - */ - public function getVideoUrl() - { - return $this->video_url; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get thumb url - * - * @return mixed|null - */ - public function getThumbUrl() - { - return $this->thumb_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get caption - * - * @return mixed|null - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get video width - * - * @return mixed|null - */ - public function getVideoWidth() - { - return $this->video_width; - } - - /** - * Get video height - * - * @return mixed|null - */ - public function getVideoHeight() - { - return $this->video_height; - } - - /** - * Get video duration - * - * @return mixed|null - */ - public function getVideoDuration() - { - return $this->video_duration; - } - - /** - * Get description - * - * @return mixed|null - */ - public function getDescription() - { - return $this->description; - } -} diff --git a/src/Entities/InlineQueryResultVoice.php b/src/Entities/InlineQueryResultVoice.php deleted file mode 100644 index 4dae9f2fa..000000000 --- a/src/Entities/InlineQueryResultVoice.php +++ /dev/null @@ -1,86 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InlineQueryResultVoice extends InlineQueryResult -{ - /** - * @var mixed|null - */ - protected $voice_url; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $voice_duration; - - /** - * InlineQueryResultVoice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - parent::__construct($data); - - $this->type = 'voice'; - - $this->voice_url = isset($data['voice_url']) ? $data['voice_url'] : null; - if (empty($this->voice_url)) { - throw new TelegramException('voice_url is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->voice_duration = isset($data['voice_duration']) ? $data['voice_duration'] : null; - } - - /** - * Get voice url - * - * @return mixed|null - */ - public function getVoiceUrl() - { - return $this->voice_url; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get voice duration - * - * @return mixed|null - */ - public function getVoiceDuration() - { - return $this->voice_duration; - } -} diff --git a/src/Entities/InputContactMessageContent.php b/src/Entities/InputContactMessageContent.php deleted file mode 100644 index 3a5da47b4..000000000 --- a/src/Entities/InputContactMessageContent.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputContactMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $phone_number; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * InputContactMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null; - if (empty($this->phone_number)) { - throw new TelegramException('phone_number is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - if (empty($this->first_name)) { - throw new TelegramException('first_name is empty!'); - } - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - } -} diff --git a/src/Entities/InputLocationMessageContent.php b/src/Entities/InputLocationMessageContent.php deleted file mode 100644 index 567da0464..000000000 --- a/src/Entities/InputLocationMessageContent.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputLocationMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * InputLocationMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - } -} diff --git a/src/Entities/InputMessageContent/InputContactMessageContent.php b/src/Entities/InputMessageContent/InputContactMessageContent.php new file mode 100644 index 000000000..d6bf7ec59 --- /dev/null +++ b/src/Entities/InputMessageContent/InputContactMessageContent.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputContactMessageContent + * + * @link https://core.telegram.org/bots/api#inputcontactmessagecontent + * + * + * $data = [ + * 'phone_number' => '', + * 'first_name' => '', + * 'last_name' => '', + * ]; + * + * + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * + * @method $this setPhoneNumber(string $phone_number) Contact's phone number + * @method $this setFirstName(string $first_name) Contact's first name + * @method $this setLastName(string $last_name) Optional. Contact's last name + */ +class InputContactMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputLocationMessageContent.php b/src/Entities/InputMessageContent/InputLocationMessageContent.php new file mode 100644 index 000000000..8e0fa9011 --- /dev/null +++ b/src/Entities/InputMessageContent/InputLocationMessageContent.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputLocationMessageContent + * + * @link https://core.telegram.org/bots/api#inputlocationmessagecontent + * + * + * $data = [ + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * ]; + * + * @method float getLatitude() Latitude of the location in degrees + * @method float getLongitude() Longitude of the location in degrees + * + * @method $this setLatitude(float $latitude) Latitude of the location in degrees + * @method $this setLongitude(float $longitude) Longitude of the location in degrees + */ +class InputLocationMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputMessageContent.php b/src/Entities/InputMessageContent/InputMessageContent.php new file mode 100644 index 000000000..3b43be9e9 --- /dev/null +++ b/src/Entities/InputMessageContent/InputMessageContent.php @@ -0,0 +1,8 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputTextMessageContent + * + * @link https://core.telegram.org/bots/api#inputtextmessagecontent + * + * + * $data = [ + * 'message_text' => '', + * 'parse_mode' => '', + * 'disable_web_page_preview' => true, + * ]; + * + * + * @method string getMessageText() Text of the message to be sent, 1-4096 characters. + * @method string getParseMode() Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message. + * @method bool getDisableWebPagePreview() Optional. Disables link previews for links in the sent message + * + * @method $this setMessageText(string $message_text) Text of the message to be sent, 1-4096 characters. + * @method $this setParseMode(string $parse_mode) Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message. + * @method $this setDisableWebPagePreview(bool $disable_web_page_preview) Optional. Disables link previews for links in the sent message + */ +class InputTextMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputMessageContent/InputVenueMessageContent.php b/src/Entities/InputMessageContent/InputVenueMessageContent.php new file mode 100644 index 000000000..4d00cab15 --- /dev/null +++ b/src/Entities/InputMessageContent/InputVenueMessageContent.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities\InputMessageContent; + +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; + +/** + * Class InputVenueMessageContent + * + * @link https://core.telegram.org/bots/api#inputvenuemessagecontent + * + * + * $data = [ + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'address' => '', + * 'foursquare_id' => '', + * ]; + * + * + * @method float getLatitude() Latitude of the location in degrees + * @method float getLongitude() Longitude of the location in degrees + * @method string getTitle() Name of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareIdTitle() Optional. Foursquare identifier of the venue, if known + * + * @method $this setLatitude(float $latitude) Latitude of the location in degrees + * @method $this setLongitude(float $longitude) Longitude of the location in degrees + * @method $this setTitle(string $title) Name of the venue + * @method $this setAddress(string $address) Address of the venue + * @method $this setFoursquareIdTitle(string $foursquare_id_title) Optional. Foursquare identifier of the venue, if known + */ +class InputVenueMessageContent extends InlineEntity implements InputMessageContent +{ + +} diff --git a/src/Entities/InputTextMessageContent.php b/src/Entities/InputTextMessageContent.php deleted file mode 100644 index 608adba84..000000000 --- a/src/Entities/InputTextMessageContent.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputTextMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $message_text; - - /** - * @var mixed|null - */ - protected $parse_mode; - - /** - * @var mixed|null - */ - protected $disable_web_page_preview; - - /** - * InputTextMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->message_text = isset($data['message_text']) ? $data['message_text'] : null; - if (empty($this->message_text)) { - throw new TelegramException('message_text is empty!'); - } - - $this->parse_mode = isset($data['parse_mode']) ? $data['parse_mode'] : null; - $this->disable_web_page_preview = isset($data['disable_web_page_preview']) ? $data['disable_web_page_preview'] : null; - } -} diff --git a/src/Entities/InputVenueMessageContent.php b/src/Entities/InputVenueMessageContent.php deleted file mode 100644 index 9516bde5b..000000000 --- a/src/Entities/InputVenueMessageContent.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -class InputVenueMessageContent extends InputMessageContent -{ - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * InputVenueMessageContent constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - } -} diff --git a/src/Entities/Location.php b/src/Entities/Location.php index e9e20e037..c9340bce7 100644 --- a/src/Entities/Location.php +++ b/src/Entities/Location.php @@ -10,56 +10,15 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Location + * + * @link https://core.telegram.org/bots/api#location + * + * @method float getLongitude() Longitude as defined by sender + * @method float getLatitude() Latitude as defined by sender + */ class Location extends Entity { - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $latitude; - - /** - * Location constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } } diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 7cac7d22c..6eadb1aa5 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -10,402 +10,140 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Message + * + * @link https://core.telegram.org/bots/api#message + * + * @method int getMessageId() Unique message identifier + * @method User getFrom() Optional. Sender, can be empty for messages sent to channels + * @method int getDate() Date the message was sent in Unix time + * @method Chat getChat() Conversation the message belongs to + * @method User getForwardFrom() Optional. For forwarded messages, sender of the original message + * @method Chat getForwardFromChat() Optional. For messages forwarded from a channel, information about the original channel + * @method int getForwardDate() Optional. For forwarded messages, date the original message was sent in Unix time + * @method Message getReplyToMessage() Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. + * @method int getEditDate() Optional. Date the message was last edited in Unix time + * @method Audio getAudio() Optional. Message is an audio file, information about the file + * @method Document getDocument() Optional. Message is a general file, information about the file + * @method Sticker getSticker() Optional. Message is a sticker, information about the sticker + * @method Video getVideo() Optional. Message is a video, information about the video + * @method Voice getVoice() Optional. Message is a voice message, information about the file + * @method string getCaption() Optional. Caption for the document, photo or video, 0-200 characters + * @method Contact getContact() Optional. Message is a shared contact, information about the contact + * @method Location getLocation() Optional. Message is a shared location, information about the location + * @method Venue getVenue() Optional. Message is a venue, information about the venue + * @method User getNewChatMember() Optional. A new member was added to the group, information about them (this member may be the bot itself) + * @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself) + * @method string getNewChatTitle() Optional. A chat title was changed to this value + * @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted + * @method bool getGroupChatCreated() Optional. Service message: the group has been created + * @method bool getSupergroupChatCreated() Optional. Service message: the supergroup has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a supergroup when it is created. It can only be found in reply_to_message if someone replies to a very first message in a directly created supergroup. + * @method bool getChannelChatCreated() Optional. Service message: the channel has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a channel when it is created. It can only be found in reply_to_message if someone replies to a very first message in a channel. + * @method int getMigrateToChatId() Optional. The group has been migrated to a supergroup with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method int getMigrateFromChatId() Optional. The supergroup has been migrated from a group with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + * @method Message getPinnedMessage() Optional. Specified message was pinned. Note that the Message object in this field will not contain further reply_to_message fields even if it is itself a reply. + */ class Message extends Entity { /** - * @var mixed|null - */ - protected $message_id; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $from; - - /** - * @var mixed|null - */ - protected $date; - - /** - * @var \Longman\TelegramBot\Entities\Chat|null - */ - protected $chat; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $forward_from; - - /** - * @var \Longman\TelegramBot\Entities\Chat|null - */ - protected $forward_from_chat; - - /** - * @var mixed|null - */ - protected $forward_date; - - /** - * @var mixed|null - */ - protected $edit_date; - - /** - * @var \Longman\TelegramBot\Entities\ReplyToMessage - */ - protected $reply_to_message; - - /** - * @var string|null - */ - protected $text; - - /** - * @var \Longman\TelegramBot\Entities\Audio|null - */ - protected $audio; - - /** - * @var \Longman\TelegramBot\Entities\Document|null - */ - protected $document; - - /** - * @var array|null - */ - protected $photo; - - /** - * @var \Longman\TelegramBot\Entities\Sticker|null - */ - protected $sticker; - - /** - * @var \Longman\TelegramBot\Entities\Video|null - */ - protected $video; - - /** - * @var \Longman\TelegramBot\Entities\Voice|null - */ - protected $voice; - - /** - * @var mixed|null - */ - protected $caption; - - /** - * @var \Longman\TelegramBot\Entities\Contact|null - */ - protected $contact; - - /** - * @var \Longman\TelegramBot\Entities\Location|null - */ - protected $location; - - /** - * @var mixed|null - */ - protected $venue; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $new_chat_member; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $left_chat_member; - - /** - * @var mixed|null - */ - protected $new_chat_title; - - /** - * @var mixed|null - */ - protected $new_chat_photo; - - /** - * @var mixed|null + * {@inheritdoc} */ - protected $delete_chat_photo; - - /** - * @var mixed|null - */ - protected $group_chat_created; - - /** - * @var mixed|null - */ - protected $supergroup_chat_created; - - /** - * @var mixed|null - */ - protected $channel_chat_created; - - /** - * @var mixed|null - */ - protected $migrate_to_chat_id; - - /** - * @var mixed|null - */ - protected $migrate_from_chat_id; - - /** - * @var mixed|null - */ - protected $pinned_message; - - /** - * @var mixed|null - */ - protected $entities; - - /** - * @var mixed|null - */ - private $command; - - /** - * @var mixed|null - */ - private $type; - - /** - * Message constructor. - * - * @param array $data - * @param $bot_name - */ - public function __construct(array $data, $bot_name) + protected function subEntities() { - - $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null; - if (!empty($this->reply_to_message)) { - $this->reply_to_message = new ReplyToMessage($this->reply_to_message, $bot_name); - } - - $this->init($data, $bot_name); + return [ + 'from' => User::class, + 'chat' => Chat::class, + 'forward_from' => User::class, + 'forward_from_chat' => User::class, + 'reply_to_message' => self::class, + 'entities' => MessageEntity::class, + 'audio' => Audio::class, + 'document' => Document::class, + 'photo' => PhotoSize::class, + 'sticker' => Sticker::class, + 'video' => Video::class, + 'voice' => Voice::class, + 'contact' => Contact::class, + 'location' => Location::class, + 'venue' => Venue::class, + 'new_chat_member' => User::class, + 'left_chat_member' => User::class, + 'new_chat_photo' => PhotoSize::class, + 'pinned_message' => Message::class, + ]; } /** - * Common init to Message and ReplyToMessage + * Message constructor * * @param array $data * @param string $bot_name + * * @throws \Longman\TelegramBot\Exception\TelegramException */ - protected function init(array &$data, $bot_name) + public function __construct(array $data, $bot_name = '') { - $this->bot_name = $bot_name; - - $this->type = 'Message'; - - $this->message_id = isset($data['message_id']) ? $data['message_id'] : null; - if (empty($this->message_id)) { - throw new TelegramException('message_id is empty!'); - } - - $this->from = isset($data['from']) ? $data['from'] : null; - if (!empty($this->from)) { - $this->from = new User($this->from); - } - - $this->chat = isset($data['chat']) ? $data['chat'] : null; - if (empty($this->chat)) { - throw new TelegramException('chat is empty!'); - } - $this->chat = new Chat($this->chat); - - $this->date = isset($data['date']) ? $data['date'] : null; - if (empty($this->date)) { - throw new TelegramException('date is empty!'); - } - - $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; - if (!empty($this->forward_from)) { - $this->forward_from = new User($this->forward_from); - } - - $this->forward_from_chat = isset($data['forward_from_chat']) ? $data['forward_from_chat'] : null; - if (!empty($this->forward_from_chat)) { - $this->forward_from_chat = new Chat($this->forward_from_chat); - } - - $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null; - - $this->edit_date = isset($data['edit_date']) ? $data['edit_date'] : null; - - $this->text = isset($data['text']) ? $data['text'] : null; - $command = $this->getCommand(); - if (!empty($command)) { - $this->type = 'command'; - } - - $this->audio = isset($data['audio']) ? $data['audio'] : null; - if (!empty($this->audio)) { - $this->audio = new Audio($this->audio); - $this->type = 'Audio'; - } - - $this->document = isset($data['document']) ? $data['document'] : null; - if (!empty($this->document)) { - $this->document = new Document($this->document); - $this->type = 'Document'; - } - - $this->photo = isset($data['photo']) ? $data['photo'] : null; //array of photosize - if (!empty($this->photo)) { - foreach ($this->photo as $photo) { - if (!empty($photo)) { - $photos[] = new PhotoSize($photo); - } - } - $this->photo = $photos; - $this->type = 'Photo'; - } - - $this->sticker = isset($data['sticker']) ? $data['sticker'] : null; - if (!empty($this->sticker)) { - $this->sticker = new Sticker($this->sticker); - $this->type = 'Sticker'; - } - - $this->video = isset($data['video']) ? $data['video'] : null; - if (!empty($this->video)) { - $this->video = new Video($this->video); - $this->type = 'Video'; - } - - $this->voice = isset($data['voice']) ? $data['voice'] : null; - if (!empty($this->voice)) { - $this->voice = new Voice($this->voice); - $this->type = 'Voice'; - } - - $this->caption = isset($data['caption']) ? $data['caption'] : null;//string - - $this->contact = isset($data['contact']) ? $data['contact'] : null; - if (!empty($this->contact)) { - $this->contact = new Contact($this->contact); - } - - $this->location = isset($data['location']) ? $data['location'] : null; - if (!empty($this->location)) { - $this->location = new Location($this->location); - $this->type = 'Location'; - } - - $this->venue = isset($data['venue']) ? $data['venue'] : null; - if (!empty($this->venue)) { - $this->venue = new Venue($this->venue); - $this->type = 'Venue'; - } - - //retrocompatibility + //Retro-compatibility if (isset($data['new_chat_participant'])) { $data['new_chat_member'] = $data['new_chat_participant']; + unset($data['new_chat_participant']); } - if (isset($data['left_chat_participant'])) { $data['left_chat_member'] = $data['left_chat_participant']; + unset($data['left_chat_participant']); } - $this->new_chat_member = isset($data['new_chat_member']) ? $data['new_chat_member'] : null; - if (!empty($this->new_chat_member)) { - $this->new_chat_member = new User($this->new_chat_member); - $this->type = 'new_chat_member'; - } - - $this->left_chat_member = isset($data['left_chat_member']) ? $data['left_chat_member'] : null; - if (!empty($this->left_chat_member)) { - $this->left_chat_member = new User($this->left_chat_member); - $this->type = 'left_chat_member'; - } - - $this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null; - if (!is_null($this->new_chat_title)) { - $this->type = 'new_chat_title'; - } - - $this->new_chat_photo = isset($data['new_chat_photo']) ? $data['new_chat_photo'] : null; //array of photosize - if (!empty($this->new_chat_photo)) { - foreach ($this->new_chat_photo as $photo) { - if (!empty($photo)) { - $photos[] = new PhotoSize($photo); - } - } - $this->new_chat_photo = $photos; - $this->type = 'new_chat_photo'; - } - - $this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null; - if ($this->delete_chat_photo) { - $this->type = 'delete_chat_photo'; - } - - $this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null; - if ($this->group_chat_created) { - $this->type = 'group_chat_created'; - } - - $this->supergroup_chat_created = isset($data['supergroup_chat_created']) ? $data['supergroup_chat_created'] : null; - if ($this->supergroup_chat_created) { - $this->type = 'supergroup_chat_created'; - } - - $this->channel_chat_created = isset($data['channel_chat_created']) ? $data['channel_chat_created'] : null; - if ($this->channel_chat_created) { - $this->type = 'channel_chat_created'; - } - - $this->migrate_to_chat_id = isset($data['migrate_to_chat_id']) ? $data['migrate_to_chat_id'] : null; - if ($this->migrate_to_chat_id) { - $this->type = 'migrate_to_chat_id'; - } + parent::__construct($data, $bot_name); + } - $this->migrate_from_chat_id = isset($data['migrate_from_chat_id']) ? $data['migrate_from_chat_id'] : null; - if ($this->migrate_from_chat_id) { - $this->type = 'migrate_from_chat_id'; - } + /** + * Optional. Message is a photo, available sizes of the photo + * + * This method overrides the default getPhoto method + * and returns a nice array of PhotoSize objects. + * + * @return array + */ + public function getPhoto() + { + return $this->makePrettyObjectArray(PhotoSize::class, 'photo'); + } - $this->pinned_message = isset($data['pinned_message']) ? $data['pinned_message'] : null; - if ($this->pinned_message) { - $this->pinned_message = new Message($this->pinned_message, $this->getBotName()); - } + /** + * Optional. A chat photo was changed to this value + * + * This method overrides the default getNewChatPhoto method + * and returns a nice array of PhotoSize objects. + * + * @return array + */ + public function getNewChatPhoto() + { + return $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); + } - $this->entities = isset($data['entities']) ? $data['entities'] : null; - if (!empty($this->entities)) { - foreach ($this->entities as $entity) { - if (!empty($entity)) { - $entities[] = new MessageEntity($entity); - } - } - $this->entities = $entities; - } + /** + * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text + * + * This method overrides the default getEntities method + * and returns a nice array of MessageEntity objects. + * + * @return array + */ + public function getEntities() + { + return $this->makePrettyObjectArray(MessageEntity::class, 'entities'); } /** * return the entire command like /echo or /echo@bot1 if specified * - * @return string|void + * @return string|null */ public function getFullCommand() { - if (substr($this->text, 0, 1) === '/') { + if (strpos($this->text, '/') === 0) { $no_EOL = strtok($this->text, PHP_EOL); $no_space = strtok($this->text, ' '); @@ -416,7 +154,7 @@ public function getFullCommand() return $no_EOL; } } else { - return; + return null; } } @@ -433,14 +171,14 @@ public function getCommand() $cmd = $this->getFullCommand(); - if (substr($cmd, 0, 1) === '/') { + if (strpos($cmd, '/') === 0) { $cmd = substr($cmd, 1); //check if command is follow by botname $split_cmd = explode('@', $cmd); if (isset($split_cmd[1])) { //command is followed by name check if is addressed to me - if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) { + if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) { return $this->command = $split_cmd[0]; } } else { @@ -453,99 +191,10 @@ public function getCommand() } /** - * Get message id - * - * @return mixed - */ - public function getMessageId() - { - return $this->message_id; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getFrom() - { - return $this->from; - } - - /** - * Get date - * - * @return mixed - */ - public function getDate() - { - return $this->date; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\Chat - */ - public function getChat() - { - return $this->chat; - } - - /** - * Get User object related to the forwarded message - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getForwardFrom() - { - return $this->forward_from; - } - - /** - * Get User object related to the message - * - * @return \Longman\TelegramBot\Entities\Chat - */ - public function getForwardFromChat() - { - return $this->forward_from_chat; - } - - /** - * Get forward date - * - * @return mixed - */ - public function getForwardDate() - { - return $this->forward_date; - } - - /** - * Get edit date - * - * @return mixed - */ - public function getEditDate() - { - return $this->edit_date; - } - - /** - * Get reply to message - * - * @return \Longman\TelegramBot\Entities\ReplyToMessage - */ - public function getReplyToMessage() - { - return $this->reply_to_message; - } - - /** - * Get text + * For text messages, the actual UTF-8 text of the message, 0-4096 characters. * * @param bool $without_cmd + * * @return string */ public function getText($without_cmd = false) @@ -563,226 +212,6 @@ public function getText($without_cmd = false) return $text; } - /** - * Get audio - * - * @return \Longman\TelegramBot\Entities\Audio - */ - public function getAudio() - { - return $this->audio; - } - - /** - * Get document - * - * @return \Longman\TelegramBot\Entities\Document - */ - public function getDocument() - { - return $this->document; - } - - /** - * Get photo - * - * @return array - */ - public function getPhoto() - { - return $this->photo; - } - - /** - * Get sticker - * - * @return \Longman\TelegramBot\Entities\Sticker - */ - public function getSticker() - { - return $this->sticker; - } - - /** - * Get video - * - * @return \Longman\TelegramBot\Entities\Video - */ - public function getVideo() - { - return $this->video; - } - - /** - * Get voice - * - * @return \Longman\TelegramBot\Entities\Voice - */ - public function getVoice() - { - return $this->voice; - } - - /** - * Get caption - * - * @return mixed - */ - public function getCaption() - { - return $this->caption; - } - - /** - * Get content - * - * @return \Longman\TelegramBot\Entities\Contact - */ - public function getContact() - { - return $this->contact; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get venue - * - * @return mixed - */ - public function getVenue() - { - return $this->venue; - } - - /** - * Get new chat participant - * - * @return mixed - */ - public function getNewChatParticipant() - { - return $this->new_chat_member; - } - - /** - * Get left chat participant - * - * @return mixed - */ - public function getLeftChatParticipant() - { - return $this->left_chat_member; - } - - /** - * Get User object related to the new member - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getNewChatMember() - { - return $this->new_chat_member; - } - - /** - * Get User object related to the left member - * - * @return \Longman\TelegramBot\Entities\User - */ - public function getLeftChatMember() - { - return $this->left_chat_member; - } - - /** - * Get new chat title - * - * @return mixed - */ - public function getNewChatTitle() - { - return $this->new_chat_title; - } - - /** - * Get new chat photo - * - * @return mixed - */ - public function getNewChatPhoto() - { - return $this->new_chat_photo; - } - - /** - * Get delete chat photo - * - * @return mixed - */ - public function getDeleteChatPhoto() - { - return $this->delete_chat_photo; - } - - /** - * Get group chat created - * - * @return mixed - */ - public function getGroupChatCreated() - { - return $this->group_chat_created; - } - - /** - * Get supergroup chat created - * - * @return mixed - */ - public function getSupergroupChatCreated() - { - return $this->supergroup_chat_created; - } - - /** - * Get channel chat created - * - * @return mixed - */ - public function getChannelChatCreated() - { - return $this->channel_chat_created; - } - - /** - * Get migrate to chat id - * - * @return mixed - */ - public function getMigrateToChatId() - { - return $this->migrate_to_chat_id; - } - - /** - * Get migrate from chat id - * - * @return mixed - */ - public function getMigrateFromChatId() - { - return $this->migrate_from_chat_id; - } - /** * Bot added in chat * @@ -791,7 +220,7 @@ public function getMigrateFromChatId() public function botAddedInChat() { if (!empty($this->new_chat_member)) { - if ($this->new_chat_member->getUsername() == $this->getBotName()) { + if ($this->new_chat_member->getUsername() === $this->getBotName()) { return true; } } @@ -800,32 +229,46 @@ public function botAddedInChat() } /** - * Get type + * Detect type based on properties. * - * @return mixed + * @return string|null */ public function getType() { - return $this->type; - } + $types = [ + 'text', + 'audio', + 'document', + 'photo', + 'sticker', + 'video', + 'voice', + 'contact', + 'location', + 'venue', + 'new_chat_member', + 'left_chat_member', + 'new_chat_title', + 'new_chat_photo', + 'delete_chat_photo', + 'group_chat_created', + 'supergroup_chat_created', + 'channel_chat_created', + 'migrate_to_chat_id', + 'migrate_from_chat_id', + 'pinned_message', + ]; + + foreach ($types as $type) { + if ($this->getProperty($type)) { + if ($type === 'text' && $this->getCommand()) { + return 'command'; + } - /** - * Get pinned message - * - * @return mixed - */ - public function getPinnedMessage() - { - return $this->pinned_message; - } + return $type; + } + } - /** - * Get entities - * - * @return mixed - */ - public function getEntities() - { - return $this->entities; + return 'message'; } } diff --git a/src/Entities/MessageEntity.php b/src/Entities/MessageEntity.php index 3e6ce3259..7ab9c5976 100644 --- a/src/Entities/MessageEntity.php +++ b/src/Entities/MessageEntity.php @@ -10,111 +10,26 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class MessageEntity + * + * @link https://core.telegram.org/bots/api#messageentity + * + * @method string getType() Type of the entity. Can be mention (@username), hashtag, bot_command, url, email, bold (bold text), italic (italic text), code (monowidth string), pre (monowidth block), text_link (for clickable text URLs), text_mention (for users without usernames) + * @method int getOffset() Offset in UTF-16 code units to the start of the entity + * @method int getLength() Length of the entity in UTF-16 code units + * @method string getUrl() Optional. For "text_link" only, url that will be opened after user taps on the text + * @method User getUser() Optional. For "text_mention" only, the mentioned user + */ class MessageEntity extends Entity { /** - * @var mixed|null - */ - protected $type; - - /** - * @var mixed|null - */ - protected $offset; - - /** - * @var mixed|null - */ - protected $length; - - /** - * @var mixed|null - */ - protected $url; - - /** - * @var \Longman\TelegramBot\Entities\User|null - */ - protected $user; - - /** - * MessageEntity constructor. - * - * @TODO check for type value from this list: https://core.telegram.org/bots/api#messageentity - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->type = isset($data['type']) ? $data['type'] : null; - if (empty($this->type)) { - throw new TelegramException('type is empty!'); - } - - $this->offset = isset($data['offset']) ? $data['offset'] : null; - if ($this->offset === '') { - throw new TelegramException('offset is empty!'); - } - - $this->length = isset($data['length']) ? $data['length'] : null; - if ($this->length === '') { - throw new TelegramException('length is empty!'); - } - - $this->url = isset($data['url']) ? $data['url'] : null; - $this->user = isset($data['user']) ? new User($data['user']) : null; - } - - /** - * Get type - * - * @return mixed|null - */ - public function getType() - { - return $this->type; - } - - /** - * Get offset - * - * @return mixed|null - */ - public function getOffset() - { - return $this->offset; - } - - /** - * Get length - * - * @return mixed|null - */ - public function getLength() - { - return $this->length; - } - - /** - * Get url - * - * @return mixed|null - */ - public function getUrl() - { - return $this->url; - } - - /** - * Get user - * - * @return \Longman\TelegramBot\Entities\User|null + * {@inheritdoc} */ - public function getUser() + protected function subEntities() { - return $this->user; + return [ + 'user' => User::class, + ]; } } diff --git a/src/Entities/PhotoSize.php b/src/Entities/PhotoSize.php index 6331220e0..5b577afca 100644 --- a/src/Entities/PhotoSize.php +++ b/src/Entities/PhotoSize.php @@ -10,94 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class PhotoSize + * + * @link https://core.telegram.org/bots/api#photosize + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Photo width + * @method int getHeight() Photo height + * @method int getFileSize() Optional. File size + */ class PhotoSize extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * PhotoSize constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } diff --git a/src/Entities/ReplyToMessage.php b/src/Entities/ReplyToMessage.php index 92c6d947c..03848a52b 100644 --- a/src/Entities/ReplyToMessage.php +++ b/src/Entities/ReplyToMessage.php @@ -19,7 +19,7 @@ class ReplyToMessage extends Message * @param array $data * @param string $bot_name */ - public function __construct(array $data, $bot_name) + public function __construct(array $data, $bot_name = '') { //As explained in the documentation diff --git a/src/Entities/Sticker.php b/src/Entities/Sticker.php index fa770114c..2eebeb3a6 100644 --- a/src/Entities/Sticker.php +++ b/src/Entities/Sticker.php @@ -10,132 +10,27 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Sticker + * + * @link https://core.telegram.org/bots/api#sticker + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Sticker width + * @method int getHeight() Sticker height + * @method PhotoSize getThumb() Optional. Sticker thumbnail in .webp or .jpg format + * @method string getEmoji() Optional. Emoji associated with the sticker + * @method int getFileSize() Optional. File size + */ class Sticker extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $emoji; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Sticker constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (empty($this->thumb)) { - throw new TelegramException('thumb is empty!'); - } - $this->thumb = new PhotoSize($this->thumb); - - $this->emoji = isset($data['emoji']) ? $data['emoji'] : null; - - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get emoji - * - * @return mixed|null - */ - public function getEmoji() - { - return $this->emoji; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/Update.php b/src/Entities/Update.php index 5a4fe2e12..845b653f2 100644 --- a/src/Entities/Update.php +++ b/src/Entities/Update.php @@ -10,183 +10,71 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Update + * + * @link https://core.telegram.org/bots/api#update + * + * @method int getUpdateId() The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. + * @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc. + * @method Message getEditedMessage() Optional. New version of a message that is known to the bot and was edited + * @method InlineQuery getInlineQuery() Optional. New incoming inline query + * @method ChosenInlineResult getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner. + * @method CallbackQuery getCallbackQuery() Optional. New incoming callback query + */ class Update extends Entity { /** - * @var mixed|null - */ - protected $update_id; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - protected $message; - - /** - * @var \Longman\TelegramBot\Entities\Message + * {@inheritdoc} */ - protected $edited_message; - - /** - * @var \Longman\TelegramBot\Entities\InlineQuery - */ - protected $inline_query; - - /** - * @var \Longman\TelegramBot\Entities\ChosenInlineResult - */ - protected $chosen_inline_result; - - /** - * @var \Longman\TelegramBot\Entities\CallbackQuery - */ - protected $callback_query; - - /** - * @var string - */ - private $update_type; - - /** - * Update constructor. - * - * @param array $data - * @param $bot_name - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data, $bot_name) + protected function subEntities() { - - $this->bot_name = $bot_name; - - $update_id = isset($data['update_id']) ? $data['update_id'] : null; - $this->update_id = $update_id; - - $this->message = isset($data['message']) ? $data['message'] : null; - if (!empty($this->message)) { - $this->message = new Message($this->message, $bot_name); - $this->update_type = 'message'; - } - - $this->edited_message = isset($data['edited_message']) ? $data['edited_message'] : null; - if (!empty($this->edited_message)) { - $this->edited_message = new Message($this->edited_message, $bot_name); - $this->update_type = 'edited_message'; - } - - if (empty($update_id)) { - throw new TelegramException('update_id is empty!'); - } - - $this->inline_query = isset($data['inline_query']) ? $data['inline_query'] : null; - if (!empty($this->inline_query)) { - $this->inline_query = new InlineQuery($this->inline_query); - $this->update_type = 'inline_query'; - } - - $this->chosen_inline_result = isset($data['chosen_inline_result']) ? $data['chosen_inline_result'] : null; - if (!empty($this->chosen_inline_result)) { - $this->chosen_inline_result = new ChosenInlineResult($this->chosen_inline_result); - $this->update_type = 'chosen_inline_result'; - } - - $this->callback_query = isset($data['callback_query']) ? $data['callback_query'] : null; - if (!empty($this->callback_query)) { - $this->callback_query = new CallbackQuery($this->callback_query); - $this->update_type = 'callback_query'; - } + return [ + 'message' => Message::class, + 'edited_message' => EditedMessage::class, + 'inline_query' => InlineQuery::class, + 'chosen_inline_result' => ChosenInlineResult::class, + 'callback_query' => CallbackQuery::class, + ]; } /** - * Get update id + * Get the update type based on the set properties * - * @return mixed|null - */ - public function getUpdateId() - { - return $this->update_id; - } - - /** - * Get message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getMessage() - { - return $this->message; - } - - /** - * Get edited message - * - * @return \Longman\TelegramBot\Entities\Message - */ - public function getEditedMessage() - { - return $this->edited_message; - } - - /** - * Get inline query - * - * @return \Longman\TelegramBot\Entities\InlineQuery - */ - public function getInlineQuery() - { - return $this->inline_query; - } - - /** - * Get callback query - * - * @return \Longman\TelegramBot\Entities\CallbackQuery - */ - public function getCallbackQuery() - { - return $this->callback_query; - } - - /** - * Get chosen inline result - * - * @return \Longman\TelegramBot\Entities\ChosenInlineResult - */ - public function getChosenInlineResult() - { - return $this->chosen_inline_result; - } - - /** - * Get update type - * - * @return string + * @return string|null */ public function getUpdateType() { - return $this->update_type; + $types = [ + 'message', + 'edited_message', + 'inline_query', + 'chosen_inline_result', + 'callback_query', + ]; + foreach ($types as $type) { + if ($this->getProperty($type)) { + return $type; + } + } + + return null; } /** * Get update content * * @return \Longman\TelegramBot\Entities\CallbackQuery - * |\Longman\TelegramBot\Entities\ChosenInlineResult - * |\Longman\TelegramBot\Entities\InlineQuery - * |\Longman\TelegramBot\Entities\Message + * |\Longman\TelegramBot\Entities\ChosenInlineResult + * |\Longman\TelegramBot\Entities\InlineQuery + * |\Longman\TelegramBot\Entities\Message */ public function getUpdateContent() { - if ($this->update_type == 'message') { - return $this->getMessage(); - } elseif ($this->update_type == 'inline_query') { - return $this->getInlineQuery(); - } elseif ($this->update_type == 'chosen_inline_result') { - return $this->getChosenInlineResult(); - } elseif ($this->update_type == 'callback_query') { - return $this->getCallbackQuery(); + if ($update_type = $this->getUpdateType()) { + return $this->getProperty($update_type); } + + return null; } } diff --git a/src/Entities/User.php b/src/Entities/User.php index b0ef6c7ca..a4079d959 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -10,99 +10,32 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class User + * + * @link https://core.telegram.org/bots/api#user + * + * @property int $id Unique identifier for this user or bot + * @property string $first_name User's or bot’s first name + * @property string $last_name Optional. User's or bot’s last name + * @property string $username Optional. User's or bot’s username + * + * @method int getId() Unique identifier for this user or bot + * @method string getFirstName() User's or bot’s first name + * @method string getLastName() Optional. User's or bot’s last name + * @method string getUsername() Optional. User's or bot’s username + */ class User extends Entity { /** - * @var mixed|null - */ - protected $id; - - /** - * @var mixed|null - */ - protected $first_name; - - /** - * @var mixed|null - */ - protected $last_name; - - /** - * @var mixed|null - */ - protected $username; - - /** - * User constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->id = isset($data['id']) ? $data['id'] : null; - if (empty($this->id)) { - throw new TelegramException('id is empty!'); - } - - $this->first_name = isset($data['first_name']) ? $data['first_name'] : null; - - $this->last_name = isset($data['last_name']) ? $data['last_name'] : null; - $this->username = isset($data['username']) ? $data['username'] : null; - } - - /** - * Get id - * - * @return mixed|null - */ - public function getId() - { - return $this->id; - } - - /** - * Get first name - * - * @return mixed|null - */ - public function getFirstName() - { - return $this->first_name; - } - - /** - * Get last name - * - * @return mixed|null - */ - public function getLastName() - { - return $this->last_name; - } - - /** - * Get username - * - * @return mixed|null - */ - public function getUsername() - { - return $this->username; - } - - /** - * Try nebtion + * Try mention * - * @return mixed|null|string + * @return string|null */ public function tryMention() { - if (is_null($this->username)) { - if (!is_null($this->last_name)) { + if ($this->username === null) { + if ($this->last_name !== null) { return $this->first_name . ' ' . $this->last_name; } return $this->first_name; diff --git a/src/Entities/UserProfilePhotos.php b/src/Entities/UserProfilePhotos.php index 592ee5727..19d10b57a 100644 --- a/src/Entities/UserProfilePhotos.php +++ b/src/Entities/UserProfilePhotos.php @@ -10,69 +10,46 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class UserProfilePhotos + * + * @link https://core.telegram.org/bots/api#userprofilephotos + * + * @method int getTotalCount() Total number of profile pictures the target user has + */ class UserProfilePhotos extends Entity { /** - * @var mixed|null - */ - protected $total_count; - - /** - * @var array + * {@inheritdoc} */ - protected $photos; - - /** - * UserProfilePhotos constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) + protected function subEntities() { - - $this->total_count = isset($data['total_count']) ? $data['total_count'] : null; - if ($this->total_count === null || !is_numeric($this->total_count)) { - throw new TelegramException('total_count is empty!'); - } - $this->photos = isset($data['photos']) ? $data['photos'] : null; - if ($this->photos === null || !is_array($data['photos'])) { - throw new TelegramException('photos is empty!'); - } - - $photos = []; - foreach ($this->photos as $key => $photo) { - if (is_array($photo)) { - foreach ($photo as $photo_size) { - $photos[$key][] = new PhotoSize($photo_size); - } - } else { - throw new TelegramException('photo is not an array!'); - } - } - - $this->photos = $photos; + return [ + 'photos' => PhotoSize::class, + ]; } /** - * Get total count + * Requested profile pictures (in up to 4 sizes each) * - * @return mixed|null - */ - public function getTotalCount() - { - return $this->total_count; - } - - /** - * Get photos + * This method overrides the default getPhotos method and returns a nice array * * @return array */ public function getPhotos() { - return $this->photos; + $all_photos = []; + + if ($these_photos = $this->getProperty('photos')) { + foreach ($these_photos as $photos) { + $new_photos = []; + foreach ($photos as $photo) { + $new_photos[] = new PhotoSize($photo); + } + $all_photos[] = $new_photos; + } + } + + return $all_photos; } } diff --git a/src/Entities/Venue.php b/src/Entities/Venue.php index 3dc6cc708..46069c0b5 100644 --- a/src/Entities/Venue.php +++ b/src/Entities/Venue.php @@ -10,135 +10,25 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Venue + * + * @link https://core.telegram.org/bots/api#venue + * + * @method Location getLocation() Venue location + * @method string getTitle() Name of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareId() Optional. Foursquare identifier of the venue + */ class Venue extends Entity { /** - * @var \Longman\TelegramBot\Entities\Location - */ - protected $location; - - /** - * @var mixed|null - */ - protected $latitude; - - /** - * @var mixed|null - */ - protected $longitude; - - /** - * @var mixed|null - */ - protected $title; - - /** - * @var mixed|null - */ - protected $address; - - /** - * @var mixed|null - */ - protected $foursquare_id; - - /** - * Venue constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->location = isset($data['location']) ? $data['location'] : null; - $this->latitude = isset($data['latitude']) ? $data['latitude'] : null; - $this->longitude = isset($data['longitude']) ? $data['longitude'] : null; - - // Venue can either contain location object or just latitude and longitude fields, check accordingly - if (!empty($this->location)) { - $this->location = new Location($this->location); - } else { - if (empty($this->latitude)) { - throw new TelegramException('latitude is empty!'); - } - - if (empty($this->longitude)) { - throw new TelegramException('longitude is empty!'); - } - } - - $this->title = isset($data['title']) ? $data['title'] : null; - if (empty($this->title)) { - throw new TelegramException('title is empty!'); - } - - $this->address = isset($data['address']) ? $data['address'] : null; - if (empty($this->address)) { - throw new TelegramException('address is empty!'); - } - - $this->foursquare_id = isset($data['foursquare_id']) ? $data['foursquare_id'] : null; - } - - /** - * Get location - * - * @return \Longman\TelegramBot\Entities\Location - */ - public function getLocation() - { - return $this->location; - } - - /** - * Get longitude - * - * @return mixed|null - */ - public function getLongitude() - { - return $this->longitude; - } - - /** - * Get latitude - * - * @return mixed|null - */ - public function getLatitude() - { - return $this->latitude; - } - - /** - * Get title - * - * @return mixed|null - */ - public function getTitle() - { - return $this->title; - } - - /** - * Get address - * - * @return mixed|null - */ - public function getAddress() - { - return $this->address; - } - - /** - * Get forsquare id - * - * @return mixed|null + * {@inheritdoc} */ - public function getFoursquareId() + protected function subEntities() { - return $this->foursquare_id; + return [ + 'location' => Location::class, + ]; } } diff --git a/src/Entities/Video.php b/src/Entities/Video.php index a159b5753..4cdc6b810 100644 --- a/src/Entities/Video.php +++ b/src/Entities/Video.php @@ -10,149 +10,28 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Video + * + * @link https://core.telegram.org/bots/api#video + * + * @method string getFileId() Unique identifier for this file + * @method int getWidth() Video width as defined by sender + * @method int getHeight() Video height as defined by sender + * @method int getDuration() Duration of the video in seconds as defined by sender + * @method PhotoSize getThumb() Optional. Video thumbnail + * @method string getMimeType() Optional. Mime type of a file as defined by sender + * @method int getFileSize() Optional. File size + */ class Video extends Entity { /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $width; - - /** - * @var mixed|null - */ - protected $height; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var \Longman\TelegramBot\Entities\PhotoSize - */ - protected $thumb; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Video constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->width = isset($data['width']) ? $data['width'] : null; - if (empty($this->width)) { - throw new TelegramException('width is empty!'); - } - - $this->height = isset($data['height']) ? $data['height'] : null; - if (empty($this->height)) { - throw new TelegramException('height is empty!'); - } - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - $this->thumb = isset($data['thumb']) ? $data['thumb'] : null; - if (empty($this->thumb)) { - throw new TelegramException('thumb is empty!'); - } - $this->thumb = new PhotoSize($this->thumb); - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get width - * - * @return mixed|null - */ - public function getWidth() - { - return $this->width; - } - - /** - * Get height - * - * @return mixed|null - */ - public function getHeight() - { - return $this->height; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get thumb - * - * @return \Longman\TelegramBot\Entities\PhotoSize - */ - public function getThumb() - { - return $this->thumb; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - - /** - * Get file size - * - * @return mixed|null + * {@inheritdoc} */ - public function getFileSize() + protected function subEntities() { - return $this->file_size; + return [ + 'thumb' => PhotoSize::class, + ]; } } diff --git a/src/Entities/Voice.php b/src/Entities/Voice.php index b38cefb93..0b422f8c1 100644 --- a/src/Entities/Voice.php +++ b/src/Entities/Voice.php @@ -10,89 +10,17 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class Voice + * + * @link https://core.telegram.org/bots/api#voice + * + * @method string getFileId() Unique identifier for this file + * @method int getDuration() Duration of the audio in seconds as defined by sender + * @method string getMimeType() Optional. MIME type of the file as defined by sender + * @method int getFileSize() Optional. File size + */ class Voice extends Entity { - /** - * @var mixed|null - */ - protected $file_id; - - /** - * @var mixed|null - */ - protected $duration; - - /** - * @var mixed|null - */ - protected $mime_type; - - /** - * @var mixed|null - */ - protected $file_size; - - /** - * Voice constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data) - { - $this->file_id = isset($data['file_id']) ? $data['file_id'] : null; - if (empty($this->file_id)) { - throw new TelegramException('file_id is empty!'); - } - - $this->duration = isset($data['duration']) ? $data['duration'] : null; - if ($this->duration === '' || $this->duration === null) { - throw new TelegramException('duration is empty!'); - } - - $this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null; - $this->file_size = isset($data['file_size']) ? $data['file_size'] : null; - } - - /** - * Get file id - * - * @return mixed|null - */ - public function getFileId() - { - return $this->file_id; - } - - /** - * Get duration - * - * @return mixed|null - */ - public function getDuration() - { - return $this->duration; - } - - /** - * Get mime type - * - * @return mixed|null - */ - public function getMimeType() - { - return $this->mime_type; - } - /** - * Get file size - * - * @return mixed|null - */ - public function getFileSize() - { - return $this->file_size; - } } From 3791a65b3c8900d70fb0d34b5be9614099f27563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Fri, 2 Sep 2016 13:23:25 +0200 Subject: [PATCH 02/31] Update all InlineQueryResult classes with getters and setters. --- src/Entities/Entity.php | 2 +- src/Entities/InlineQuery/InlineEntity.php | 4 +- .../InlineQuery/InlineQueryResultAudio.php | 2 +- .../InlineQueryResultCachedAudio.php | 4 +- .../InlineQueryResultCachedDocument.php | 2 +- .../InlineQueryResultCachedGif.php | 4 +- .../InlineQueryResultCachedMpeg4Gif.php | 12 ++--- .../InlineQueryResultCachedPhoto.php | 37 +++++++++++++ .../InlineQueryResultCachedSticker.php | 28 ++++++++++ .../InlineQueryResultCachedVideo.php | 37 +++++++++++++ .../InlineQueryResultCachedVoice.php | 31 +++++++++++ .../InlineQuery/InlineQueryResultContact.php | 43 +++++++++++++++ .../InlineQuery/InlineQueryResultDocument.php | 49 +++++++++++++++++ .../InlineQuery/InlineQueryResultGif.php | 43 +++++++++++++++ .../InlineQuery/InlineQueryResultLocation.php | 43 +++++++++++++++ .../InlineQuery/InlineQueryResultMpeg4Gif.php | 43 +++++++++++++++ .../InlineQuery/InlineQueryResultPhoto.php | 46 ++++++++++++++++ .../InlineQuery/InlineQueryResultVenue.php | 49 +++++++++++++++++ .../InlineQuery/InlineQueryResultVideo.php | 52 +++++++++++++++++++ .../InlineQuery/InlineQueryResultVoice.php | 34 ++++++++++++ 20 files changed, 551 insertions(+), 14 deletions(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index c00b7b240..f5a5f2a30 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -18,7 +18,7 @@ * * This is the base class for all entities. */ -class Entity +abstract class Entity { /** * @var string diff --git a/src/Entities/InlineQuery/InlineEntity.php b/src/Entities/InlineQuery/InlineEntity.php index 24c03284e..9188d51c5 100644 --- a/src/Entities/InlineQuery/InlineEntity.php +++ b/src/Entities/InlineQuery/InlineEntity.php @@ -29,8 +29,10 @@ public function __call($method, $args) //Convert method to snake_case (which is the name of the property) $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); $this->$property_name = $args[0]; + + return $this; } - return $this; + return parent::__call($method, $args); } } diff --git a/src/Entities/InlineQuery/InlineQueryResultAudio.php b/src/Entities/InlineQuery/InlineQueryResultAudio.php index 00890d77b..1fb4cb661 100644 --- a/src/Entities/InlineQuery/InlineQueryResultAudio.php +++ b/src/Entities/InlineQuery/InlineQueryResultAudio.php @@ -45,7 +45,7 @@ * @method $this setPerformer(string $performer) Optional. Performer * @method $this setAudioDuration(int $audio_duration) Optional. Audio duration in seconds * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message - * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio */ class InlineQueryResultAudio extends InlineEntity { diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php index 062a93601..e7c10a9a5 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php @@ -35,8 +35,8 @@ * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setAudioFileId(string $audio_file_id) A valid file identifier for the audio file - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message - * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio */ class InlineQueryResultCachedAudio extends InlineEntity { diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php index 6ddcfb6a6..e493aeefb 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php @@ -44,7 +44,7 @@ * @method $this setDocumentFileId(string $document_file_id) A valid file identifier for the file * @method $this setDescription(string $description) Optional. Short description of the result * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file */ class InlineQueryResultCachedDocument extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php index 5923ff9ef..de214cd68 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php @@ -41,8 +41,8 @@ * @method $this setGifFileId(string $gif_file_id) A valid file identifier for the GIF file * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message - * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation */ class InlineQueryResultCachedGif extends InlineEntity { diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php index 13b16ec5d..10aa53df8 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php @@ -29,11 +29,11 @@ * ]; * * - * @method String getType() Type of the result, must be mpeg4_gif - * @method String getId() Unique identifier for this result, 1-64 bytes - * @method String getMpeg4FileId() A valid file identifier for the MP4 file - * @method String getTitle() Optional. Title for the result - * @method String getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method string getType() Type of the result, must be mpeg4_gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getMpeg4FileId() A valid file identifier for the MP4 file + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation * @@ -41,7 +41,7 @@ * @method $this setMpeg4FileId(string $mpeg4_file_id) A valid file identifier for the MP4 file * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation */ class InlineQueryResultCachedMpeg4Gif extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php index 41041680b..042c07954 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php @@ -10,6 +10,43 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedPhoto + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedphoto + * + * + * $data = [ + * 'id' => '', + * 'photo_file_id' => '', + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be photo + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getPhotoFileId() A valid file identifier of the photo + * @method string getTitle() Optional. Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setPhotoFileId(string $photo_file_id) A valid file identifier of the photo + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ class InlineQueryResultCachedPhoto extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php index d46caac40..d984dd655 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php @@ -10,6 +10,34 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedSticker + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedsticker + * + * + * $data = [ + * 'id' => '', + * 'sticker_file_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be sticker + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getStickerFileId() A valid file identifier of the sticker + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the sticker + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setStickerFileId(string $sticker_file_id) A valid file identifier of the sticker + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the sticker + */ class InlineQueryResultCachedSticker extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php index 414ab0266..56c3f9039 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php @@ -10,6 +10,43 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedVideo + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedvideo + * + * + * $data = [ + * 'id' => '', + * 'video_file_id' => '', + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be video + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVideoFileId() A valid file identifier for the video file + * @method string getTitle() Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the video to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVideoFileId(string $video_file_id) A valid file identifier for the video file + * @method $this setTitle(string $title) Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the video to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video + */ class InlineQueryResultCachedVideo extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php index 6b947bb8e..4305e8b9a 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php @@ -10,6 +10,37 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultCachedVoice + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcachedvoice + * + * + * $data = [ + * 'id' => '', + * 'voice_file_id' => '', + * 'title' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be voice + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVoiceFileId() A valid file identifier for the voice message + * @method string getTitle() Voice message title + * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice message + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVoiceFileId(string $voice_file_id) A valid file identifier for the voice message + * @method $this setTitle(string $title) Voice message title + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice message + */ class InlineQueryResultCachedVoice extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultContact.php b/src/Entities/InlineQuery/InlineQueryResultContact.php index c0198c687..b46d2ad57 100644 --- a/src/Entities/InlineQuery/InlineQueryResultContact.php +++ b/src/Entities/InlineQuery/InlineQueryResultContact.php @@ -10,6 +10,49 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultContact + * + * @link https://core.telegram.org/bots/api#inlinequeryresultcontact + * + * + * $data = [ + * 'id' => '', + * 'phone_number' => '', + * 'first_name' => '', + * 'last_name' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be contact + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method string getPhoneNumber() Contact's phone number + * @method string getFirstName() Contact's first name + * @method string getLastName() Optional. Contact's last name + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the contact + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setPhoneNumber(string $phone_number) Contact's phone number + * @method $this setFirstName(string $first_name) Contact's first name + * @method $this setLastName(string $last_name) Optional. Contact's last name + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the contact + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ class InlineQueryResultContact extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultDocument.php b/src/Entities/InlineQuery/InlineQueryResultDocument.php index c08c63374..22e2be560 100644 --- a/src/Entities/InlineQuery/InlineQueryResultDocument.php +++ b/src/Entities/InlineQuery/InlineQueryResultDocument.php @@ -10,6 +10,55 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultDocument + * + * @link https://core.telegram.org/bots/api#inlinequeryresultdocument + * + * + * $data = [ + * 'id' => '', + * 'title' => '', + * 'caption' => '', + * 'document_url' => '', + * 'mime_type' => '', + * 'description' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be document + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getTitle() Title for the result + * @method string getCaption() Optional. Caption of the document to be sent, 0-200 characters + * @method string getDocumentUrl() A valid URL for the file + * @method string getMimeType() Mime type of the content of the file, either “application/pdf” or “application/zip” + * @method string getDescription() Optional. Short description of the result + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file + * @method string getThumbUrl() Optional. URL of the thumbnail (jpeg only) for the file + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setTitle(string $title) Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters + * @method $this setDocumentUrl(string $document_url) A valid URL for the file + * @method $this setMimeType(string $mime_type) Mime type of the content of the file, either “application/pdf” or “application/zip” + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file + * @method $this setThumbUrl(string $thumb_url) Optional. URL of the thumbnail (jpeg only) for the file + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ class InlineQueryResultDocument extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultGif.php b/src/Entities/InlineQuery/InlineQueryResultGif.php index 8389d98ab..4bff65617 100644 --- a/src/Entities/InlineQuery/InlineQueryResultGif.php +++ b/src/Entities/InlineQuery/InlineQueryResultGif.php @@ -10,6 +10,49 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultGif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultgif + * + * + * $data = [ + * 'id' => '', + * 'gif_url' => '', + * 'gif_width' => 30, + * 'gif_height' => 30, + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getGifUrl() A valid URL for the GIF file. File size must not exceed 1MB + * @method int getGifWidth() Optional. Width of the GIF + * @method int getGifHeight() Optional. Height of the GIF + * @method string getThumbUrl() URL of the static thumbnail for the result (jpeg or gif) + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setGifUrl(string $gif_url) A valid URL for the GIF file. File size must not exceed 1MB + * @method $this setGifWidth(int $gif_width) Optional. Width of the GIF + * @method $this setGifHeight(int $gif_height) Optional. Height of the GIF + * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail for the result (jpeg or gif) + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation + */ class InlineQueryResultGif extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultLocation.php b/src/Entities/InlineQuery/InlineQueryResultLocation.php index 27b5b6c80..f1d912493 100644 --- a/src/Entities/InlineQuery/InlineQueryResultLocation.php +++ b/src/Entities/InlineQuery/InlineQueryResultLocation.php @@ -10,6 +10,49 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultLocation + * + * @link https://core.telegram.org/bots/api#inlinequeryresultlocation + * + * + * $data = [ + * 'id' => '', + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be location + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method float getLatitude() Location latitude in degrees + * @method float getLongitude() Location longitude in degrees + * @method string getTitle() Location title + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the location + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setLatitude(float $latitude) Location latitude in degrees + * @method $this setLongitude(float $longitude) Location longitude in degrees + * @method $this setTitle(string $title) Location title + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the location + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ class InlineQueryResultLocation extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php index 3d968d611..08a61dc61 100644 --- a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php +++ b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php @@ -10,6 +10,49 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultMpeg4Gif + * + * @link https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif + * + * + * $data = [ + * 'id' => '', + * 'mpeg4_url' => '', + * 'mpeg4_width' => 30, + * 'mpeg4_height' => 30, + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be mpeg4_gif + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getMpeg4Url() A valid URL for the MP4 file. File size must not exceed 1MB + * @method int getMpeg4Width() Optional. Video width + * @method int getMpeg4Height() Optional. Video height + * @method string getThumbUrl() URL of the static thumbnail (jpeg or gif) for the result + * @method string getTitle() Optional. Title for the result + * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setMpeg4Url(string $mpeg4_url) A valid URL for the MP4 file. File size must not exceed 1MB + * @method $this setMpeg4Width(int $mpeg4_width) Optional. Video width + * @method $this setMpeg4Height(int $mpeg4_height) Optional. Video height + * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail (jpeg or gif) for the result + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation + */ class InlineQueryResultMpeg4Gif extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultPhoto.php b/src/Entities/InlineQuery/InlineQueryResultPhoto.php index 1f9b837f8..dd310f206 100644 --- a/src/Entities/InlineQuery/InlineQueryResultPhoto.php +++ b/src/Entities/InlineQuery/InlineQueryResultPhoto.php @@ -10,6 +10,52 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultPhoto + * + * @link https://core.telegram.org/bots/api#inlinequeryresultphoto + * + * + * $data = [ + * 'id' => '', + * 'photo_url' => '', + * 'thumb_url' => '', + * 'photo_width' => 30, + * 'photo_height' => 30, + * 'title' => '', + * 'description' => '', + * 'caption' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be photo + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getPhotoUrl() A valid URL of the photo. Photo must be in jpeg format. Photo size must not exceed 5MB + * @method string getThumbUrl() URL of the thumbnail for the photo + * @method int getPhotoWidth() Optional. Width of the photo + * @method int getPhotoHeight() Optional. Height of the photo + * @method string getTitle() Optional. Title for the result + * @method string getDescription() Optional. Short description of the result + * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setPhotoUrl(string $photo_url) A valid URL of the photo. Photo must be in jpeg format. Photo size must not exceed 5MB + * @method $this setThumbUrl(string $thumb_url) URL of the thumbnail for the photo + * @method $this setPhotoWidth(int $photo_width) Optional. Width of the photo + * @method $this setPhotoHeight(int $photo_height) Optional. Height of the photo + * @method $this setTitle(string $title) Optional. Title for the result + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo + */ class InlineQueryResultPhoto extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultVenue.php b/src/Entities/InlineQuery/InlineQueryResultVenue.php index 4d7a58255..8e4fdbed8 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVenue.php +++ b/src/Entities/InlineQuery/InlineQueryResultVenue.php @@ -10,6 +10,55 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVenue + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvenue + * + * + * $data = [ + * 'id' => '', + * 'latitude' => 36.0338, + * 'longitude' => 71.8601, + * 'title' => '', + * 'address' => '', + * 'foursquare_id' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * 'thumb_url' => '', + * 'thumb_width' => 30, + * 'thumb_height' => 30, + * ]; + * + * + * @method string getType() Type of the result, must be venue + * @method string getId() Unique identifier for this result, 1-64 Bytes + * @method float getLatitude() Latitude of the venue location in degrees + * @method float getLongitude() Longitude of the venue location in degrees + * @method string getTitle() Title of the venue + * @method string getAddress() Address of the venue + * @method string getFoursquareId() Optional. Foursquare identifier of the venue if known + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the venue + * @method string getThumbUrl() Optional. Url of the thumbnail for the result + * @method int getThumbWidth() Optional. Thumbnail width + * @method int getThumbHeight() Optional. Thumbnail height + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes + * @method $this setLatitude(float $latitude) Latitude of the venue location in degrees + * @method $this setLongitude(float $longitude) Longitude of the venue location in degrees + * @method $this setTitle(string $title) Title of the venue + * @method $this setAddress(string $address) Address of the venue + * @method $this setFoursquareId(string $foursquare_id) Optional. Foursquare identifier of the venue if known + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the venue + * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result + * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width + * @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height + */ class InlineQueryResultVenue extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultVideo.php b/src/Entities/InlineQuery/InlineQueryResultVideo.php index 50c7d0ff1..a032ff30b 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVideo.php +++ b/src/Entities/InlineQuery/InlineQueryResultVideo.php @@ -10,6 +10,58 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVideo + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvideo + * + * + * $data = [ + * 'id' => '', + * 'video_url' => '', + * 'mime_type' => '', + * 'thumb_url' => '', + * 'title' => '', + * 'caption' => '', + * 'video_width' => 30, + * 'video_height' => 30, + * 'video_duration' => 123, + * 'description' => '', + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be video + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVideoUrl() A valid URL for the embedded video player or video file + * @method string getMimeType() Mime type of the content of video url, “text/html” or “video/mp4” + * @method string getThumbUrl() URL of the thumbnail (jpeg only) for the video + * @method string getTitle() Title for the result + * @method string getCaption() Optional. Caption of the video to be sent, 0-200 characters + * @method int getVideoWidth() Optional. Video width + * @method int getVideoHeight() Optional. Video height + * @method int getVideoDuration() Optional. Video duration in seconds + * @method string getDescription() Optional. Short description of the result + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVideoUrl(string $video_url) A valid URL for the embedded video player or video file + * @method $this setMimeType(string $mime_type) Mime type of the content of video url, “text/html” or “video/mp4” + * @method $this setThumbUrl(string $thumb_url) URL of the thumbnail (jpeg only) for the video + * @method $this setTitle(string $title) Title for the result + * @method $this setCaption(string $caption) Optional. Caption of the video to be sent, 0-200 characters + * @method $this setVideoWidth(int $video_width) Optional. Video width + * @method $this setVideoHeight(int $video_height) Optional. Video height + * @method $this setVideoDuration(int $video_duration) Optional. Video duration in seconds + * @method $this setDescription(string $description) Optional. Short description of the result + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video + */ class InlineQueryResultVideo extends InlineEntity { /** diff --git a/src/Entities/InlineQuery/InlineQueryResultVoice.php b/src/Entities/InlineQuery/InlineQueryResultVoice.php index eaf971987..6220c2961 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVoice.php +++ b/src/Entities/InlineQuery/InlineQueryResultVoice.php @@ -10,6 +10,40 @@ namespace Longman\TelegramBot\Entities\InlineQuery; +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; + +/** + * Class InlineQueryResultVoice + * + * @link https://core.telegram.org/bots/api#inlinequeryresultvoice + * + * + * $data = [ + * 'id' => '', + * 'voice_url' => '', + * 'title' => '', + * 'voice_duration' => 123, + * 'reply_markup' => , + * 'input_message_content' => , + * ]; + * + * + * @method string getType() Type of the result, must be voice + * @method string getId() Unique identifier for this result, 1-64 bytes + * @method string getVoiceUrl() A valid URL for the voice recording + * @method string getTitle() Recording title + * @method int getVoiceDuration() Optional. Recording duration in seconds + * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice recording + * + * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes + * @method $this setVoiceUrl(string $voice_url) A valid URL for the voice recording + * @method $this setTitle(string $title) Recording title + * @method $this setVoiceDuration(int $voice_duration) Optional. Recording duration in seconds + * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice recording + */ class InlineQueryResultVoice extends InlineEntity { /** From 95867a6e8fb8220d05512bede6ecf3811a7d6952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Fri, 2 Sep 2016 13:30:08 +0200 Subject: [PATCH 03/31] Update InlinequeryCommand SystemCommand with new classes. --- src/Commands/SystemCommands/InlinequeryCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Commands/SystemCommands/InlinequeryCommand.php b/src/Commands/SystemCommands/InlinequeryCommand.php index 65942aa1f..71d3678b0 100644 --- a/src/Commands/SystemCommands/InlinequeryCommand.php +++ b/src/Commands/SystemCommands/InlinequeryCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\SystemCommands; use Longman\TelegramBot\Commands\SystemCommand; -use Longman\TelegramBot\Entities\InlineQueryResultArticle; -use Longman\TelegramBot\Entities\InputTextMessageContent; +use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle; +use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent; use Longman\TelegramBot\Request; /** @@ -39,6 +39,7 @@ class InlinequeryCommand extends SystemCommand * Command execute method * * @return mixed + * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() { From da0a94d9399db930f077ac4f3015e012af865829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Fri, 2 Sep 2016 13:30:31 +0200 Subject: [PATCH 04/31] Update entity tests. --- .../unit/Entities/InlineKeyboardButtonTest.php | 2 +- tests/unit/Entities/ServerResponseTest.php | 18 +++++++++++++----- tests/unit/Entities/UpdateTest.php | 5 ----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/unit/Entities/InlineKeyboardButtonTest.php b/tests/unit/Entities/InlineKeyboardButtonTest.php index f682abac3..92ae6da3e 100644 --- a/tests/unit/Entities/InlineKeyboardButtonTest.php +++ b/tests/unit/Entities/InlineKeyboardButtonTest.php @@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase { /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage text is empty! + * @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query! */ public function testInlineKeyboardButtonNoTextFail() { diff --git a/tests/unit/Entities/ServerResponseTest.php b/tests/unit/Entities/ServerResponseTest.php index df4c2d10b..8f27a268f 100644 --- a/tests/unit/Entities/ServerResponseTest.php +++ b/tests/unit/Entities/ServerResponseTest.php @@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Tests\Unit; +use Longman\TelegramBot\Entities\Message; use \Longman\TelegramBot\Entities\ServerResponse; use \Longman\TelegramBot\Request; @@ -57,10 +58,10 @@ public function testSendMessageOk() { $this->assertEquals('1234', $server_result->getMessageId()); $this->assertEquals('123456789', $server_result->getFrom()->getId()); $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); + $this->assertEquals('namebot', $server_result->getFrom()->getUsername()); $this->assertEquals('123456789', $server_result->getChat()->getId()); $this->assertEquals('john', $server_result->getChat()->getFirstName()); - $this->assertEquals('Mjohn', $server_result->getChat()->getUserName()); + $this->assertEquals('Mjohn', $server_result->getChat()->getUsername()); $this->assertEquals('1441378360', $server_result->getDate()); $this->assertEquals('hello', $server_result->getText()); //... they are not finished... @@ -221,9 +222,11 @@ public function testGetUserProfilePhotos() $result = $this->getUserProfilePhotos(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); $server_result = $this->server->getResult(); + $photos = $server_result->getPhotos(); //Photo count + $this->assertEquals(3, $server_result->getTotalCount()); $this->assertCount(3, $photos); //Photo size count $this->assertCount(3, $photos[0]); @@ -252,7 +255,8 @@ public function testGetFile() $this->assertInstanceOf('\Longman\TelegramBot\Entities\File', $this->server->getResult()); } - public function testSetGeneralTestFakeResponse() { + public function testSetGeneralTestFakeResponse() + { //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); @@ -267,6 +271,8 @@ public function testSetGeneralTestFakeResponse() { $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']); $this->server = new ServerResponse($fake_response, 'testbot'); + + /** @var Message $server_result */ $server_result = $this->server->getResult(); $this->assertTrue($this->server->isOk()); @@ -278,14 +284,16 @@ public function testSetGeneralTestFakeResponse() { $this->assertEquals('1234', $server_result->getMessageId()); $this->assertEquals('1441378360', $server_result->getDate()); $this->assertEquals('hello', $server_result->getText()); + //Message //User $this->assertEquals('123456789', $server_result->getFrom()->getId()); $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); + $this->assertEquals('namebot', $server_result->getFrom()->getUsername()); + //Message //Chat $this->assertEquals('123456789', $server_result->getChat()->getId()); $this->assertEquals('', $server_result->getChat()->getFirstName()); - $this->assertEquals('', $server_result->getChat()->getUserName()); + $this->assertEquals('', $server_result->getChat()->getUsername()); //... they are not finished... } diff --git a/tests/unit/Entities/UpdateTest.php b/tests/unit/Entities/UpdateTest.php index c2717b679..dacf9fff0 100644 --- a/tests/unit/Entities/UpdateTest.php +++ b/tests/unit/Entities/UpdateTest.php @@ -21,11 +21,6 @@ */ class UpdateTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Update - */ - private $update; - public function testUpdateCast() { $json = ' From d30757a8c84d231f1bc672ef9d743e4e5d96b23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 21 Sep 2016 13:00:32 +0200 Subject: [PATCH 05/31] If a pretty object array is empty, return null instead of an empty array. --- src/Entities/Message.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 6eadb1aa5..2922573dd 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -103,11 +103,13 @@ public function __construct(array $data, $bot_name = '') * This method overrides the default getPhoto method * and returns a nice array of PhotoSize objects. * - * @return array + * @return null|array */ public function getPhoto() { - return $this->makePrettyObjectArray(PhotoSize::class, 'photo'); + $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo'); + + return empty($pretty_array) ? null : $pretty_array; } /** @@ -116,11 +118,13 @@ public function getPhoto() * This method overrides the default getNewChatPhoto method * and returns a nice array of PhotoSize objects. * - * @return array + * @return null|array */ public function getNewChatPhoto() { - return $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); + $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); + + return empty($pretty_array) ? null : $pretty_array; } /** @@ -129,11 +133,13 @@ public function getNewChatPhoto() * This method overrides the default getEntities method * and returns a nice array of MessageEntity objects. * - * @return array + * @return null|array */ public function getEntities() { - return $this->makePrettyObjectArray(MessageEntity::class, 'entities'); + $pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities'); + + return empty($pretty_array) ? null : $pretty_array; } /** From bf5c06480932c7bf5d79f12ed21bdca88afcd54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 21 Sep 2016 13:06:49 +0200 Subject: [PATCH 06/31] Add initial rewrites of keyboard entities. --- src/Entities/ForceReply.php | 28 ++++++------- src/Entities/InlineKeyboardButton.php | 2 + src/Entities/InlineKeyboardMarkup.php | 43 +++++++++++--------- src/Entities/KeyboardButton.php | 39 ++++++------------ src/Entities/ReplyKeyboardHide.php | 28 ++++++------- src/Entities/ReplyKeyboardMarkup.php | 57 ++++++++++++--------------- 6 files changed, 92 insertions(+), 105 deletions(-) diff --git a/src/Entities/ForceReply.php b/src/Entities/ForceReply.php index 8ca0b2b23..6406cbeaa 100644 --- a/src/Entities/ForceReply.php +++ b/src/Entities/ForceReply.php @@ -12,26 +12,26 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ForceReply + * + * @link https://core.telegram.org/bots/api#forcereply + * + * @method bool getForceReply() Shows reply interface to the user, as if they manually selected the bot‘s message and tapped 'Reply' + * @method bool getSelective() Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. + */ class ForceReply extends Entity { - /** - * @var bool - */ - protected $force_reply; - - /** - * @var bool|mixed - */ - protected $selective; - /** * ForceReply constructor. * - * @param array|null $data + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function __construct(array $data = null) + public function __construct(array $data = []) { - $this->force_reply = true; - $this->selective = isset($data['selective']) ? $data['selective'] : false; + $data['force_reply'] = true; + parent::__construct($data); } } diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index ae1f0da8b..c5f8e689a 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -30,11 +30,13 @@ class InlineKeyboardButton extends Entity protected function validate() { $num_params = 0; + foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { if (!empty($this->getProperty($param))) { $num_params++; } } + if ($num_params !== 1) { throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!'); } diff --git a/src/Entities/InlineKeyboardMarkup.php b/src/Entities/InlineKeyboardMarkup.php index 799ebcf9b..d74759e61 100644 --- a/src/Entities/InlineKeyboardMarkup.php +++ b/src/Entities/InlineKeyboardMarkup.php @@ -16,35 +16,40 @@ * Class InlineKeyboardMarkup * * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup + * + * @method InlineKeyboardButton[][] getInlineKeyboard() Array of button rows, each represented by an Array of InlineKeyboardButton objects */ class InlineKeyboardMarkup extends Entity { /** - * @var array + * {@inheritdoc} */ - protected $inline_keyboard; + protected function subEntities() + { + return [ + 'inline_keyboard' => InlineKeyboardButton::class, + ]; + } /** - * InlineKeyboardMarkup constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException + * {@inheritdoc} */ - public function __construct($data = array()) + protected function validate() { - if (isset($data['inline_keyboard'])) { - if (is_array($data['inline_keyboard'])) { - foreach ($data['inline_keyboard'] as $item) { - if (!is_array($item)) { - throw new TelegramException('Inline Keyboard subfield is not an array!'); - } - } - $this->inline_keyboard = $data['inline_keyboard']; - } else { - throw new TelegramException('Inline Keyboard field is not an array!'); - } - } else { + $inline_keyboard = $this->getProperty('inline_keyboard'); + + if ($inline_keyboard === null) { throw new TelegramException('Inline Keyboard field is empty!'); } + + if (!is_array($inline_keyboard)) { + throw new TelegramException('Inline Keyboard field is not an array!'); + } + + foreach ($inline_keyboard as $item) { + if (!is_array($item)) { + throw new TelegramException('Inline Keyboard subfield is not an array!'); + } + } } } diff --git a/src/Entities/KeyboardButton.php b/src/Entities/KeyboardButton.php index 82463a7ae..dd7a199f3 100644 --- a/src/Entities/KeyboardButton.php +++ b/src/Entities/KeyboardButton.php @@ -12,37 +12,24 @@ use Longman\TelegramBot\Exception\TelegramException; +/** + * Class KeyboardButton + * + * @link https://core.telegram.org/bots/api#keyboardbutton + * + * @method string getText() Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed + * @method bool getRequestContact() Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only + * @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only + */ class KeyboardButton extends Entity { /** - * @var mixed|null - */ - protected $text; - - /** - * @var mixed|null + * {@inheritdoc} */ - protected $request_contact; - - /** - * @var mixed|null - */ - protected $request_location; - - /** - * KeyboardButton constructor. - * - * @param array $data - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data = []) + protected function validate() { - $this->text = isset($data['text']) ? $data['text'] : null; - if (empty($this->text)) { - throw new TelegramException('text is empty!'); + if ($this->getRequestContact() && $this->getRequestLocation()) { + throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); } - - $this->request_contact = isset($data['request_contact']) ? $data['request_contact'] : null; - $this->request_location = isset($data['request_location']) ? $data['request_location'] : null; } } diff --git a/src/Entities/ReplyKeyboardHide.php b/src/Entities/ReplyKeyboardHide.php index f3c3e88c4..0c7de70af 100644 --- a/src/Entities/ReplyKeyboardHide.php +++ b/src/Entities/ReplyKeyboardHide.php @@ -12,26 +12,26 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ReplyKeyboardHide + * + * @link https://core.telegram.org/bots/api#replykeyboardhide + * + * @method bool getHideKeyboard() Requests clients to hide the custom keyboard + * @method bool getSelective() Optional. Use this parameter if you want to hide keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. + */ class ReplyKeyboardHide extends Entity { - /** - * @var bool - */ - protected $hide_keyboard; - - /** - * @var bool - */ - protected $selective; - /** * ReplyKeyboardHide constructor. * - * @param array|null $data + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function __construct(array $data = null) + public function __construct(array $data = []) { - $this->hide_keyboard = true; - $this->selective = isset($data['selective']) ? $data['selective'] : false; + $data['hide_keyboard'] = true; + parent::__construct($data); } } diff --git a/src/Entities/ReplyKeyboardMarkup.php b/src/Entities/ReplyKeyboardMarkup.php index 5271ce66a..6153a82e9 100644 --- a/src/Entities/ReplyKeyboardMarkup.php +++ b/src/Entities/ReplyKeyboardMarkup.php @@ -14,55 +14,48 @@ use Longman\TelegramBot\Exception\TelegramException; + +/** + * Class ReplyKeyboardMarkup + * + * @link https://core.telegram.org/bots/api#replykeyboardmarkup + * + * @method KeyboardButton[][] getKeyboard() Array of button rows, each represented by an Array of KeyboardButton objects + * @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard. + * @method bool getOneTimeKeyboard() Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. + * @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. + */ class ReplyKeyboardMarkup extends Entity { /** - * @var array + * {@inheritdoc} */ - protected $keyboard; - - /** - * @var bool - */ - protected $resize_keyboard; - - /** - * @var bool - */ - protected $one_time_keyboard; - - /** - * @var bool - */ - protected $selective; + protected function subEntities() + { + return [ + 'keyboard' => KeyboardButton::class, + ]; + } /** - * ReplyKeyboardMarkup constructor. - * - * @param array $data - * - * @throws \Longman\TelegramBot\Exception\TelegramException + * {@inheritdoc} */ - public function __construct(array $data = []) + protected function validate() { - if (!isset($data['keyboard'])) { + $keyboard = $this->getProperty('keyboard'); + + if ($keyboard === null) { throw new TelegramException('Keyboard field is empty!'); } - if (!is_array($data['keyboard'])) { + if (!is_array($keyboard)) { throw new TelegramException('Keyboard field is not an array!'); } - foreach ($data['keyboard'] as $item) { + foreach ($keyboard as $item) { if (!is_array($item)) { throw new TelegramException('Keyboard subfield is not an array!'); } } - $this->keyboard = $data['keyboard']; - - //Set the object members from the passed data params - foreach (['resize_keyboard', 'one_time_keyboard', 'selective'] as $param) { - $this->$param = isset($data[$param]) ? (bool)$data[$param] : false; - } } } From 2711d62ab0aecf53f81648826a38f327ead1bd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Fri, 23 Sep 2016 16:13:06 +0200 Subject: [PATCH 07/31] First rewrite of the keyboard entities. Update all keyboard commands to follow new structure. --- examples/Commands/ForcereplyCommand.php | 9 +- examples/Commands/HidekeyboardCommand.php | 10 +- examples/Commands/InlinekeyboardCommand.php | 26 +-- examples/Commands/KeyboardCommand.php | 88 ++++------ src/Entities/Entity.php | 17 +- src/Entities/ForceReply.php | 37 ---- src/Entities/InlineKeyboard.php | 50 ++++++ src/Entities/InlineKeyboardButton.php | 37 +++- src/Entities/InlineKeyboardMarkup.php | 55 ------ src/Entities/InlineQuery/InlineEntity.php | 25 +-- src/Entities/Keyboard.php | 176 ++++++++++++++++++++ src/Entities/KeyboardButton.php | 44 +++++ src/Entities/ReplyKeyboardHide.php | 37 ---- src/Entities/ReplyKeyboardMarkup.php | 61 ------- 14 files changed, 381 insertions(+), 291 deletions(-) delete mode 100644 src/Entities/ForceReply.php create mode 100644 src/Entities/InlineKeyboard.php delete mode 100644 src/Entities/InlineKeyboardMarkup.php create mode 100644 src/Entities/Keyboard.php delete mode 100644 src/Entities/ReplyKeyboardHide.php delete mode 100644 src/Entities/ReplyKeyboardMarkup.php diff --git a/examples/Commands/ForcereplyCommand.php b/examples/Commands/ForcereplyCommand.php index d5b3151b4..23a96e12a 100644 --- a/examples/Commands/ForcereplyCommand.php +++ b/examples/Commands/ForcereplyCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\ForceReply; /** * User "/forcereply" command @@ -25,7 +25,7 @@ class ForceReplyCommand extends UserCommand protected $name = 'forcereply'; protected $description = 'Force reply with reply markup'; protected $usage = '/forcereply'; - protected $version = '0.0.6'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -33,13 +33,12 @@ class ForceReplyCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); + $chat_id = $this->getMessage()->getChat()->getId(); $data = [ 'chat_id' => $chat_id, 'text' => 'Write something:', - 'reply_markup' => new ForceReply(['selective' => false]), + 'reply_markup' => Keyboard::forceReply(), ]; return Request::sendMessage($data); diff --git a/examples/Commands/HidekeyboardCommand.php b/examples/Commands/HidekeyboardCommand.php index 269ffd492..a5abf1493 100644 --- a/examples/Commands/HidekeyboardCommand.php +++ b/examples/Commands/HidekeyboardCommand.php @@ -11,8 +11,7 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; /** @@ -26,7 +25,7 @@ class HidekeyboardCommand extends UserCommand protected $name = 'hidekeyboard'; protected $description = 'Hide the custom keyboard'; protected $usage = '/hidekeyboard'; - protected $version = '0.0.6'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -34,13 +33,12 @@ class HidekeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); + $chat_id = $this->getMessage()->getChat()->getId(); $data = [ 'chat_id' => $chat_id, 'text' => 'Keyboard Hidden', - 'reply_markup' => new ReplyKeyboardHide(['selective' => false]), + 'reply_markup' => Keyboard::hide(), ]; return Request::sendMessage($data); diff --git a/examples/Commands/InlinekeyboardCommand.php b/examples/Commands/InlinekeyboardCommand.php index 575c63f8c..1f60a2537 100644 --- a/examples/Commands/InlinekeyboardCommand.php +++ b/examples/Commands/InlinekeyboardCommand.php @@ -11,9 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; -use Longman\TelegramBot\Entities\InlineKeyboardButton; /** * User "/inlinekeyboard" command @@ -23,10 +22,10 @@ class InlinekeyboardCommand extends UserCommand /**#@+ * {@inheritdoc} */ - protected $name = 'Inlinekeyboard'; + protected $name = 'inlinekeyboard'; protected $description = 'Show inline keyboard'; protected $usage = '/inlinekeyboard'; - protected $version = '0.0.2'; + protected $version = '0.1.0'; /**#@-*/ /** @@ -34,17 +33,18 @@ class InlinekeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); + $chat_id = $this->getMessage()->getChat()->getId(); - $inline_keyboard = [ - new InlineKeyboardButton(['text' => 'inline', 'switch_inline_query' => 'true']), - new InlineKeyboardButton(['text' => 'callback', 'callback_data' => 'identifier']), - new InlineKeyboardButton(['text' => 'open url', 'url' => 'https://github.com/akalongman/php-telegram-bot']), - ]; - $data = [ - 'chat_id' => $message->getChat()->getId(), + $inline_keyboard = new InlineKeyboard([ + ['text' => 'inline', 'switch_inline_query' => 'true'], + ['text' => 'callback', 'callback_data' => 'identifier'], + ['text' => 'open url', 'url' => 'https://github.com/akalongman/php-telegram-bot'], + ]); + + $data = [ + 'chat_id' => $chat_id, 'text' => 'inline keyboard', - 'reply_markup' => new InlineKeyboardMarkup(['inline_keyboard' => [$inline_keyboard]]), + 'reply_markup' => $inline_keyboard, ]; return Request::sendMessage($data); diff --git a/examples/Commands/KeyboardCommand.php b/examples/Commands/KeyboardCommand.php index 6800977b5..95722fe21 100644 --- a/examples/Commands/KeyboardCommand.php +++ b/examples/Commands/KeyboardCommand.php @@ -11,8 +11,8 @@ namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; /** * User "/keyboard" command @@ -25,7 +25,7 @@ class KeyboardCommand extends UserCommand protected $name = 'keyboard'; protected $description = 'Show a custom keyboard with reply markup'; protected $usage = '/keyboard'; - protected $version = '0.1.0'; + protected $version = '0.2.0'; /**#@-*/ /** @@ -33,71 +33,55 @@ class KeyboardCommand extends UserCommand */ public function execute() { - $message = $this->getMessage(); - $chat_id = $message->getChat()->getId(); - - $data = [ - 'chat_id' => $chat_id, - 'text' => 'Press a Button:', - ]; + $chat_id = $this->getMessage()->getChat()->getId(); //Keyboard examples + /** @var Keyboard[] $keyboards */ $keyboards = []; //Example 0 - $keyboard = []; - $keyboard[] = ['7', '8', '9']; - $keyboard[] = ['4', '5', '6']; - $keyboard[] = ['1', '2', '3']; - $keyboard[] = [' ', '0', ' ']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['7', '8', '9'], + ['4', '5', '6'], + ['1', '2', '3'], + [' ', '0', ' '] + ); //Example 1 - $keyboard = []; - $keyboard[] = ['7', '8', '9', '+']; - $keyboard[] = ['4', '5', '6', '-']; - $keyboard[] = ['1', '2', '3', '*']; - $keyboard[] = [' ', '0', ' ', '/']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['7', '8', '9', '+'], + ['4', '5', '6', '-'], + ['1', '2', '3', '*'], + [' ', '0', ' ', '/'] + ); //Example 2 - $keyboard = []; - $keyboard[] = ['A']; - $keyboard[] = ['B']; - $keyboard[] = ['C']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard('A', 'B', 'C'); //Example 3 - $keyboard = []; - $keyboard[] = ['A']; - $keyboard[] = ['B']; - $keyboard[] = ['C', 'D']; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard( + ['text' => 'A'], + 'B', + ['C', 'D'] + ); //Example 4 (bots version 2.0) - $keyboard = []; - $keyboard[] = [ - [ - 'text' => 'Send my contact', - 'request_contact' => true, - ], - [ - 'text' => 'Send my location', - 'request_location' => true, - ], - ]; - $keyboards[] = $keyboard; + $keyboards[] = new Keyboard([ + ['text' => 'Send my contact', 'request_contact' => true], + ['text' => 'Send my location', 'request_location' => true], + ]); //Return a random keyboard. - $keyboard = $keyboards[mt_rand(0, count($keyboards) - 1)]; - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => $keyboard, - 'resize_keyboard' => true, - 'one_time_keyboard' => false, - 'selective' => false, - ] - ); + $keyboard = $keyboards[mt_rand(0, count($keyboards) - 1)] + ->setResizeKeyboard(true) + ->setOneTimeKeyboard(true) + ->setSelective(false); + + $data = [ + 'chat_id' => $chat_id, + 'text' => 'Press a Button:', + 'reply_markup' => $keyboard, + ]; return Request::sendMessage($data); } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index f5a5f2a30..a59f12836 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -11,6 +11,7 @@ namespace Longman\TelegramBot\Entities; use Exception; +use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; use ReflectionObject; /** @@ -198,7 +199,7 @@ public function getProperty($property, $default = null) } /** - * Return the variable for the called getter + * Return the variable for the called getter or magically set properties dynamically. * * @param $method * @param $args @@ -207,11 +208,12 @@ public function getProperty($property, $default = null) */ public function __call($method, $args) { + //Convert method to snake_case (which is the name of the property) + $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); + $action = substr($method, 0, 3); if ($action === 'get') { - //Convert method to snake_case (which is the name of the property) - $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); - $property = $this->getProperty($property_name); + $property = $this->getProperty($property_name); if ($property !== null) { //Get all sub-Entities of the current Entity @@ -223,6 +225,13 @@ public function __call($method, $args) return $property; } + } elseif ($action === 'set') { + // Limit setters to specific classes. + if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) { + $this->$property_name = $args[0]; + + return $this; + } } return null; diff --git a/src/Entities/ForceReply.php b/src/Entities/ForceReply.php deleted file mode 100644 index 6406cbeaa..000000000 --- a/src/Entities/ForceReply.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -/** - * Class ForceReply - * - * @link https://core.telegram.org/bots/api#forcereply - * - * @method bool getForceReply() Shows reply interface to the user, as if they manually selected the bot‘s message and tapped 'Reply' - * @method bool getSelective() Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. - */ -class ForceReply extends Entity -{ - /** - * ForceReply constructor. - * - * @param array $data - * - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data = []) - { - $data['force_reply'] = true; - parent::__construct($data); - } -} diff --git a/src/Entities/InlineKeyboard.php b/src/Entities/InlineKeyboard.php new file mode 100644 index 000000000..ce14023f6 --- /dev/null +++ b/src/Entities/InlineKeyboard.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities; + +use Longman\TelegramBot\Exception\TelegramException; + +/** + * Class InlineKeyboard + * + * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup + */ +class InlineKeyboard extends Keyboard +{ + /** + * {@inheritdoc} + */ + public function __construct($data = []) + { + $data = call_user_func_array([$this, 'createFromParams'], func_get_args()); + parent::__construct($data); + } + + /** + * {@inheritdoc} + */ + protected function validate() + { + $inline_keyboard = $this->getProperty('inline_keyboard'); + + if ($inline_keyboard !== null) { + if (!is_array($inline_keyboard)) { + throw new TelegramException('Inline Keyboard field is not an array!'); + } + + foreach ($inline_keyboard as $item) { + if (!is_array($item)) { + throw new TelegramException('Inline Keyboard subfield is not an array!'); + } + } + } + } +} diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index c5f8e689a..abae6f37c 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -21,9 +21,31 @@ * @method string getUrl() Optional. HTTP url to be opened when button is pressed * @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes * @method string getSwitchInlineQuery() Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted. + * + * @method $this setText(string $text) Label text on the button + * @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed + * @method $this setCallbackData(string $callback_data) Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes + * @method $this setSwitchInlineQuery(string $switch_inline_query) Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted. */ -class InlineKeyboardButton extends Entity +class InlineKeyboardButton extends KeyboardButton { + /** + * Check if the passed data array could be an InlineKeyboardButton. + * + * @param array $data + * + * @return bool + */ + public static function couldBe($data) + { + return is_array($data) && + array_key_exists('text', $data) && ( + array_key_exists('url', $data) || + array_key_exists('callback_data', $data) || + array_key_exists('switch_inline_query', $data) + ); + } + /** * {@inheritdoc} */ @@ -41,4 +63,17 @@ protected function validate() throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!'); } } + + /** + * {@inheritdoc} + */ + public function __call($method, $args) + { + // Only 1 of these can be set, so clear the others when setting a new one. + if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery'], true)) { + unset($this->url, $this->callback_data, $this->switch_inline_query); + } + + return parent::__call($method, $args); + } } diff --git a/src/Entities/InlineKeyboardMarkup.php b/src/Entities/InlineKeyboardMarkup.php deleted file mode 100644 index d74759e61..000000000 --- a/src/Entities/InlineKeyboardMarkup.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - -/** - * Class InlineKeyboardMarkup - * - * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup - * - * @method InlineKeyboardButton[][] getInlineKeyboard() Array of button rows, each represented by an Array of InlineKeyboardButton objects - */ -class InlineKeyboardMarkup extends Entity -{ - /** - * {@inheritdoc} - */ - protected function subEntities() - { - return [ - 'inline_keyboard' => InlineKeyboardButton::class, - ]; - } - - /** - * {@inheritdoc} - */ - protected function validate() - { - $inline_keyboard = $this->getProperty('inline_keyboard'); - - if ($inline_keyboard === null) { - throw new TelegramException('Inline Keyboard field is empty!'); - } - - if (!is_array($inline_keyboard)) { - throw new TelegramException('Inline Keyboard field is not an array!'); - } - - foreach ($inline_keyboard as $item) { - if (!is_array($item)) { - throw new TelegramException('Inline Keyboard subfield is not an array!'); - } - } - } -} diff --git a/src/Entities/InlineQuery/InlineEntity.php b/src/Entities/InlineQuery/InlineEntity.php index 9188d51c5..57f32db8d 100644 --- a/src/Entities/InlineQuery/InlineEntity.php +++ b/src/Entities/InlineQuery/InlineEntity.php @@ -12,27 +12,12 @@ use Longman\TelegramBot\Entities\Entity; +/** + * Class InlineEntity + * + * This is the base class for all inline entities. + */ abstract class InlineEntity extends Entity { - /** - * Magic method to set properties dynamically - * - * @param $method - * @param $args - * - * @return \Longman\TelegramBot\Entities\InlineQuery\InlineEntity - */ - public function __call($method, $args) - { - $action = substr($method, 0, 3); - if ($action === 'set') { - //Convert method to snake_case (which is the name of the property) - $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); - $this->$property_name = $args[0]; - - return $this; - } - return parent::__call($method, $args); - } } diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php new file mode 100644 index 000000000..9bf7bc628 --- /dev/null +++ b/src/Entities/Keyboard.php @@ -0,0 +1,176 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Written by Marco Boretto + */ + +namespace Longman\TelegramBot\Entities; + +use Longman\TelegramBot\Exception\TelegramException; + +/** + * Class ReplyKeyboardMarkup + * + * @link https://core.telegram.org/bots/api#replykeyboardmarkup + * + * @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard. + * @method bool getOneTimeKeyboard() Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. + * @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. + * + * @method $this setResizeKeyboard(bool $resize_keyboard) Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard. + * @method $this setOneTimeKeyboard(bool $one_time_keyboard) Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. + * @method $this setSelective(bool $selective) Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. + */ +class Keyboard extends Entity +{ + /** + * {@inheritdoc} + */ + public function __construct($data = []) + { + $data = call_user_func_array([$this, 'createFromParams'], func_get_args()); + parent::__construct($data); + } + + /** + * If this keyboard is an inline keyboard. + * + * @return bool + */ + public function isInlineKeyboard() + { + return $this instanceof InlineKeyboard; + } + + /** + * Get the proper keyboard button class for this keyboard. + * + * @return mixed + */ + public function getKeyboardButtonClass() + { + return $this->isInlineKeyboard() ? InlineKeyboardButton::class : KeyboardButton::class; + } + + /** + * Get the type of keyboard, either "inline_keyboard" or "keyboard". + * + * @return string + */ + public function getKeyboardType() + { + return $this->isInlineKeyboard() ? 'inline_keyboard' : 'keyboard'; + } + + /** + * If no explicit keyboard is passed, try to create one from the parameters. + * + * @return array + */ + protected function createFromParams() + { + /** @var KeyboardButton|InlineKeyboardButton $button_class */ + $button_class = $this->getKeyboardButtonClass(); + $keyboard_type = $this->getKeyboardType(); + + // If the inline_keyboard isn't set directly, try to create one from the arguments. + $data = func_get_arg(0); + if (!array_key_exists($keyboard_type, $data)) { + $new_keyboard = []; + foreach (func_get_args() as $row) { + if (is_array($row)) { + $new_row = []; + if ($button_class::couldBe($row)) { + $new_row[] = new $button_class($row); + } else { + foreach ($row as $button) { + if ($button instanceof $button_class) { + $new_row[] = $button; + } elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { + $new_row[] = new $button_class($button); + } + } + } + } else { + $new_row = [new $button_class($row)]; + } + $new_keyboard[] = $new_row; + } + + if (!empty($new_keyboard)) { + $data = [$keyboard_type => $new_keyboard]; + } + } + + return $data; + } + + /** + * Create a new row in keyboard and add buttons. + * + * @return $this + */ + public function addRow() + { + $keyboard_type = $this->getKeyboardType(); + $this->$keyboard_type[] = func_get_args(); + + return $this; + } + + /** + * {@inheritdoc} + */ + protected function validate() + { + $keyboard = $this->getProperty('keyboard'); + + if ($keyboard !== null) { + if (!is_array($keyboard)) { + throw new TelegramException('Keyboard field is not an array!'); + } + + foreach ($keyboard as $item) { + if (!is_array($item)) { + throw new TelegramException('Keyboard subfield is not an array!'); + } + } + } + } + + /** + * Hide the current custom keyboard and display the default letter-keyboard. + * + * @link https://core.telegram.org/bots/api#replykeyboardhide + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\Keyboard + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function hide(array $data = []) + { + return new static(array_merge(['keyboard' => null, 'hide_keyboard' => true, 'selective' => false], $data)); + } + + /** + * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). + * + * @link https://core.telegram.org/bots/api#forcereply + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\Keyboard + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function forceReply(array $data = []) + { + return new static(array_merge(['keyboard' => null, 'force_reply' => true, 'selective' => false], $data)); + } +} diff --git a/src/Entities/KeyboardButton.php b/src/Entities/KeyboardButton.php index dd7a199f3..0276c97ec 100644 --- a/src/Entities/KeyboardButton.php +++ b/src/Entities/KeyboardButton.php @@ -20,9 +20,40 @@ * @method string getText() Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed * @method bool getRequestContact() Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only * @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only + * + * @method $this setText(string $text) Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed + * @method $this setRequestContact(bool $request_contact) Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only + * @method $this setRequestLocation(bool $request_location) Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only */ class KeyboardButton extends Entity { + /** + * {@inheritdoc} + */ + public function __construct($data) + { + if (is_string($data)) { + $data = ['text' => $data]; + } + parent::__construct($data); + } + + /** + * Check if the passed data array could be a KeyboardButton. + * + * @param array $data + * + * @return bool + */ + public static function couldBe($data) + { + return is_array($data) && + array_key_exists('text', $data) && ( + array_key_exists('request_contact', $data) || + array_key_exists('request_location', $data) + ); + } + /** * {@inheritdoc} */ @@ -32,4 +63,17 @@ protected function validate() throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); } } + + /** + * {@inheritdoc} + */ + public function __call($method, $args) + { + // Only 1 of these can be set, so clear the others when setting a new one. + if (in_array($method, ['setRequestContact', 'setRequestLocation'], true)) { + unset($this->request_contact, $this->request_location); + } + + return parent::__call($method, $args); + } } diff --git a/src/Entities/ReplyKeyboardHide.php b/src/Entities/ReplyKeyboardHide.php deleted file mode 100644 index 0c7de70af..000000000 --- a/src/Entities/ReplyKeyboardHide.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -/** - * Class ReplyKeyboardHide - * - * @link https://core.telegram.org/bots/api#replykeyboardhide - * - * @method bool getHideKeyboard() Requests clients to hide the custom keyboard - * @method bool getSelective() Optional. Use this parameter if you want to hide keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. - */ -class ReplyKeyboardHide extends Entity -{ - /** - * ReplyKeyboardHide constructor. - * - * @param array $data - * - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public function __construct(array $data = []) - { - $data['hide_keyboard'] = true; - parent::__construct($data); - } -} diff --git a/src/Entities/ReplyKeyboardMarkup.php b/src/Entities/ReplyKeyboardMarkup.php deleted file mode 100644 index 6153a82e9..000000000 --- a/src/Entities/ReplyKeyboardMarkup.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Written by Marco Boretto - */ - -namespace Longman\TelegramBot\Entities; - -use Longman\TelegramBot\Exception\TelegramException; - - -/** - * Class ReplyKeyboardMarkup - * - * @link https://core.telegram.org/bots/api#replykeyboardmarkup - * - * @method KeyboardButton[][] getKeyboard() Array of button rows, each represented by an Array of KeyboardButton objects - * @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard. - * @method bool getOneTimeKeyboard() Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false. - * @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message. - */ -class ReplyKeyboardMarkup extends Entity -{ - /** - * {@inheritdoc} - */ - protected function subEntities() - { - return [ - 'keyboard' => KeyboardButton::class, - ]; - } - - /** - * {@inheritdoc} - */ - protected function validate() - { - $keyboard = $this->getProperty('keyboard'); - - if ($keyboard === null) { - throw new TelegramException('Keyboard field is empty!'); - } - - if (!is_array($keyboard)) { - throw new TelegramException('Keyboard field is not an array!'); - } - - foreach ($keyboard as $item) { - if (!is_array($item)) { - throw new TelegramException('Keyboard subfield is not an array!'); - } - } - } -} From 94aa39ee842b3eabfc5e87b71e861f82ba266c70 Mon Sep 17 00:00:00 2001 From: MBoretto Date: Thu, 29 Sep 2016 00:32:03 +0200 Subject: [PATCH 08/31] introduce md tryMention --- src/Entities/User.php | 57 +++++++++++++++++++---- tests/unit/Entities/UserTest.php | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 tests/unit/Entities/UserTest.php diff --git a/src/Entities/User.php b/src/Entities/User.php index a4079d959..65c7c137a 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -28,18 +28,59 @@ class User extends Entity { /** - * Try mention + * tryMention * - * @return string|null + * @param bool $markdown + * + * @return string */ - public function tryMention() + public function tryMention($markdown = false) { - if ($this->username === null) { - if ($this->last_name !== null) { - return $this->first_name . ' ' . $this->last_name; + if (isset($this->username)) { + if ($markdown) { + //Escaping md special characters + //Please notice that just the _ is allowed in the username ` * [ are not allowed + return $this->prependAt($this->stripMarkDown($this->username)); } - return $this->first_name; + return $this->prependAt($this->username); + } + + $name = $this->first_name; + if (isset($this->last_name)) { + $name .= ' ' . $this->last_name; } - return '@' . $this->username; + + if ($markdown) { + //Escaping md special characters + return $this->stripMarkDown($name); + } + return $name; + } + + /** + * stripMarkDown + * + * @param string $string + * + * @return string + */ + public function stripMarkDown($string) + { + $string = str_replace('[', '\[', $string); + $string = str_replace('`', '\`', $string); + $string = str_replace('*', '\*', $string); + return str_replace('_', '\_', $string); + } + + /** + * prepend@ + * + * @param string $string + * + * @return string + */ + public function prependAt($string) + { + return '@' . $string; } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php new file mode 100644 index 000000000..2374ac8d5 --- /dev/null +++ b/tests/unit/Entities/UserTest.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use \Longman\TelegramBot\Entities\User; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class UserTest extends TestCase +{ + /** + * @var \Longman\TelegramBot\Entities\User + */ + private $user; + + public function testUsername() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']); + $this->assertEquals('@jtaylor', $this->user->tryMention()); + } + + public function testFirstName() + { + $this->user = new User(['id' => 1, 'first_name' => 'John']); + $this->assertEquals('John', $this->user->tryMention()); + } + + public function testLastName() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); + $this->assertEquals('John Taylor', $this->user->tryMention()); + } + + public function testStripMarkDown() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); + $this->assertEquals('\`\[\*\_', $this->user->stripMarkDown('`[*_')); + } + + public function testPrependAt() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); + $this->assertEquals('@string', $this->user->prependAt('string')); + } + + public function testUsernameMarkdown() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); + $this->assertEquals('@j_taylor', $this->user->tryMention()); + $this->assertEquals('@j\_taylor', $this->user->tryMention(true)); + } + + public function testFirstNameMarkdown() + { + $this->user = new User(['id' => 1, 'first_name' => 'John[']); + $this->assertEquals('John[', $this->user->tryMention()); + $this->assertEquals('John\[', $this->user->tryMention(true)); + } + + public function testLastNameMarkdown() + { + $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']); + $this->assertEquals('John `Taylor`', $this->user->tryMention()); + $this->assertEquals('John \`Taylor\`', $this->user->tryMention(true)); + } +} From c816458d1c7e525e67f91e183a0ba6a624582b60 Mon Sep 17 00:00:00 2001 From: MBoretto Date: Thu, 29 Sep 2016 18:04:48 +0200 Subject: [PATCH 09/31] improving try mention markdown --- src/Entities/Entity.php | 17 ++++++++++++ src/Entities/User.php | 44 ++++++++------------------------ tests/unit/Entities/UserTest.php | 6 ----- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index a59f12836..64b9247b7 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -266,4 +266,21 @@ protected function makePrettyObjectArray($class, $property) return $new_objects; } + + /** + * stripMarkDown + * Gived a string escape special charactes used in Markdown + * + * @param string $string + * + * @return string + */ + public function stripMarkDown($string) + { + return str_replace( + ['[', '`', '*', '_',], + ['\[', '\`', '\*', '\_',], + $string + ); + } } diff --git a/src/Entities/User.php b/src/Entities/User.php index 65c7c137a..31304aa96 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -30,24 +30,29 @@ class User extends Entity /** * tryMention * + * Mention the user with the username otherwise print first and last name + * if the $markdown arguments is true special characters are escaped from the output + * * @param bool $markdown * * @return string */ public function tryMention($markdown = false) { - if (isset($this->username)) { + $username = $this->getProperty('username'); + if ($username !== null) { if ($markdown) { //Escaping md special characters //Please notice that just the _ is allowed in the username ` * [ are not allowed - return $this->prependAt($this->stripMarkDown($this->username)); + return '@' . $this->stripMarkDown($this->username); } - return $this->prependAt($this->username); + return '@' . $this->username; } - $name = $this->first_name; - if (isset($this->last_name)) { - $name .= ' ' . $this->last_name; + $name = $this->getProperty('first_name'); + $last_name = $this->getProperty('last_name'); + if ($last_name !== null) { + $name .= ' ' . $last_name; } if ($markdown) { @@ -56,31 +61,4 @@ public function tryMention($markdown = false) } return $name; } - - /** - * stripMarkDown - * - * @param string $string - * - * @return string - */ - public function stripMarkDown($string) - { - $string = str_replace('[', '\[', $string); - $string = str_replace('`', '\`', $string); - $string = str_replace('*', '\*', $string); - return str_replace('_', '\_', $string); - } - - /** - * prepend@ - * - * @param string $string - * - * @return string - */ - public function prependAt($string) - { - return '@' . $string; - } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php index 2374ac8d5..8099cfcf1 100644 --- a/tests/unit/Entities/UserTest.php +++ b/tests/unit/Entities/UserTest.php @@ -50,12 +50,6 @@ public function testStripMarkDown() $this->assertEquals('\`\[\*\_', $this->user->stripMarkDown('`[*_')); } - public function testPrependAt() - { - $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); - $this->assertEquals('@string', $this->user->prependAt('string')); - } - public function testUsernameMarkdown() { $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); From 8dba19fe8c8e2779a170204a974d65b676b4fe8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sat, 1 Oct 2016 23:25:03 +0200 Subject: [PATCH 10/31] Add initial keyboard tests and fix a few keyboard issues. --- src/Entities/InlineKeyboardButton.php | 4 + src/Entities/Keyboard.php | 14 +- src/Entities/KeyboardButton.php | 4 + .../Entities/InlineKeyboardButtonTest.php | 2 +- tests/unit/Entities/KeyboardButtonTest.php | 48 +++++++ tests/unit/Entities/KeyboardTest.php | 131 ++++++++++++++++++ 6 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 tests/unit/Entities/KeyboardButtonTest.php create mode 100644 tests/unit/Entities/KeyboardTest.php diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index abae6f37c..b31a12f31 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -51,6 +51,10 @@ public static function couldBe($data) */ protected function validate() { + if ($this->getProperty('text', '') === '') { + throw new TelegramException('You must add some text to the button!'); + } + $num_params = 0; foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 9bf7bc628..710384001 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -80,10 +80,16 @@ protected function createFromParams() $keyboard_type = $this->getKeyboardType(); // If the inline_keyboard isn't set directly, try to create one from the arguments. - $data = func_get_arg(0); - if (!array_key_exists($keyboard_type, $data)) { + $args = func_get_args(); + $data = reset($args); + + // Force button parameters into a single row. + if (!is_array($data)) { + $args = [$args]; + } + if (!array_key_exists($keyboard_type, (array) $data)) { $new_keyboard = []; - foreach (func_get_args() as $row) { + foreach ($args as $row) { if (is_array($row)) { $new_row = []; if ($button_class::couldBe($row)) { @@ -119,7 +125,7 @@ protected function createFromParams() public function addRow() { $keyboard_type = $this->getKeyboardType(); - $this->$keyboard_type[] = func_get_args(); + $this->{$keyboard_type}[] = func_get_args(); return $this; } diff --git a/src/Entities/KeyboardButton.php b/src/Entities/KeyboardButton.php index 0276c97ec..4ee2c97c1 100644 --- a/src/Entities/KeyboardButton.php +++ b/src/Entities/KeyboardButton.php @@ -59,6 +59,10 @@ public static function couldBe($data) */ protected function validate() { + if ($this->getProperty('text', '') === '') { + throw new TelegramException('You must add some text to the button!'); + } + if ($this->getRequestContact() && $this->getRequestLocation()) { throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); } diff --git a/tests/unit/Entities/InlineKeyboardButtonTest.php b/tests/unit/Entities/InlineKeyboardButtonTest.php index 92ae6da3e..ccf017380 100644 --- a/tests/unit/Entities/InlineKeyboardButtonTest.php +++ b/tests/unit/Entities/InlineKeyboardButtonTest.php @@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase { /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query! + * @expectedExceptionMessage You must add some text to the button! */ public function testInlineKeyboardButtonNoTextFail() { diff --git a/tests/unit/Entities/KeyboardButtonTest.php b/tests/unit/Entities/KeyboardButtonTest.php new file mode 100644 index 000000000..247f96fb7 --- /dev/null +++ b/tests/unit/Entities/KeyboardButtonTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\KeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class KeyboardButtonTest extends TestCase +{ + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage You must add some text to the button! + */ + public function testKeyboardButtonNoTextFail() + { + new KeyboardButton([]); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage You must use only one of these fields: request_contact, request_location! + */ + public function testKeyboardButtonTooManyParametersFail() + { + new KeyboardButton(['text' => 'message', 'request_contact' => true, 'request_location' => true]); + } + + public function testKeyboardButtonSuccess() + { + new KeyboardButton(['text' => 'message']); + new KeyboardButton(['text' => 'message', 'request_contact' => true]); + new KeyboardButton(['text' => 'message', 'request_location' => true]); + } +} diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php new file mode 100644 index 000000000..7dcfdb22a --- /dev/null +++ b/tests/unit/Entities/KeyboardTest.php @@ -0,0 +1,131 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\Keyboard; +use Longman\TelegramBot\Entities\KeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class KeyboardTest extends TestCase +{ + public function testKeyboardSingleButtonSingleRow() + { + $keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard = (new Keyboard(['Button Text 2']))->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[0][0]->getText()); + } + + public function testKeyboardSingleButtonMultipleRows() + { + $keyboard = (new Keyboard( + ['Button Text 1'], + ['Button Text 2'], + ['Button Text 3'] + ))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + } + + public function testKeyboardMultipleButtonsSingleRow() + { + /** @var KeyboardButton $keyboard_button_1 */ + /** @var KeyboardButton $keyboard_button_2 */ + + $keyboard = (new Keyboard('Button Text 1', 'Button Text 2'))->getProperty('keyboard'); + list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. + self::assertSame('Button Text 1', $keyboard_button_1->getText()); + self::assertSame('Button Text 2', $keyboard_button_2->getText()); + + $keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard'); + list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. + self::assertSame('Button Text 3', $keyboard_button_1->getText()); + self::assertSame('Button Text 4', $keyboard_button_2->getText()); + } + + public function testKeyboardMultipleButtonsMultipleRows() + { + /** @var KeyboardButton $keyboard_button_1 */ + /** @var KeyboardButton $keyboard_button_2 */ + /** @var KeyboardButton $keyboard_button_3 */ + /** @var KeyboardButton $keyboard_button_4 */ + + $keyboard = (new Keyboard( + ['Button Text 1', 'Button Text 2'], + ['Button Text 3', 'Button Text 4'] + ))->getProperty('keyboard'); + + list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. + list($keyboard_button_3, $keyboard_button_4) = $keyboard[1]; // Row 2. + + self::assertSame('Button Text 1', $keyboard_button_1->getText()); + self::assertSame('Button Text 2', $keyboard_button_2->getText()); + self::assertSame('Button Text 3', $keyboard_button_3->getText()); + self::assertSame('Button Text 4', $keyboard_button_4->getText()); + } + + public function testKeyboardWithButtonObjects() + { + $keyboard = (new Keyboard( + new KeyboardButton('Button Text 1') + ))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard = (new Keyboard( + new KeyboardButton('Button Text 2'), + new KeyboardButton('Button Text 3') + ))->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[0][0]->getText()); + self::assertSame('Button Text 3', $keyboard[0][1]->getText()); + + $keyboard = (new Keyboard( + [new KeyboardButton('Button Text 4')], + [new KeyboardButton('Button Text 5'), new KeyboardButton('Button Text 6')] + ))->getProperty('keyboard'); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[1][1]->getText()); + } + + + /*public function testKeyboardWithData() + { + $keyboard = (new Keyboard( + ['Button Text 1', 'Button Text 2'], + ['Button Text 3', 'Button Text 4'] + ))->getProperty('keyboard'); + + }*/ + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage You must use only one of these fields: request_contact, request_location! + */ + /*public function testKeyboardTooManyParametersFail() + { + new Keyboard(['text' => 'message', 'request_contact' => true, 'request_location' => true]); + }*/ + + /*public function testKeyboardSuccess() + { + new Keyboard(['text' => 'message']); + new Keyboard(['text' => 'message', 'request_contact' => true]); + new Keyboard(['text' => 'message', 'request_location' => true]); + }*/ +} From 9eb50bb00d84e5a4d9192917b9110a2defa52002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 3 Oct 2016 17:26:11 +0200 Subject: [PATCH 11/31] Change interpretation of keyboard parameters, so that each parameter represents a button on it's own row. --- src/Entities/Keyboard.php | 19 ++++---- tests/unit/Entities/KeyboardTest.php | 66 +++++++--------------------- 2 files changed, 26 insertions(+), 59 deletions(-) diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 710384001..435898f11 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -51,7 +51,7 @@ public function isInlineKeyboard() /** * Get the proper keyboard button class for this keyboard. * - * @return mixed + * @return KeyboardButton|InlineKeyboardButton */ public function getKeyboardButtonClass() { @@ -75,19 +75,20 @@ public function getKeyboardType() */ protected function createFromParams() { - /** @var KeyboardButton|InlineKeyboardButton $button_class */ $button_class = $this->getKeyboardButtonClass(); $keyboard_type = $this->getKeyboardType(); - // If the inline_keyboard isn't set directly, try to create one from the arguments. $args = func_get_args(); - $data = reset($args); - // Force button parameters into a single row. - if (!is_array($data)) { - $args = [$args]; + // Force button parameters into individual rows. + foreach ($args as &$arg) { + !is_array($arg) && $arg = [$arg]; } - if (!array_key_exists($keyboard_type, (array) $data)) { + unset($arg); + + $data = reset($args); + + if (!array_key_exists($keyboard_type, $data)) { $new_keyboard = []; foreach ($args as $row) { if (is_array($row)) { @@ -124,7 +125,7 @@ protected function createFromParams() */ public function addRow() { - $keyboard_type = $this->getKeyboardType(); + $keyboard_type = $this->getKeyboardType(); $this->{$keyboard_type}[] = func_get_args(); return $this; diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php index 7dcfdb22a..c4cc216c4 100644 --- a/tests/unit/Entities/KeyboardTest.php +++ b/tests/unit/Entities/KeyboardTest.php @@ -33,6 +33,15 @@ public function testKeyboardSingleButtonSingleRow() public function testKeyboardSingleButtonMultipleRows() { + $keyboard = (new Keyboard( + 'Button Text 1', + 'Button Text 2', + 'Button Text 3' + ))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + $keyboard = (new Keyboard( ['Button Text 1'], ['Button Text 2'], @@ -45,39 +54,22 @@ public function testKeyboardSingleButtonMultipleRows() public function testKeyboardMultipleButtonsSingleRow() { - /** @var KeyboardButton $keyboard_button_1 */ - /** @var KeyboardButton $keyboard_button_2 */ - - $keyboard = (new Keyboard('Button Text 1', 'Button Text 2'))->getProperty('keyboard'); - list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. - self::assertSame('Button Text 1', $keyboard_button_1->getText()); - self::assertSame('Button Text 2', $keyboard_button_2->getText()); - $keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard'); - list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. - self::assertSame('Button Text 3', $keyboard_button_1->getText()); - self::assertSame('Button Text 4', $keyboard_button_2->getText()); + self::assertSame('Button Text 3', $keyboard[0][0]->getText()); + self::assertSame('Button Text 4', $keyboard[0][1]->getText()); } public function testKeyboardMultipleButtonsMultipleRows() { - /** @var KeyboardButton $keyboard_button_1 */ - /** @var KeyboardButton $keyboard_button_2 */ - /** @var KeyboardButton $keyboard_button_3 */ - /** @var KeyboardButton $keyboard_button_4 */ - $keyboard = (new Keyboard( ['Button Text 1', 'Button Text 2'], ['Button Text 3', 'Button Text 4'] ))->getProperty('keyboard'); - list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. - list($keyboard_button_3, $keyboard_button_4) = $keyboard[1]; // Row 2. - - self::assertSame('Button Text 1', $keyboard_button_1->getText()); - self::assertSame('Button Text 2', $keyboard_button_2->getText()); - self::assertSame('Button Text 3', $keyboard_button_3->getText()); - self::assertSame('Button Text 4', $keyboard_button_4->getText()); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); + self::assertSame('Button Text 4', $keyboard[1][1]->getText()); } public function testKeyboardWithButtonObjects() @@ -92,7 +84,7 @@ public function testKeyboardWithButtonObjects() new KeyboardButton('Button Text 3') ))->getProperty('keyboard'); self::assertSame('Button Text 2', $keyboard[0][0]->getText()); - self::assertSame('Button Text 3', $keyboard[0][1]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); $keyboard = (new Keyboard( [new KeyboardButton('Button Text 4')], @@ -102,30 +94,4 @@ public function testKeyboardWithButtonObjects() self::assertSame('Button Text 5', $keyboard[1][0]->getText()); self::assertSame('Button Text 6', $keyboard[1][1]->getText()); } - - - /*public function testKeyboardWithData() - { - $keyboard = (new Keyboard( - ['Button Text 1', 'Button Text 2'], - ['Button Text 3', 'Button Text 4'] - ))->getProperty('keyboard'); - - }*/ - - /** - * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage You must use only one of these fields: request_contact, request_location! - */ - /*public function testKeyboardTooManyParametersFail() - { - new Keyboard(['text' => 'message', 'request_contact' => true, 'request_location' => true]); - }*/ - - /*public function testKeyboardSuccess() - { - new Keyboard(['text' => 'message']); - new Keyboard(['text' => 'message', 'request_contact' => true]); - new Keyboard(['text' => 'message', 'request_location' => true]); - }*/ } From ce42f431d5d8d848ae261ed7702e2b3e5d614b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 4 Oct 2016 00:31:32 +0200 Subject: [PATCH 12/31] Add InlineKeyboard tests and fix createFromParams method. --- src/Entities/Keyboard.php | 8 +- tests/unit/Entities/InlineKeyboardTest.php | 103 +++++++++++++++++++++ tests/unit/Entities/KeyboardTest.php | 18 ++-- 3 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 tests/unit/Entities/InlineKeyboardTest.php diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 435898f11..1eff05c1a 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -88,7 +88,7 @@ protected function createFromParams() $data = reset($args); - if (!array_key_exists($keyboard_type, $data)) { + if (!array_key_exists($keyboard_type, (array)$data)) { $new_keyboard = []; foreach ($args as $row) { if (is_array($row)) { @@ -105,7 +105,11 @@ protected function createFromParams() } } } else { - $new_row = [new $button_class($row)]; + $button = $row; + if (!($button instanceof $button_class)) { + $button = new $button_class($button); + } + $new_row = [$button]; } $new_keyboard[] = $new_row; } diff --git a/tests/unit/Entities/InlineKeyboardTest.php b/tests/unit/Entities/InlineKeyboardTest.php new file mode 100644 index 000000000..5b07dd84c --- /dev/null +++ b/tests/unit/Entities/InlineKeyboardTest.php @@ -0,0 +1,103 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Tests\Unit; + +use Longman\TelegramBot\Entities\InlineKeyboard; +use Longman\TelegramBot\Entities\InlineKeyboardButton; + +/** + * @package TelegramTest + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) + * @link http://www.github.com/akalongman/php-telegram-bot + */ +class InlineKeyboardTest extends TestCase +{ + private function getRandomButton($text) + { + $random_params = ['url', 'callback_data', 'switch_inline_query']; + $param = $random_params[array_rand($random_params, 1)]; + $data = [ + 'text' => $text, + $param => 'random_param', + ]; + + return new InlineKeyboardButton($data); + } + + public function testNothing(){ + + } + + public function testInlineKeyboardSingleButtonSinleRow() + { + $inline_keyboard = (new InlineKeyboard( + $this->getRandomButton('Button Text 1') + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $inline_keyboard[0][0]->getText()); + + $inline_keyboard = (new InlineKeyboard( + [$this->getRandomButton('Button Text 2')] + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 2', $inline_keyboard[0][0]->getText()); + } + + public function testInlineKeyboardSingleButtonMultipleRows() + { + $keyboard = (new InlineKeyboard( + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2'), + $this->getRandomButton('Button Text 3') + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + + $keyboard = (new InlineKeyboard( + [$this->getRandomButton('Button Text 4')], + [$this->getRandomButton('Button Text 5')], + [$this->getRandomButton('Button Text 6')] + ))->getProperty('inline_keyboard'); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[2][0]->getText()); + } + + public function testInlineKeyboardMultipleButtonsSingleRow() + { + $keyboard = (new InlineKeyboard([ + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2') + ]))->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + } + + public function testInlineKeyboardMultipleButtonsMultipleRows() + { + $keyboard = (new InlineKeyboard( + [ + $this->getRandomButton('Button Text 1'), + $this->getRandomButton('Button Text 2') + ], + [ + $this->getRandomButton('Button Text 3'), + $this->getRandomButton('Button Text 4') + ] + ))->getProperty('inline_keyboard'); + + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); + self::assertSame('Button Text 3', $keyboard[1][0]->getText()); + self::assertSame('Button Text 4', $keyboard[1][1]->getText()); + } +} diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php index c4cc216c4..801cd1a8a 100644 --- a/tests/unit/Entities/KeyboardTest.php +++ b/tests/unit/Entities/KeyboardTest.php @@ -43,20 +43,20 @@ public function testKeyboardSingleButtonMultipleRows() self::assertSame('Button Text 3', $keyboard[2][0]->getText()); $keyboard = (new Keyboard( - ['Button Text 1'], - ['Button Text 2'], - ['Button Text 3'] + ['Button Text 4'], + ['Button Text 5'], + ['Button Text 6'] ))->getProperty('keyboard'); - self::assertSame('Button Text 1', $keyboard[0][0]->getText()); - self::assertSame('Button Text 2', $keyboard[1][0]->getText()); - self::assertSame('Button Text 3', $keyboard[2][0]->getText()); + self::assertSame('Button Text 4', $keyboard[0][0]->getText()); + self::assertSame('Button Text 5', $keyboard[1][0]->getText()); + self::assertSame('Button Text 6', $keyboard[2][0]->getText()); } public function testKeyboardMultipleButtonsSingleRow() { - $keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard'); - self::assertSame('Button Text 3', $keyboard[0][0]->getText()); - self::assertSame('Button Text 4', $keyboard[0][1]->getText()); + $keyboard = (new Keyboard(['Button Text 1', 'Button Text 2']))->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + self::assertSame('Button Text 2', $keyboard[0][1]->getText()); } public function testKeyboardMultipleButtonsMultipleRows() From 1b6647513012b40d6d8e3a6953393a0ee69746d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 4 Oct 2016 21:56:10 +0200 Subject: [PATCH 13/31] Remove unnecessary conditional when creating keyboard from params. --- src/Entities/Keyboard.php | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 1eff05c1a..a4ba57a1b 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -91,25 +91,17 @@ protected function createFromParams() if (!array_key_exists($keyboard_type, (array)$data)) { $new_keyboard = []; foreach ($args as $row) { - if (is_array($row)) { - $new_row = []; - if ($button_class::couldBe($row)) { - $new_row[] = new $button_class($row); - } else { - foreach ($row as $button) { - if ($button instanceof $button_class) { - $new_row[] = $button; - } elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { - $new_row[] = new $button_class($button); - } - } - } + $new_row = []; + if ($button_class::couldBe($row)) { + $new_row[] = new $button_class($row); } else { - $button = $row; - if (!($button instanceof $button_class)) { - $button = new $button_class($button); + foreach ($row as $button) { + if ($button instanceof $button_class) { + $new_row[] = $button; + } elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { + $new_row[] = new $button_class($button); + } } - $new_row = [$button]; } $new_keyboard[] = $new_row; } From 0001e68713f0c38210e4fc8c77a5138ac748c471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Thu, 6 Oct 2016 01:12:21 +0300 Subject: [PATCH 14/31] Add extra keyboard tests and fix keyboard row and button parsing. --- src/Entities/Keyboard.php | 92 ++++++++++++++----- src/Entities/KeyboardButton.php | 6 +- .../Entities/InlineKeyboardButtonTest.php | 72 ++++++++++++++- tests/unit/Entities/InlineKeyboardTest.php | 39 +++++++- tests/unit/Entities/KeyboardButtonTest.php | 21 +++++ tests/unit/Entities/KeyboardTest.php | 91 ++++++++++++++++++ 6 files changed, 290 insertions(+), 31 deletions(-) diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index a4ba57a1b..5103be9d6 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -36,6 +36,9 @@ public function __construct($data = []) { $data = call_user_func_array([$this, 'createFromParams'], func_get_args()); parent::__construct($data); + + // Remove any empty buttons. + $this->{$this->getKeyboardType()} = array_filter($this->{$this->getKeyboardType()}); } /** @@ -75,7 +78,6 @@ public function getKeyboardType() */ protected function createFromParams() { - $button_class = $this->getKeyboardButtonClass(); $keyboard_type = $this->getKeyboardType(); $args = func_get_args(); @@ -88,27 +90,25 @@ protected function createFromParams() $data = reset($args); - if (!array_key_exists($keyboard_type, (array)$data)) { - $new_keyboard = []; - foreach ($args as $row) { - $new_row = []; - if ($button_class::couldBe($row)) { - $new_row[] = new $button_class($row); - } else { - foreach ($row as $button) { - if ($button instanceof $button_class) { - $new_row[] = $button; - } elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { - $new_row[] = new $button_class($button); - } - } - } - $new_keyboard[] = $new_row; + if ($from_data = array_key_exists($keyboard_type, (array)$data)) { + $args = $data[$keyboard_type]; + + // Make sure we're working with a proper row. + if (!is_array($args)) { + $args = []; } + } - if (!empty($new_keyboard)) { - $data = [$keyboard_type => $new_keyboard]; + $new_keyboard = []; + foreach ($args as $row) { + $new_keyboard[] = $this->parseRow($row); + } + + if (!empty($new_keyboard)) { + if (!$from_data) { + $data = []; } + $data[$keyboard_type] = $new_keyboard; } return $data; @@ -121,12 +121,58 @@ protected function createFromParams() */ public function addRow() { - $keyboard_type = $this->getKeyboardType(); - $this->{$keyboard_type}[] = func_get_args(); + if (($new_row = $this->parseRow(func_get_args())) !== null) { + $this->{$this->getKeyboardType()}[] = $new_row; + } return $this; } + /** + * Parse a given row to the correct array format. + * + * @param array $row + * + * @return array + */ + protected function parseRow($row) + { + if (!is_array($row)) { + return null; + } + + $new_row = []; + foreach ($row as $button) { + if (($new_button = $this->parseButton($button)) !== null) { + $new_row[] = $new_button; + } + } + + return $new_row; + } + + /** + * Parse a given button to the correct KeyboardButton object type. + * + * @param array|string|\Longman\TelegramBot\Entities\KeyboardButton $button + * + * @return \Longman\TelegramBot\Entities\KeyboardButton|null + */ + protected function parseButton($button) + { + $button_class = $this->getKeyboardButtonClass(); + + if ($button instanceof $button_class) { + return $button; + } + + if (!$this->isInlineKeyboard() || $button_class::couldBe($button)) { + return new $button_class($button); + } + + return null; + } + /** * {@inheritdoc} */ @@ -159,7 +205,7 @@ protected function validate() */ public static function hide(array $data = []) { - return new static(array_merge(['keyboard' => null, 'hide_keyboard' => true, 'selective' => false], $data)); + return new static(array_merge(['keyboard' => [], 'hide_keyboard' => true, 'selective' => false], $data)); } /** @@ -174,6 +220,6 @@ public static function hide(array $data = []) */ public static function forceReply(array $data = []) { - return new static(array_merge(['keyboard' => null, 'force_reply' => true, 'selective' => false], $data)); + return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); } } diff --git a/src/Entities/KeyboardButton.php b/src/Entities/KeyboardButton.php index 4ee2c97c1..a7fb7ad5e 100644 --- a/src/Entities/KeyboardButton.php +++ b/src/Entities/KeyboardButton.php @@ -47,11 +47,7 @@ public function __construct($data) */ public static function couldBe($data) { - return is_array($data) && - array_key_exists('text', $data) && ( - array_key_exists('request_contact', $data) || - array_key_exists('request_location', $data) - ); + return is_array($data) && array_key_exists('text', $data); } /** diff --git a/tests/unit/Entities/InlineKeyboardButtonTest.php b/tests/unit/Entities/InlineKeyboardButtonTest.php index ccf017380..d7cd69185 100644 --- a/tests/unit/Entities/InlineKeyboardButtonTest.php +++ b/tests/unit/Entities/InlineKeyboardButtonTest.php @@ -45,7 +45,31 @@ public function testInlineKeyboardButtonNoParameterFail() */ public function testInlineKeyboardButtonTooManyParametersFail() { - new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value', 'callback_data' => 'callback_data_value']); + $test_funcs = [ + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'url' => 'url_value', + 'callback_data' => 'callback_data_value', + ]); + }, + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'url' => 'url_value', + 'switch_inline_query' => 'switch_inline_query_value', + ]); + }, + function () { + new InlineKeyboardButton([ + 'text' => 'message', + 'callback_data' => 'callback_data_value', + 'switch_inline_query' => 'switch_inline_query_value', + ]); + }, + ]; + + $test_funcs[array_rand($test_funcs)](); } public function testInlineKeyboardButtonSuccess() @@ -54,4 +78,50 @@ public function testInlineKeyboardButtonSuccess() new InlineKeyboardButton(['text' => 'message', 'callback_data' => 'callback_data_value']); new InlineKeyboardButton(['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']); } + + public function testInlineKeyboardButtonCouldBe() + { + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'url' => 'url_value'] + )); + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'callback_data' => 'callback_data_value'] + )); + self::assertTrue(InlineKeyboardButton::couldBe( + ['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value'] + )); + + self::assertFalse(InlineKeyboardButton::couldBe(['no_text' => 'message'])); + self::assertFalse(InlineKeyboardButton::couldBe(['text' => 'message'])); + self::assertFalse(InlineKeyboardButton::couldBe(['url' => 'url_value'])); + self::assertFalse(InlineKeyboardButton::couldBe( + ['callback_data' => 'callback_data_value'] + )); + self::assertFalse(InlineKeyboardButton::couldBe( + ['switch_inline_query' => 'switch_inline_query_value'] + )); + self::assertFalse(InlineKeyboardButton::couldBe([ + 'url' => 'url_value', + 'callback_data' => 'callback_data_value', + 'switch_inline_query' => 'switch_inline_query_value', + ])); + } + + public function testInlineKeyboardButtonParameterSetting() + { + $button = new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value']); + self::assertSame('url_value', $button->getUrl()); + self::assertEmpty($button->getCallbackData()); + self::assertEmpty($button->getSwitchInlineQuery()); + + $button->setCallbackData('callback_data_value'); + self::assertEmpty($button->getUrl()); + self::assertSame('callback_data_value', $button->getCallbackData()); + self::assertEmpty($button->getSwitchInlineQuery()); + + $button->setSwitchInlineQuery('switch_inline_query_value'); + self::assertEmpty($button->getUrl()); + self::assertEmpty($button->getCallbackData()); + self::assertSame('switch_inline_query_value', $button->getSwitchInlineQuery()); + } } diff --git a/tests/unit/Entities/InlineKeyboardTest.php b/tests/unit/Entities/InlineKeyboardTest.php index 5b07dd84c..e88c46e78 100644 --- a/tests/unit/Entities/InlineKeyboardTest.php +++ b/tests/unit/Entities/InlineKeyboardTest.php @@ -34,11 +34,25 @@ private function getRandomButton($text) return new InlineKeyboardButton($data); } - public function testNothing(){ + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Inline Keyboard field is not an array! + */ + public function testInlineKeyboardDataMalformedField() + { + new InlineKeyboard(['inline_keyboard' => 'wrong']); + } + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Inline Keyboard subfield is not an array! + */ + public function testInlineKeyboardDataMalformedSubfield() + { + new InlineKeyboard(['inline_keyboard' => ['wrong']]); } - public function testInlineKeyboardSingleButtonSinleRow() + public function testInlineKeyboardSingleButtonSingleRow() { $inline_keyboard = (new InlineKeyboard( $this->getRandomButton('Button Text 1') @@ -100,4 +114,25 @@ public function testInlineKeyboardMultipleButtonsMultipleRows() self::assertSame('Button Text 3', $keyboard[1][0]->getText()); self::assertSame('Button Text 4', $keyboard[1][1]->getText()); } + + public function testInlineKeyboardAddRows() + { + $keyboard_obj = new InlineKeyboard([]); + + $keyboard_obj->addRow($this->getRandomButton('Button Text 1')); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard_obj->addRow( + $this->getRandomButton('Button Text 2'), + $this->getRandomButton('Button Text 3') + ); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[1][1]->getText()); + + $keyboard_obj->addRow($this->getRandomButton('Button Text 4')); + $keyboard = $keyboard_obj->getProperty('inline_keyboard'); + self::assertSame('Button Text 4', $keyboard[2][0]->getText()); + } } diff --git a/tests/unit/Entities/KeyboardButtonTest.php b/tests/unit/Entities/KeyboardButtonTest.php index 247f96fb7..0e1260946 100644 --- a/tests/unit/Entities/KeyboardButtonTest.php +++ b/tests/unit/Entities/KeyboardButtonTest.php @@ -45,4 +45,25 @@ public function testKeyboardButtonSuccess() new KeyboardButton(['text' => 'message', 'request_contact' => true]); new KeyboardButton(['text' => 'message', 'request_location' => true]); } + + public function testInlineKeyboardButtonCouldBe() + { + self::assertTrue(KeyboardButton::couldBe(['text' => 'message'])); + self::assertFalse(KeyboardButton::couldBe(['no_text' => 'message'])); + } + + public function testKeyboardButtonParameterSetting() + { + $button = new KeyboardButton('message'); + self::assertEmpty($button->getRequestContact()); + self::assertEmpty($button->getRequestLocation()); + + $button->setRequestContact(true); + self::assertTrue($button->getRequestContact()); + self::assertEmpty($button->getRequestLocation()); + + $button->setRequestLocation(true); + self::assertEmpty($button->getRequestContact()); + self::assertTrue($button->getRequestLocation()); + } } diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php index 801cd1a8a..ca902262b 100644 --- a/tests/unit/Entities/KeyboardTest.php +++ b/tests/unit/Entities/KeyboardTest.php @@ -12,6 +12,7 @@ use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Entities\KeyboardButton; +use Longman\TelegramBot\Exception\TelegramException; /** * @package TelegramTest @@ -22,6 +23,24 @@ */ class KeyboardTest extends TestCase { + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Keyboard field is not an array! + */ + public function testKeyboardDataMalformedField() + { + new Keyboard(['keyboard' => 'wrong']); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Keyboard subfield is not an array! + */ + public function testKeyboardDataMalformedSubfield() + { + new Keyboard(['keyboard' => ['wrong']]); + } + public function testKeyboardSingleButtonSingleRow() { $keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard'); @@ -94,4 +113,76 @@ public function testKeyboardWithButtonObjects() self::assertSame('Button Text 5', $keyboard[1][0]->getText()); self::assertSame('Button Text 6', $keyboard[1][1]->getText()); } + + public function testKeyboardWithDataArray() + { + $resize_keyboard = (bool)mt_rand(0, 1); + $one_time_keyboard = (bool)mt_rand(0, 1); + $selective = (bool)mt_rand(0, 1); + + $keyboard_obj = new Keyboard([ + 'resize_keyboard' => $resize_keyboard, + 'one_time_keyboard' => $one_time_keyboard, + 'selective' => $selective, + 'keyboard' => [['Button Text 1']], + ]); + + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard()); + self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard()); + self::assertSame($selective, $keyboard_obj->getSelective()); + } + + public function testPredefinedKeyboards() + { + $keyboard_hide = Keyboard::hide(); + self::assertTrue($keyboard_hide->getProperty('hide_keyboard')); + + $keyboard_force_reply = Keyboard::forceReply(); + self::assertTrue($keyboard_force_reply->getProperty('force_reply')); + } + + public function testKeyboardMethods() + { + $keyboard_obj = new Keyboard([]); + + self::assertEmpty($keyboard_obj->getOneTimeKeyboard()); + self::assertEmpty($keyboard_obj->getResizeKeyboard()); + self::assertEmpty($keyboard_obj->getSelective()); + + $keyboard_obj->setOneTimeKeyboard(true); + self::assertTrue($keyboard_obj->getOneTimeKeyboard()); + $keyboard_obj->setOneTimeKeyboard(false); + self::assertFalse($keyboard_obj->getOneTimeKeyboard()); + + $keyboard_obj->setResizeKeyboard(true); + self::assertTrue($keyboard_obj->getResizeKeyboard()); + $keyboard_obj->setResizeKeyboard(false); + self::assertFalse($keyboard_obj->getResizeKeyboard()); + + $keyboard_obj->setSelective(true); + self::assertTrue($keyboard_obj->getSelective()); + $keyboard_obj->setSelective(false); + self::assertFalse($keyboard_obj->getSelective()); + } + + public function testKeyboardAddRows() + { + $keyboard_obj = new Keyboard([]); + + $keyboard_obj->addRow('Button Text 1'); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 1', $keyboard[0][0]->getText()); + + $keyboard_obj->addRow('Button Text 2', 'Button Text 3'); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 2', $keyboard[1][0]->getText()); + self::assertSame('Button Text 3', $keyboard[1][1]->getText()); + + $keyboard_obj->addRow(['text' => 'Button Text 4']); + $keyboard = $keyboard_obj->getProperty('keyboard'); + self::assertSame('Button Text 4', $keyboard[2][0]->getText()); + } } From f9a70a29ea099089b28ea7feb4e27ea525e4a660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 17:24:55 +0300 Subject: [PATCH 15/31] Make use of the new Keyboard entities. Some typos, code cleanup. --- examples/Commands/KeyboardCommand.php | 3 +- examples/Commands/MarkdownCommand.php | 2 +- src/Commands/UserCommands/CancelCommand.php | 4 +-- src/Entities/Keyboard.php | 2 +- src/Entities/Message.php | 36 +++++++++------------ 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/examples/Commands/KeyboardCommand.php b/examples/Commands/KeyboardCommand.php index 95722fe21..0c12b738b 100644 --- a/examples/Commands/KeyboardCommand.php +++ b/examples/Commands/KeyboardCommand.php @@ -33,8 +33,6 @@ class KeyboardCommand extends UserCommand */ public function execute() { - $chat_id = $this->getMessage()->getChat()->getId(); - //Keyboard examples /** @var Keyboard[] $keyboards */ $keyboards = []; @@ -77,6 +75,7 @@ public function execute() ->setOneTimeKeyboard(true) ->setSelective(false); + $chat_id = $this->getMessage()->getChat()->getId(); $data = [ 'chat_id' => $chat_id, 'text' => 'Press a Button:', diff --git a/examples/Commands/MarkdownCommand.php b/examples/Commands/MarkdownCommand.php index 07f863691..5aefdd51c 100644 --- a/examples/Commands/MarkdownCommand.php +++ b/examples/Commands/MarkdownCommand.php @@ -23,7 +23,7 @@ class MarkdownCommand extends UserCommand * {@inheritdoc} */ protected $name = 'markdown'; - protected $description = 'Print Markdown tesxt'; + protected $description = 'Print Markdown text'; protected $usage = '/markdown'; protected $version = '1.0.1'; /**#@-*/ diff --git a/src/Commands/UserCommands/CancelCommand.php b/src/Commands/UserCommands/CancelCommand.php index b6eb93d87..5c7d6313d 100644 --- a/src/Commands/UserCommands/CancelCommand.php +++ b/src/Commands/UserCommands/CancelCommand.php @@ -12,7 +12,7 @@ use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Conversation; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; /** @@ -85,7 +85,7 @@ private function hideKeyboard($text) { return Request::sendMessage( [ - 'reply_markup' => new ReplyKeyboardHide(['selective' => true]), + 'reply_markup' => Keyboard::hide(['selective' => true]), 'chat_id' => $this->getMessage()->getChat()->getId(), 'text' => $text, ] diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 5103be9d6..0edeada0f 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -15,7 +15,7 @@ use Longman\TelegramBot\Exception\TelegramException; /** - * Class ReplyKeyboardMarkup + * Class Keyboard * * @link https://core.telegram.org/bots/api#replykeyboardmarkup * diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 2922573dd..afb278eb4 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -149,19 +149,16 @@ public function getEntities() */ public function getFullCommand() { - if (strpos($this->text, '/') === 0) { - $no_EOL = strtok($this->text, PHP_EOL); - $no_space = strtok($this->text, ' '); + $text = $this->getProperty('text'); + if (strpos($text, '/') === 0) { + $no_EOL = strtok($text, PHP_EOL); + $no_space = strtok($text, ' '); //try to understand which separator \n or space divide /command from text - if (strlen($no_space) < strlen($no_EOL)) { - return $no_space; - } else { - return $no_EOL; - } - } else { - return null; + return strlen($no_space) < strlen($no_EOL) ? $no_space : $no_EOL; } + + return null; } /** @@ -171,8 +168,9 @@ public function getFullCommand() */ public function getCommand() { - if (!empty($this->command)) { - return $this->command; + $command = $this->getProperty('command'); + if (!empty($command)) { + return $command; } $cmd = $this->getFullCommand(); @@ -185,11 +183,11 @@ public function getCommand() if (isset($split_cmd[1])) { //command is followed by name check if is addressed to me if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) { - return $this->command = $split_cmd[0]; + return $split_cmd[0]; } } else { //command is not followed by name - return $this->command = $cmd; + return $cmd; } } @@ -205,7 +203,7 @@ public function getCommand() */ public function getText($without_cmd = false) { - $text = $this->text; + $text = $this->getProperty('text'); if ($without_cmd && $command = $this->getFullCommand()) { if (strlen($command) + 1 < strlen($text)) { @@ -225,13 +223,9 @@ public function getText($without_cmd = false) */ public function botAddedInChat() { - if (!empty($this->new_chat_member)) { - if ($this->new_chat_member->getUsername() === $this->getBotName()) { - return true; - } - } + $member = $this->getNewChatMember(); - return false; + return $member !== null && $member->getUsername() === $this->getBotName(); } /** From d58e54ba99b33d3b179d9b76f917470d2fd39f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 17:27:18 +0300 Subject: [PATCH 16/31] Fix survey command to use new Keyboard entities. Also free some bugs! --- src/Commands/UserCommands/SurveyCommand.php | 77 ++++++++------------- 1 file changed, 28 insertions(+), 49 deletions(-) diff --git a/src/Commands/UserCommands/SurveyCommand.php b/src/Commands/UserCommands/SurveyCommand.php index 7907d8fd7..05293cd7e 100644 --- a/src/Commands/UserCommands/SurveyCommand.php +++ b/src/Commands/UserCommands/SurveyCommand.php @@ -10,13 +10,12 @@ namespace Longman\TelegramBot\Commands\UserCommands; +use Longman\TelegramBot\Commands\UserCommand; +use Longman\TelegramBot\Conversation; +use Longman\TelegramBot\Entities\Keyboard; +use Longman\TelegramBot\Entities\KeyboardButton; use Longman\TelegramBot\Entities\PhotoSize; use Longman\TelegramBot\Request; -use Longman\TelegramBot\Conversation; -use Longman\TelegramBot\Commands\UserCommand; -use Longman\TelegramBot\Entities\ForceReply; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; /** * User "/survery" command @@ -68,7 +67,6 @@ public function execute() $chat = $message->getChat(); $user = $message->getFrom(); $text = trim($message->getText(true)); - $chat_id = $chat->getId(); $user_id = $user->getId(); @@ -80,12 +78,14 @@ public function execute() if ($chat->isGroupChat() || $chat->isSuperGroup()) { //reply to message id is applied by default //Force reply is applied by default so it can work with privacy on - $data['reply_markup'] = new ForceReply(['selective' => true]); + $data['reply_markup'] = Keyboard::forceReply(['selective' => true]); } //Conversation start $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); - $notes = &$this->conversation->notes; + + $notes = &$this->conversation->notes; + !is_array($notes) && $notes = []; //cache data from the tracking session if any $state = 0; @@ -105,7 +105,7 @@ public function execute() $this->conversation->update(); $data['text'] = 'Type your name:'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; @@ -149,18 +149,14 @@ public function execute() // no break case 3: - if ($text === '' || !($text === 'M' || $text === 'F')) { + if ($text === '' || !in_array($text, ['M', 'F'], true)) { $notes['state'] = 3; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [['M', 'F']], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard(['M', 'F'])) + ->setResizeKeyboard(true) + ->setOneTimeKeyboard(true) + ->setSelective(true); $data['text'] = 'Select your gender:'; if ($text !== '') { @@ -170,6 +166,7 @@ public function execute() $result = Request::sendMessage($data); break; } + $notes['gender'] = $text; // no break @@ -178,21 +175,12 @@ public function execute() $notes['state'] = 4; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [ - [ - [ - 'text' => 'Share Location', - 'request_location' => true, - ], - ], - ], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard( + (new KeyboardButton('Share Location'))->setRequestLocation(true) + )) + ->setOneTimeKeyboard(true) + ->setResizeKeyboard(true) + ->setSelective(true); $data['text'] = 'Share your location:'; @@ -225,21 +213,12 @@ public function execute() $notes['state'] = 6; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyboardMarkup( - [ - 'keyboard' => [ - [ - [ - 'text' => 'Share Contact', - 'request_contact' => true, - ], - ], - ], - 'resize_keyboard' => true, - 'one_time_keyboard' => true, - 'selective' => true, - ] - ); + $data['reply_markup'] = (new Keyboard( + (new KeyboardButton('Share Contact'))->setRequestContact(true) + )) + ->setOneTimeKeyboard(true) + ->setResizeKeyboard(true) + ->setSelective(true); $data['text'] = 'Share your contact information:'; @@ -259,7 +238,7 @@ public function execute() } $data['photo'] = $notes['photo_id']; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $data['caption'] = $out_text; $this->conversation->stop(); From 3120727f2943e67ebc412ae90c6d733418c2cb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 18:31:52 +0300 Subject: [PATCH 17/31] Improve docblock for object arrays. --- src/Entities/Message.php | 6 +++--- src/Entities/UserProfilePhotos.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Entities/Message.php b/src/Entities/Message.php index afb278eb4..b437e4b95 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -103,7 +103,7 @@ public function __construct(array $data, $bot_name = '') * This method overrides the default getPhoto method * and returns a nice array of PhotoSize objects. * - * @return null|array + * @return null|PhotoSize[] */ public function getPhoto() { @@ -118,7 +118,7 @@ public function getPhoto() * This method overrides the default getNewChatPhoto method * and returns a nice array of PhotoSize objects. * - * @return null|array + * @return null|PhotoSize[] */ public function getNewChatPhoto() { @@ -133,7 +133,7 @@ public function getNewChatPhoto() * This method overrides the default getEntities method * and returns a nice array of MessageEntity objects. * - * @return null|array + * @return null|MessageEntity[] */ public function getEntities() { diff --git a/src/Entities/UserProfilePhotos.php b/src/Entities/UserProfilePhotos.php index 19d10b57a..29c3d4d4a 100644 --- a/src/Entities/UserProfilePhotos.php +++ b/src/Entities/UserProfilePhotos.php @@ -34,7 +34,7 @@ protected function subEntities() * * This method overrides the default getPhotos method and returns a nice array * - * @return array + * @return PhotoSize[] */ public function getPhotos() { From d1b4f1cedeb414beeddd1341152b8cf7781ad8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 18:46:57 +0300 Subject: [PATCH 18/31] Fix sendtochannel command to use updated code and use new Keyboard entities. --- .../AdminCommands/SendtochannelCommand.php | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Commands/AdminCommands/SendtochannelCommand.php b/src/Commands/AdminCommands/SendtochannelCommand.php index 1ac187ea8..158f4fc49 100644 --- a/src/Commands/AdminCommands/SendtochannelCommand.php +++ b/src/Commands/AdminCommands/SendtochannelCommand.php @@ -10,12 +10,11 @@ namespace Longman\TelegramBot\Commands\AdminCommands; +use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Request; use Longman\TelegramBot\Conversation; use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Entities\Message; -use Longman\TelegramBot\Entities\ReplyKeyboardHide; -use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; use Longman\TelegramBot\Exception\TelegramException; class SendtochannelCommand extends AdminCommand @@ -67,7 +66,7 @@ public function execute() $type = $message->getType(); // 'Cast' the command type into message to protect the machine state // if the commmad is recalled when the conversation is already started - $type = ($type === 'command') ? 'Message' : $type; + in_array($type, ['command', 'text'], true) && $type = 'message'; $text = trim($message->getText(true)); $text_yes_or_no = ($text === 'Yes' || $text === 'No'); @@ -78,7 +77,9 @@ public function execute() // Conversation $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); - $notes = &$this->conversation->notes; + + $notes = &$this->conversation->notes; + !is_array($notes) && $notes = []; $channels = (array)$this->getConfig('your_channel'); if (isset($notes['state'])) { @@ -91,12 +92,12 @@ public function execute() switch ($state) { case -1: // getConfig has not been configured asking for channel to administer - if ($type !== 'Message' || $text === '') { + if ($type !== 'message' || $text === '') { $notes['state'] = -1; $this->conversation->update(); $data['text'] = 'Insert the channel name: (@yourchannel)'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; @@ -110,7 +111,7 @@ public function execute() default: case 0: // getConfig has been configured choose channel - if ($type !== 'Message' || !in_array($text, $channels, true)) { + if ($type !== 'message' || !in_array($text, $channels, true)) { $notes['state'] = 0; $this->conversation->update(); @@ -118,7 +119,7 @@ public function execute() foreach ($channels as $channel) { $keyboard[] = [$channel]; } - $data['reply_markup'] = new ReplyKeyboardMarkup( + $data['reply_markup'] = new Keyboard( [ 'keyboard' => $keyboard, 'resize_keyboard' => true, @@ -137,11 +138,11 @@ public function execute() // no break case 1: insert: - if (($type === 'Message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { + if (($type === 'message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { $notes['state'] = 1; $this->conversation->update(); - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $data['text'] = 'Insert the content you want to share: text, photo, audio...'; $result = Request::sendMessage($data); break; @@ -156,8 +157,8 @@ public function execute() $this->conversation->update(); // Execute this just with object that allow caption - if ($notes['message_type'] === 'Video' || $notes['message_type'] === 'Photo') { - $data['reply_markup'] = new ReplyKeyboardMarkup( + if (in_array($notes['message_type'], ['video', 'photo'], true)) { + $data['reply_markup'] = new Keyboard( [ 'keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, @@ -178,12 +179,12 @@ public function execute() $notes['last_message_id'] = $message->getMessageId(); // no break case 3: - if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'Message')) { + if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'message')) { $notes['state'] = 3; $this->conversation->update(); $data['text'] = 'Insert caption:'; - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); $result = Request::sendMessage($data); break; } @@ -204,7 +205,7 @@ public function execute() } $this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data); - $data['reply_markup'] = new ReplyKeyboardMarkup( + $data['reply_markup'] = new Keyboard( [ 'keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, @@ -226,7 +227,7 @@ public function execute() $notes['last_message_id'] = $message->getMessageId(); // no break case 5: - $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); + $data['reply_markup'] = Keyboard::hide(['selective' => true]); if ($notes['post_message']) { $data['text'] = $this->publish( @@ -265,35 +266,37 @@ public function execute() protected function sendBack(Message $message, array $data) { $type = $message->getType(); - $type = ($type === 'command') ? 'Message' : $type; - if ($type === 'Message') { + in_array($type, ['command', 'text'], true) && $type = 'message'; + + if ($type === 'message') { $data['text'] = $message->getText(true); - } elseif ($type === 'Audio') { + } elseif ($type === 'audio') { $data['audio'] = $message->getAudio()->getFileId(); $data['duration'] = $message->getAudio()->getDuration(); $data['performer'] = $message->getAudio()->getPerformer(); $data['title'] = $message->getAudio()->getTitle(); - } elseif ($type === 'Document') { + } elseif ($type === 'document') { $data['document'] = $message->getDocument()->getFileId(); - } elseif ($type === 'Photo') { + } elseif ($type === 'photo') { $data['photo'] = $message->getPhoto()[0]->getFileId(); - } elseif ($type === 'Sticker') { + } elseif ($type === 'sticker') { $data['sticker'] = $message->getSticker()->getFileId(); - } elseif ($type === 'Video') { + } elseif ($type === 'video') { $data['video'] = $message->getVideo()->getFileId(); - } elseif ($type === 'Voice') { + } elseif ($type === 'voice') { $data['voice'] = $message->getVoice()->getFileId(); - } elseif ($type === 'Location') { + } elseif ($type === 'location') { $data['latitude'] = $message->getLocation()->getLatitude(); $data['longitude'] = $message->getLocation()->getLongitude(); } + $callback_path = 'Longman\TelegramBot\Request'; - $callback_function = 'send' . $type; + $callback_function = 'send' . ucfirst($type); if (!method_exists($callback_path, $callback_function)) { throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.'); } - return call_user_func_array($callback_path . '::' . $callback_function, [$data]); + return $callback_path::$callback_function($data); } /** From d4baa566172db4bf8dba29da1b6306ef43f302bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 19:48:17 +0300 Subject: [PATCH 19/31] Update getWebhookInfo entity to new structure. --- src/Entities/WebhookInfo.php | 79 ++++++------------------------------ src/Request.php | 3 +- 2 files changed, 14 insertions(+), 68 deletions(-) diff --git a/src/Entities/WebhookInfo.php b/src/Entities/WebhookInfo.php index a17a0a033..331ccdd92 100644 --- a/src/Entities/WebhookInfo.php +++ b/src/Entities/WebhookInfo.php @@ -10,74 +10,19 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - +/** + * Class WebhookInfo + * + * @link https://core.telegram.org/bots/api#webhookinfo + * + * @method string getUrl() Webhook URL, may be empty if webhook is not set up + * @method bool getHasCustomCertificate() True, if a custom certificate was provided for webhook certificate checks + * @method int getPendingUpdateCount() Number of updates awaiting delivery + * @method int getLastErrorDate() Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook + * @method string getLastErrorMessage() Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook + * + */ class WebhookInfo extends Entity { - protected $url; // String Webhook URL, may be empty if webhook is not set up - protected $has_custom_certificate; // Boolean True, if a custom certificate was provided for webhook certificate checks - protected $pending_update_count; // Integer Number of updates awaiting delivery - protected $last_error_date; // Integer Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook - protected $last_error_message; // String Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook - - public function __construct(array $data) - { - $this->url = isset($data['url']) ? $data['url'] : null; - $this->has_custom_certificate = isset($data['has_custom_certificate']) ? $data['has_custom_certificate'] : null; - $this->pending_update_count = isset($data['pending_update_count']) ? $data['pending_update_count'] : null; - $this->last_error_date = isset($data['last_error_date']) ? $data['last_error_date'] : null; - $this->last_error_message = isset($data['last_error_message']) ? $data['last_error_message'] : null; - } - - /** - * Webhook URL, may be empty if webhook is not set up. - * - * @return string - */ - public function getUrl() - { - return $this->url; - } - - /** - * True, if a custom certificate was provided for webhook certificate checks. - * - * @return bool - */ - public function getHasCustomCertificate() - { - return $this->has_custom_certificate; - } - - /** - * Number of updates awaiting delivery. - * - * @return int - */ - public function getPendingUpdateCount() - { - return $this->pending_update_count; - } - - /** - * Optional. - * Unix time for the most recent error that happened when trying to deliver an update via webhook. - * - * @return int - */ - public function getLastErrorDate() - { - return $this->last_error_date; - } - /** - * Optional. - * Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook. - * - * @return string - */ - public function getLastErrorMessage() - { - return $this->last_error_message; - } } diff --git a/src/Request.php b/src/Request.php index e4e110371..fd2b3a7ba 100644 --- a/src/Request.php +++ b/src/Request.php @@ -848,9 +848,10 @@ public static function sendToActiveChats( * Use this method to get current webhook status. * * @return Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getWebhookInfo() { - return self::send('getWebhookInfo', []); + return self::send('getWebhookInfo', ['info']); } } From c268c7fd782f10918165fba24b4a2293a343e169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Oct 2016 19:55:44 +0300 Subject: [PATCH 20/31] Use proper InlineKeyboard type hinting for inline queries. [skip ci] --- src/Entities/InlineQuery/InlineQueryResultArticle.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultAudio.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultCachedAudio.php | 8 ++++---- .../InlineQuery/InlineQueryResultCachedDocument.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultCachedGif.php | 8 ++++---- .../InlineQuery/InlineQueryResultCachedMpeg4Gif.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php | 8 ++++---- .../InlineQuery/InlineQueryResultCachedSticker.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultCachedVideo.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultCachedVoice.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultContact.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultDocument.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultGif.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultLocation.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultPhoto.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultVenue.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultVideo.php | 8 ++++---- src/Entities/InlineQuery/InlineQueryResultVoice.php | 8 ++++---- 19 files changed, 76 insertions(+), 76 deletions(-) diff --git a/src/Entities/InlineQuery/InlineQueryResultArticle.php b/src/Entities/InlineQuery/InlineQueryResultArticle.php index d8abff49c..f4b74a156 100644 --- a/src/Entities/InlineQuery/InlineQueryResultArticle.php +++ b/src/Entities/InlineQuery/InlineQueryResultArticle.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -23,7 +23,7 @@ * 'id' => '', * 'title' => '', * 'input_message_content' => , - * 'reply_markup' => , + * 'reply_markup' => , * 'url' => '', * 'hide_url' => true, * 'description' => '', @@ -37,7 +37,7 @@ * @method string getId() Unique identifier for this result, 1-64 Bytes * @method string getTitle() Title of the result * @method InputMessageContent getInputMessageContent() Content of the message to be sent - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method string getUrl() Optional. URL of the result * @method bool getHideUrl() Optional. Pass True, if you don't want the URL to be shown in the message * @method string getDescription() Optional. Short description of the result @@ -48,7 +48,7 @@ * @method $this setId(string $id) Unique identifier for this result, 1-64 Bytes * @method $this setTitle(string $title) Title of the result * @method $this setInputMessageContent(InputMessageContent $input_message_content) Content of the message to be sent - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setUrl(string $url) Optional. URL of the result * @method $this setHideUrl(bool $hide_url) Optional. Pass True, if you don't want the URL to be shown in the message * @method $this setDescription(string $description) Optional. Short description of the result diff --git a/src/Entities/InlineQuery/InlineQueryResultAudio.php b/src/Entities/InlineQuery/InlineQueryResultAudio.php index 1fb4cb661..891bd679f 100644 --- a/src/Entities/InlineQuery/InlineQueryResultAudio.php +++ b/src/Entities/InlineQuery/InlineQueryResultAudio.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -25,7 +25,7 @@ * 'title' => '', * 'performer' => '', * 'audio_duration' => 123, - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -36,7 +36,7 @@ * @method string getTitle() Title * @method string getPerformer() Optional. Performer * @method int getAudioDuration() Optional. Audio duration in seconds - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -44,7 +44,7 @@ * @method $this setTitle(string $title) Title * @method $this setPerformer(string $performer) Optional. Performer * @method $this setAudioDuration(int $audio_duration) Optional. Audio duration in seconds - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio */ class InlineQueryResultAudio extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php index e7c10a9a5..ed64020b5 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedAudio.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -22,7 +22,7 @@ * $data = [ * 'id' => '', * 'audio_file_id' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -30,12 +30,12 @@ * @method string getType() Type of the result, must be audio * @method string getId() Unique identifier for this result, 1-64 bytes * @method string getAudioFileId() A valid file identifier for the audio file - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the audio * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setAudioFileId(string $audio_file_id) A valid file identifier for the audio file - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio */ class InlineQueryResultCachedAudio extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php index e493aeefb..89fbf9c42 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedDocument.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -25,7 +25,7 @@ * 'document_file_id' => '', * 'description' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -36,7 +36,7 @@ * @method string getDocumentFileId() A valid file identifier for the file * @method string getDescription() Optional. Short description of the result * @method string getCaption() Optional. Caption of the document to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -44,7 +44,7 @@ * @method $this setDocumentFileId(string $document_file_id) A valid file identifier for the file * @method $this setDescription(string $description) Optional. Short description of the result * @method $this setCaption(string $caption) Optional. Caption of the document to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file */ class InlineQueryResultCachedDocument extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php index de214cd68..9c4192faf 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedGif.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedGif.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -24,7 +24,7 @@ * 'gif_file_id' => '', * 'title' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -34,14 +34,14 @@ * @method string getGifFileId() A valid file identifier for the GIF file * @method string getTitle() Optional. Title for the result * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setGifFileId(string $gif_file_id) A valid file identifier for the GIF file * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation */ class InlineQueryResultCachedGif extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php index 10aa53df8..8d52efdc0 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedMpeg4Gif.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -24,7 +24,7 @@ * 'mpeg4_file_id' => '', * 'title' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -34,14 +34,14 @@ * @method string getMpeg4FileId() A valid file identifier for the MP4 file * @method string getTitle() Optional. Title for the result * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setMpeg4FileId(string $mpeg4_file_id) A valid file identifier for the MP4 file * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation */ class InlineQueryResultCachedMpeg4Gif extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php index 042c07954..31f614901 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedPhoto.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -25,7 +25,7 @@ * 'title' => '', * 'description' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -36,7 +36,7 @@ * @method string getTitle() Optional. Title for the result * @method string getDescription() Optional. Short description of the result * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -44,7 +44,7 @@ * @method $this setTitle(string $title) Optional. Title for the result * @method $this setDescription(string $description) Optional. Short description of the result * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo */ class InlineQueryResultCachedPhoto extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php index d984dd655..1f7c1d4fe 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedSticker.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -22,7 +22,7 @@ * $data = [ * 'id' => '', * 'sticker_file_id' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -30,12 +30,12 @@ * @method string getType() Type of the result, must be sticker * @method string getId() Unique identifier for this result, 1-64 bytes * @method string getStickerFileId() A valid file identifier of the sticker - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the sticker * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setStickerFileId(string $sticker_file_id) A valid file identifier of the sticker - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the sticker */ class InlineQueryResultCachedSticker extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php index 56c3f9039..3173fe483 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVideo.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -25,7 +25,7 @@ * 'title' => '', * 'description' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -36,7 +36,7 @@ * @method string getTitle() Title for the result * @method string getDescription() Optional. Short description of the result * @method string getCaption() Optional. Caption of the video to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -44,7 +44,7 @@ * @method $this setTitle(string $title) Title for the result * @method $this setDescription(string $description) Optional. Short description of the result * @method $this setCaption(string $caption) Optional. Caption of the video to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video */ class InlineQueryResultCachedVideo extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php index 4305e8b9a..8e03507df 100644 --- a/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php +++ b/src/Entities/InlineQuery/InlineQueryResultCachedVoice.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -23,7 +23,7 @@ * 'id' => '', * 'voice_file_id' => '', * 'title' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -32,13 +32,13 @@ * @method string getId() Unique identifier for this result, 1-64 bytes * @method string getVoiceFileId() A valid file identifier for the voice message * @method string getTitle() Voice message title - * @method InlineKeyboardMarkup getReplyMarkup() Optional. An Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. An Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice message * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setVoiceFileId(string $voice_file_id) A valid file identifier for the voice message * @method $this setTitle(string $title) Voice message title - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. An Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice message */ class InlineQueryResultCachedVoice extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultContact.php b/src/Entities/InlineQuery/InlineQueryResultContact.php index b46d2ad57..18d7e5a1c 100644 --- a/src/Entities/InlineQuery/InlineQueryResultContact.php +++ b/src/Entities/InlineQuery/InlineQueryResultContact.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -24,7 +24,7 @@ * 'phone_number' => '', * 'first_name' => '', * 'last_name' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * 'thumb_url' => '', * 'thumb_width' => 30, @@ -37,7 +37,7 @@ * @method string getPhoneNumber() Contact's phone number * @method string getFirstName() Contact's first name * @method string getLastName() Optional. Contact's last name - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the contact * @method string getThumbUrl() Optional. Url of the thumbnail for the result * @method int getThumbWidth() Optional. Thumbnail width @@ -47,7 +47,7 @@ * @method $this setPhoneNumber(string $phone_number) Contact's phone number * @method $this setFirstName(string $first_name) Contact's first name * @method $this setLastName(string $last_name) Optional. Contact's last name - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the contact * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width diff --git a/src/Entities/InlineQuery/InlineQueryResultDocument.php b/src/Entities/InlineQuery/InlineQueryResultDocument.php index 22e2be560..7fa084ca8 100644 --- a/src/Entities/InlineQuery/InlineQueryResultDocument.php +++ b/src/Entities/InlineQuery/InlineQueryResultDocument.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -26,7 +26,7 @@ * 'document_url' => '', * 'mime_type' => '', * 'description' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * 'thumb_url' => '', * 'thumb_width' => 30, @@ -41,7 +41,7 @@ * @method string getDocumentUrl() A valid URL for the file * @method string getMimeType() Mime type of the content of the file, either “application/pdf” or “application/zip” * @method string getDescription() Optional. Short description of the result - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the file * @method string getThumbUrl() Optional. URL of the thumbnail (jpeg only) for the file * @method int getThumbWidth() Optional. Thumbnail width @@ -53,7 +53,7 @@ * @method $this setDocumentUrl(string $document_url) A valid URL for the file * @method $this setMimeType(string $mime_type) Mime type of the content of the file, either “application/pdf” or “application/zip” * @method $this setDescription(string $description) Optional. Short description of the result - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file * @method $this setThumbUrl(string $thumb_url) Optional. URL of the thumbnail (jpeg only) for the file * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width diff --git a/src/Entities/InlineQuery/InlineQueryResultGif.php b/src/Entities/InlineQuery/InlineQueryResultGif.php index 4bff65617..e7cc2ed4f 100644 --- a/src/Entities/InlineQuery/InlineQueryResultGif.php +++ b/src/Entities/InlineQuery/InlineQueryResultGif.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -27,7 +27,7 @@ * 'thumb_url' => '', * 'title' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -40,7 +40,7 @@ * @method string getThumbUrl() URL of the static thumbnail for the result (jpeg or gif) * @method string getTitle() Optional. Title for the result * @method string getCaption() Optional. Caption of the GIF file to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the GIF animation * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -50,7 +50,7 @@ * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail for the result (jpeg or gif) * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the GIF file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation */ class InlineQueryResultGif extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultLocation.php b/src/Entities/InlineQuery/InlineQueryResultLocation.php index f1d912493..89a036ab7 100644 --- a/src/Entities/InlineQuery/InlineQueryResultLocation.php +++ b/src/Entities/InlineQuery/InlineQueryResultLocation.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -24,7 +24,7 @@ * 'latitude' => 36.0338, * 'longitude' => 71.8601, * 'title' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * 'thumb_url' => '', * 'thumb_width' => 30, @@ -37,7 +37,7 @@ * @method float getLatitude() Location latitude in degrees * @method float getLongitude() Location longitude in degrees * @method string getTitle() Location title - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the location * @method string getThumbUrl() Optional. Url of the thumbnail for the result * @method int getThumbWidth() Optional. Thumbnail width @@ -47,7 +47,7 @@ * @method $this setLatitude(float $latitude) Location latitude in degrees * @method $this setLongitude(float $longitude) Location longitude in degrees * @method $this setTitle(string $title) Location title - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the location * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width diff --git a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php index 08a61dc61..9eae124e0 100644 --- a/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php +++ b/src/Entities/InlineQuery/InlineQueryResultMpeg4Gif.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -27,7 +27,7 @@ * 'thumb_url' => '', * 'title' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -40,7 +40,7 @@ * @method string getThumbUrl() URL of the static thumbnail (jpeg or gif) for the result * @method string getTitle() Optional. Title for the result * @method string getCaption() Optional. Caption of the MPEG-4 file to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video animation * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -50,7 +50,7 @@ * @method $this setThumbUrl(string $thumb_url) URL of the static thumbnail (jpeg or gif) for the result * @method $this setTitle(string $title) Optional. Title for the result * @method $this setCaption(string $caption) Optional. Caption of the MPEG-4 file to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation */ class InlineQueryResultMpeg4Gif extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultPhoto.php b/src/Entities/InlineQuery/InlineQueryResultPhoto.php index dd310f206..5fd8ea724 100644 --- a/src/Entities/InlineQuery/InlineQueryResultPhoto.php +++ b/src/Entities/InlineQuery/InlineQueryResultPhoto.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -28,7 +28,7 @@ * 'title' => '', * 'description' => '', * 'caption' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -42,7 +42,7 @@ * @method string getTitle() Optional. Title for the result * @method string getDescription() Optional. Short description of the result * @method string getCaption() Optional. Caption of the photo to be sent, 0-200 characters - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the photo * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -53,7 +53,7 @@ * @method $this setTitle(string $title) Optional. Title for the result * @method $this setDescription(string $description) Optional. Short description of the result * @method $this setCaption(string $caption) Optional. Caption of the photo to be sent, 0-200 characters - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo */ class InlineQueryResultPhoto extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultVenue.php b/src/Entities/InlineQuery/InlineQueryResultVenue.php index 8e4fdbed8..c9df43826 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVenue.php +++ b/src/Entities/InlineQuery/InlineQueryResultVenue.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -26,7 +26,7 @@ * 'title' => '', * 'address' => '', * 'foursquare_id' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * 'thumb_url' => '', * 'thumb_width' => 30, @@ -41,7 +41,7 @@ * @method string getTitle() Title of the venue * @method string getAddress() Address of the venue * @method string getFoursquareId() Optional. Foursquare identifier of the venue if known - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the venue * @method string getThumbUrl() Optional. Url of the thumbnail for the result * @method int getThumbWidth() Optional. Thumbnail width @@ -53,7 +53,7 @@ * @method $this setTitle(string $title) Title of the venue * @method $this setAddress(string $address) Address of the venue * @method $this setFoursquareId(string $foursquare_id) Optional. Foursquare identifier of the venue if known - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the venue * @method $this setThumbUrl(string $thumb_url) Optional. Url of the thumbnail for the result * @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width diff --git a/src/Entities/InlineQuery/InlineQueryResultVideo.php b/src/Entities/InlineQuery/InlineQueryResultVideo.php index a032ff30b..7f6fb5d16 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVideo.php +++ b/src/Entities/InlineQuery/InlineQueryResultVideo.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -30,7 +30,7 @@ * 'video_height' => 30, * 'video_duration' => 123, * 'description' => '', - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -46,7 +46,7 @@ * @method int getVideoHeight() Optional. Video height * @method int getVideoDuration() Optional. Video duration in seconds * @method string getDescription() Optional. Short description of the result - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the video * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes @@ -59,7 +59,7 @@ * @method $this setVideoHeight(int $video_height) Optional. Video height * @method $this setVideoDuration(int $video_duration) Optional. Video duration in seconds * @method $this setDescription(string $description) Optional. Short description of the result - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video */ class InlineQueryResultVideo extends InlineEntity diff --git a/src/Entities/InlineQuery/InlineQueryResultVoice.php b/src/Entities/InlineQuery/InlineQueryResultVoice.php index 6220c2961..5996c1765 100644 --- a/src/Entities/InlineQuery/InlineQueryResultVoice.php +++ b/src/Entities/InlineQuery/InlineQueryResultVoice.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Entities\InlineQuery; -use Longman\TelegramBot\Entities\InlineKeyboardMarkup; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent; /** @@ -24,7 +24,7 @@ * 'voice_url' => '', * 'title' => '', * 'voice_duration' => 123, - * 'reply_markup' => , + * 'reply_markup' => , * 'input_message_content' => , * ]; * @@ -34,14 +34,14 @@ * @method string getVoiceUrl() A valid URL for the voice recording * @method string getTitle() Recording title * @method int getVoiceDuration() Optional. Recording duration in seconds - * @method InlineKeyboardMarkup getReplyMarkup() Optional. Inline keyboard attached to the message + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message * @method InputMessageContent getInputMessageContent() Optional. Content of the message to be sent instead of the voice recording * * @method $this setId(string $id) Unique identifier for this result, 1-64 bytes * @method $this setVoiceUrl(string $voice_url) A valid URL for the voice recording * @method $this setTitle(string $title) Recording title * @method $this setVoiceDuration(int $voice_duration) Optional. Recording duration in seconds - * @method $this setReplyMarkup(InlineKeyboardMarkup $reply_markup) Optional. Inline keyboard attached to the message + * @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message * @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice recording */ class InlineQueryResultVoice extends InlineEntity From d2205167dde0172cdcde383aee6b07a497a76b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 00:22:43 +0300 Subject: [PATCH 21/31] Move tryMention to the Entity class. Rename stripMarkDown to escapeMarkdown to give it more sense. --- src/Entities/Chat.php | 35 ++++++++++-------------- src/Entities/Entity.php | 46 +++++++++++++++++++++++++++++--- src/Entities/User.php | 33 ----------------------- tests/unit/Entities/UserTest.php | 9 +++---- 4 files changed, 61 insertions(+), 62 deletions(-) diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index 9bbf58880..584ae2e11 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -34,32 +34,25 @@ public function __construct($data) { parent::__construct($data); - if (!$this->getType()) { - if ($this->getId() > 0) { - $this->type = 'private'; - } elseif ($this->getId() < 0) { - $this->type = 'group'; - } + $id = $this->getId(); + $type = $this->getType(); + if (!$type && $id !== 0) { + $id > 0 && $this->type = 'private'; + $id < 0 && $this->type = 'group'; } } /** - * Try mention + * Try to mention the user of this chat, else return the title + * + * @param bool $escape_markdown * * @return string|null */ - public function tryMention() + public function tryMention($escape_markdown = false) { if ($this->isPrivateChat()) { - if ($this->username === null) { - if ($this->last_name !== null) { - return $this->first_name . ' ' . $this->last_name; - } - - return $this->first_name; - } - - return '@' . $this->username; + return $this->tryMention($escape_markdown); } return $this->getTitle(); @@ -72,7 +65,7 @@ public function tryMention() */ public function isGroupChat() { - return $this->type === 'group' || $this->id < 0; + return $this->getType() === 'group' || $this->getId() < 0; } /** @@ -82,7 +75,7 @@ public function isGroupChat() */ public function isPrivateChat() { - return $this->type === 'private'; + return $this->getType() === 'private'; } /** @@ -92,7 +85,7 @@ public function isPrivateChat() */ public function isSuperGroup() { - return $this->type === 'supergroup'; + return $this->getType() === 'supergroup'; } /** @@ -102,6 +95,6 @@ public function isSuperGroup() */ public function isChannel() { - return $this->type === 'channel'; + return $this->getType() === 'channel'; } } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 64b9247b7..e51f7c33b 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -268,14 +268,13 @@ protected function makePrettyObjectArray($class, $property) } /** - * stripMarkDown - * Gived a string escape special charactes used in Markdown + * Escape markdown special characters * * @param string $string * * @return string */ - public function stripMarkDown($string) + public function escapeMarkdown($string) { return str_replace( ['[', '`', '*', '_',], @@ -283,4 +282,45 @@ public function stripMarkDown($string) $string ); } + + /** + * Try to mention the user + * + * Mention the user with the username otherwise print first and last name + * if the $escape_markdown argument is true special characters are escaped from the output + * + * @param bool $escape_markdown + * + * @return string|null + */ + public function tryMention($escape_markdown = false) + { + //TryMention only makes sense for the User and Chat entity. + if (!($this instanceof User || $this instanceof Chat)) { + return null; + } + + //Try with the username first... + $username = $this->getProperty('username'); + if ($username !== null) { + if ($escape_markdown) { + $username = $this->escapeMarkdown($username); + } + + return '@' . $username; + } + + //...otherwise try with the names + $name = $this->getProperty('first_name'); + $last_name = $this->getProperty('last_name'); + if ($last_name !== null) { + $name .= ' ' . $last_name; + } + + if ($escape_markdown) { + $name = $this->escapeMarkdown($name); + } + + return $name; + } } diff --git a/src/Entities/User.php b/src/Entities/User.php index 31304aa96..ee1780322 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -27,38 +27,5 @@ */ class User extends Entity { - /** - * tryMention - * - * Mention the user with the username otherwise print first and last name - * if the $markdown arguments is true special characters are escaped from the output - * - * @param bool $markdown - * - * @return string - */ - public function tryMention($markdown = false) - { - $username = $this->getProperty('username'); - if ($username !== null) { - if ($markdown) { - //Escaping md special characters - //Please notice that just the _ is allowed in the username ` * [ are not allowed - return '@' . $this->stripMarkDown($this->username); - } - return '@' . $this->username; - } - $name = $this->getProperty('first_name'); - $last_name = $this->getProperty('last_name'); - if ($last_name !== null) { - $name .= ' ' . $last_name; - } - - if ($markdown) { - //Escaping md special characters - return $this->stripMarkDown($name); - } - return $name; - } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php index 701515de3..a21660a0c 100644 --- a/tests/unit/Entities/UserTest.php +++ b/tests/unit/Entities/UserTest.php @@ -48,12 +48,8 @@ public function testTryMention() self::assertEquals('John Taylor', $user->tryMention()); } - public function testStripMarkDown() + public function testEscapeMarkdown() { - // Plain stripMarkDown functionality. - $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']); - self::assertEquals('\`\[\*\_', $user->stripMarkDown('`[*_')); - // Username. $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); self::assertEquals('@j_taylor', $user->tryMention()); @@ -68,6 +64,9 @@ public function testStripMarkDown() $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']); self::assertEquals('John `Taylor`', $user->tryMention()); self::assertEquals('John \`Taylor\`', $user->tryMention(true)); + + // Plain escapeMarkdown functionality. + self::assertEquals('a\`b\[c\*d\_e', $user->escapeMarkdown('a`b[c*d_e')); } public function testGetProperties() From 01b137cbf8f521a2c2f0d18ada535b4f5847da88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 00:26:10 +0300 Subject: [PATCH 22/31] Fix ReplyToMessage class, as data wasn't set properly. --- src/Entities/ReplyToMessage.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Entities/ReplyToMessage.php b/src/Entities/ReplyToMessage.php index 03848a52b..8e55e36e7 100644 --- a/src/Entities/ReplyToMessage.php +++ b/src/Entities/ReplyToMessage.php @@ -10,22 +10,27 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ReplyToMessage + * + * @todo Is this even required?! + */ class ReplyToMessage extends Message { - /** * ReplyToMessage constructor. * * @param array $data * @param string $bot_name + * + * @throws \Longman\TelegramBot\Exception\TelegramException */ public function __construct(array $data, $bot_name = '') { - //As explained in the documentation //Reply to message can't contain other reply to message entities - $reply_to_message = null; + unset($data['reply_to_message']); - $this->init($data, $bot_name); + parent::__construct($data, $bot_name); } } From 27cc9e9caed662b7ee5e52e4ce30be959c8c5f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 00:30:36 +0300 Subject: [PATCH 23/31] Fix tryMention for Chat entity and add extra Chat tests. --- src/Entities/Chat.php | 2 +- tests/unit/Entities/ChatTest.php | 54 +++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index 584ae2e11..139eb1f03 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -52,7 +52,7 @@ public function __construct($data) public function tryMention($escape_markdown = false) { if ($this->isPrivateChat()) { - return $this->tryMention($escape_markdown); + return parent::tryMention($escape_markdown); } return $this->getTitle(); diff --git a/tests/unit/Entities/ChatTest.php b/tests/unit/Entities/ChatTest.php index da4931e11..46520f5c6 100644 --- a/tests/unit/Entities/ChatTest.php +++ b/tests/unit/Entities/ChatTest.php @@ -19,20 +19,52 @@ */ class ChatTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Chat - */ - private $chat; - public function testChatType() { - $this->chat = TestHelpers::getFakeChatObject(); - $this->assertEquals('private', $this->chat->getType()); + $chat = TestHelpers::getFakeChatObject(); + self::assertEquals('private', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); + self::assertEquals('group', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'supergroup']); + self::assertEquals('supergroup', $chat->getType()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); + self::assertEquals('channel', $chat->getType()); + } + + public function testIsChatType() + { + $chat = TestHelpers::getFakeChatObject(); + self::assertTrue($chat->isPrivateChat()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); + self::assertTrue($chat->isGroupChat()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'supergroup']); + self::assertTrue($chat->isSuperGroup()); + + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); + self::assertTrue($chat->isChannel()); + } + + public function testTryMention() + { + // Username. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']); + self::assertEquals('@jtaylor', $chat->tryMention()); + + // First name. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => null, 'username' => null]); + self::assertEquals('John', $chat->tryMention()); - $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); - $this->assertEquals('group', $this->chat->getType()); + // First and Last name. + $chat = TestHelpers::getFakeChatObject(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => null]); + self::assertEquals('John Taylor', $chat->tryMention()); - $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); - $this->assertEquals('channel', $this->chat->getType()); + // Non-private chat should return title. + $chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null, 'title' => 'My group chat']); + self::assertSame('My group chat', $chat->tryMention()); } } From a4ea86d6a62177becf461cc193bb93d7d7dcd549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 00:50:08 +0300 Subject: [PATCH 24/31] Update Message tests and add initial ones for getType. --- tests/unit/Entities/MessageTest.php | 102 +++++++++++++++------------- 1 file changed, 55 insertions(+), 47 deletions(-) diff --git a/tests/unit/Entities/MessageTest.php b/tests/unit/Entities/MessageTest.php index 35a4ffd16..639ff0b13 100644 --- a/tests/unit/Entities/MessageTest.php +++ b/tests/unit/Entities/MessageTest.php @@ -19,66 +19,74 @@ */ class MessageTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $message; - - public function testTextAndCommandRecognise() { + public function testTextAndCommandRecognise() + { // /command - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help']); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help', $this->message->getText()); - $this->assertEquals('', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help']); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help', $message->getText()); + self::assertEquals('', $message->getText(true)); // text - $this->message = TestHelpers::getFakeMessageObject(['text' => 'some text']); - $this->assertEquals('', $this->message->getFullCommand()); - $this->assertEquals('', $this->message->getCommand()); - $this->assertEquals('some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => 'some text']); + self::assertEquals('', $message->getFullCommand()); + self::assertEquals('', $message->getCommand()); + self::assertEquals('some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help@testbot', $this->message->getText()); - $this->assertEquals('', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help@testbot', $message->getText()); + self::assertEquals('', $message->getText(true)); // /commmad text - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help some text']); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help some text']); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot some text - $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals('/help@testbot some text', $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals('/help@testbot some text', $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /commmad\n text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]); - $this->assertEquals('/help', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help\n some text", $this->message->getText()); - $this->assertEquals(' some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]); + self::assertEquals('/help', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help\n some text", $message->getText()); + self::assertEquals(' some text', $message->getText(true)); // /command@bot\nsome text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help@testbot\nsome text", $this->message->getText()); - $this->assertEquals('some text', $this->message->getText(true)); + $message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help@testbot\nsome text", $message->getText()); + self::assertEquals('some text', $message->getText(true)); // /command@bot \nsome text - $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]); - $this->assertEquals('/help@testbot', $this->message->getFullCommand()); - $this->assertEquals('help', $this->message->getCommand()); - $this->assertEquals("/help@testbot \nsome text", $this->message->getText()); - $this->assertEquals("\nsome text", $this->message->getText(true)); - } + $message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]); + self::assertEquals('/help@testbot', $message->getFullCommand()); + self::assertEquals('help', $message->getCommand()); + self::assertEquals("/help@testbot \nsome text", $message->getText()); + self::assertEquals("\nsome text", $message->getText(true)); + } + + public function testGetType() + { + $message = TestHelpers::getFakeMessageObject(['text' => null]); + self::assertSame('message', $message->getType()); + + $message = TestHelpers::getFakeMessageObject(['text' => '/help']); + self::assertSame('command', $message->getType()); + + $message = TestHelpers::getFakeMessageObject(['text' => 'some text']); + self::assertSame('text', $message->getType()); + } } From 1cfa47799860739e4645fb13b9549dee905365a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 01:02:33 +0300 Subject: [PATCH 25/31] Optimise tryMention a bit, removing duplicate code. --- src/Entities/Entity.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index e51f7c33b..85eda30b4 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -301,26 +301,22 @@ public function tryMention($escape_markdown = false) } //Try with the username first... - $username = $this->getProperty('username'); - if ($username !== null) { - if ($escape_markdown) { - $username = $this->escapeMarkdown($username); + $name = $this->getProperty('username'); + $is_username = $name !== null; + + if ($name === null) { + //...otherwise try with the names. + $name = $this->getProperty('first_name'); + $last_name = $this->getProperty('last_name'); + if ($last_name !== null) { + $name .= ' ' . $last_name; } - - return '@' . $username; - } - - //...otherwise try with the names - $name = $this->getProperty('first_name'); - $last_name = $this->getProperty('last_name'); - if ($last_name !== null) { - $name .= ' ' . $last_name; } if ($escape_markdown) { $name = $this->escapeMarkdown($name); } - return $name; + return ($is_username ? '@' : '') . $name; } } From 1ff9b4f99552364e7b72c9acbb57d1032ced290d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 10:26:30 +0300 Subject: [PATCH 26/31] Simplify Keyboard entities and tests to remove duplicate code. Small optimisation for Chat entity. --- src/Entities/Chat.php | 2 +- src/Entities/InlineKeyboard.php | 30 ---------------------- src/Entities/Keyboard.php | 7 ++--- tests/unit/Entities/InlineKeyboardTest.php | 4 +-- tests/unit/Entities/KeyboardTest.php | 4 +-- 5 files changed, 9 insertions(+), 38 deletions(-) diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index 139eb1f03..58ac876d3 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -36,7 +36,7 @@ public function __construct($data) $id = $this->getId(); $type = $this->getType(); - if (!$type && $id !== 0) { + if (!$type) { $id > 0 && $this->type = 'private'; $id < 0 && $this->type = 'group'; } diff --git a/src/Entities/InlineKeyboard.php b/src/Entities/InlineKeyboard.php index ce14023f6..1981abb69 100644 --- a/src/Entities/InlineKeyboard.php +++ b/src/Entities/InlineKeyboard.php @@ -10,8 +10,6 @@ namespace Longman\TelegramBot\Entities; -use Longman\TelegramBot\Exception\TelegramException; - /** * Class InlineKeyboard * @@ -19,32 +17,4 @@ */ class InlineKeyboard extends Keyboard { - /** - * {@inheritdoc} - */ - public function __construct($data = []) - { - $data = call_user_func_array([$this, 'createFromParams'], func_get_args()); - parent::__construct($data); - } - - /** - * {@inheritdoc} - */ - protected function validate() - { - $inline_keyboard = $this->getProperty('inline_keyboard'); - - if ($inline_keyboard !== null) { - if (!is_array($inline_keyboard)) { - throw new TelegramException('Inline Keyboard field is not an array!'); - } - - foreach ($inline_keyboard as $item) { - if (!is_array($item)) { - throw new TelegramException('Inline Keyboard subfield is not an array!'); - } - } - } - } } diff --git a/src/Entities/Keyboard.php b/src/Entities/Keyboard.php index 0edeada0f..63c3419f7 100644 --- a/src/Entities/Keyboard.php +++ b/src/Entities/Keyboard.php @@ -178,16 +178,17 @@ protected function parseButton($button) */ protected function validate() { - $keyboard = $this->getProperty('keyboard'); + $keyboard_type = $this->getKeyboardType(); + $keyboard = $this->getProperty($keyboard_type); if ($keyboard !== null) { if (!is_array($keyboard)) { - throw new TelegramException('Keyboard field is not an array!'); + throw new TelegramException($keyboard_type . ' field is not an array!'); } foreach ($keyboard as $item) { if (!is_array($item)) { - throw new TelegramException('Keyboard subfield is not an array!'); + throw new TelegramException($keyboard_type . ' subfield is not an array!'); } } } diff --git a/tests/unit/Entities/InlineKeyboardTest.php b/tests/unit/Entities/InlineKeyboardTest.php index e88c46e78..731e4a628 100644 --- a/tests/unit/Entities/InlineKeyboardTest.php +++ b/tests/unit/Entities/InlineKeyboardTest.php @@ -36,7 +36,7 @@ private function getRandomButton($text) /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage Inline Keyboard field is not an array! + * @expectedExceptionMessage inline_keyboard field is not an array! */ public function testInlineKeyboardDataMalformedField() { @@ -45,7 +45,7 @@ public function testInlineKeyboardDataMalformedField() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage Inline Keyboard subfield is not an array! + * @expectedExceptionMessage inline_keyboard subfield is not an array! */ public function testInlineKeyboardDataMalformedSubfield() { diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php index ca902262b..bc3024f65 100644 --- a/tests/unit/Entities/KeyboardTest.php +++ b/tests/unit/Entities/KeyboardTest.php @@ -25,7 +25,7 @@ class KeyboardTest extends TestCase { /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage Keyboard field is not an array! + * @expectedExceptionMessage keyboard field is not an array! */ public function testKeyboardDataMalformedField() { @@ -34,7 +34,7 @@ public function testKeyboardDataMalformedField() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage Keyboard subfield is not an array! + * @expectedExceptionMessage keyboard subfield is not an array! */ public function testKeyboardDataMalformedSubfield() { From 879ceb9970123106917dd5c5082dce2cd88b87c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 12:43:45 +0300 Subject: [PATCH 27/31] Return body on request exception instead of throwing TelegramException. Save raw JSON response to ServerResponse object. --- src/Entities/ServerResponse.php | 1 + src/Request.php | 16 +++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Entities/ServerResponse.php b/src/Entities/ServerResponse.php index 982fe8e4c..144dfbe76 100644 --- a/src/Entities/ServerResponse.php +++ b/src/Entities/ServerResponse.php @@ -40,6 +40,7 @@ class ServerResponse extends Entity */ public function __construct(array $data, $bot_name) { + $this->raw_json = isset($data['raw_json']) ? $data['raw_json'] : null; if (isset($data['ok'], $data['result'])) { if (is_array($data['result'])) { if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { diff --git a/src/Request.php b/src/Request.php index fd2b3a7ba..a49c2c8db 100644 --- a/src/Request.php +++ b/src/Request.php @@ -223,14 +223,14 @@ public static function execute($action, array $data = []) if ($action === 'getUpdates') { TelegramLog::update($result); } - - return $result; } catch (RequestException $e) { - throw new TelegramException($e->getMessage()); + $result = (string)$e->getResponse()->getBody(); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); } + + return $result; } /** @@ -263,7 +263,7 @@ public static function downloadFile(File $file) return filesize($file_path) > 0; } catch (RequestException $e) { - throw new TelegramException($e->getMessage()); + return (string)$e->getResponse()->getBody(); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n"); @@ -313,12 +313,14 @@ public static function send($action, array $data = []) self::ensureNonEmptyData($data); - $response = json_decode(self::execute($action, $data), true); + $raw_json = self::execute($action, $data); + $response = json_decode($raw_json, true); if (null === $response) { throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); } + $response['raw_json'] = $raw_json; return new ServerResponse($response, $bot_name); } @@ -574,10 +576,6 @@ public static function sendChatAction(array $data) */ public static function getUserProfilePhotos(array $data) { - if (!isset($data['user_id'])) { - throw new TelegramException('User id is empty!'); - } - return self::send('getUserProfilePhotos', $data); } From 6009277bc1fb7704175b8143420f9445413efcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 18:12:43 +0300 Subject: [PATCH 28/31] Remove entity reflect method and replace it by saving the raw data array which will be used for returning JSON string. --- .../AdminCommands/SendtochannelCommand.php | 4 +- src/Entities/Entity.php | 103 ++---------------- src/Entities/ServerResponse.php | 2 +- src/Request.php | 4 +- 4 files changed, 12 insertions(+), 101 deletions(-) diff --git a/src/Commands/AdminCommands/SendtochannelCommand.php b/src/Commands/AdminCommands/SendtochannelCommand.php index 158f4fc49..d7ae5b0e7 100644 --- a/src/Commands/AdminCommands/SendtochannelCommand.php +++ b/src/Commands/AdminCommands/SendtochannelCommand.php @@ -148,7 +148,7 @@ public function execute() break; } $notes['last_message_id'] = $message->getMessageId(); - $notes['message'] = $message->reflect(); + $notes['message'] = $message->getRawData(); $notes['message_type'] = $type; // no break case 2: @@ -350,7 +350,7 @@ public function executeNoDb() $channels = (array)$this->getConfig('your_channel'); $first_channel = $channels[0]; $data['text'] = $this->publish( - new Message($message->reflect(), $this->telegram->getBotName()), + new Message($message->getRawData(), $this->telegram->getBotName()), $first_channel ); } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 85eda30b4..c3bb4585e 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -18,39 +18,29 @@ * Class Entity * * This is the base class for all entities. + * + * @link https://core.telegram.org/bots/api#available-types + * + * @method array getRawData() Get the raw data passed to this entity */ abstract class Entity { - /** - * @var string - */ - protected $bot_name; - - /** - * Get bot name - * - * @return string - */ - public function getBotName() - { - return $this->bot_name; - } - /** * Entity constructor. * * @todo Get rid of the $bot_name, it shouldn't be here! * - * @param $data + * @param array $data * @param string $bot_name * * @throws \Longman\TelegramBot\Exception\TelegramException */ public function __construct($data, $bot_name = '') { + $data['raw_data'] = $data; + $data['bot_name'] = $bot_name; $this->assignMemberVariables($data); $this->validate(); - $this->bot_name = $bot_name; } /** @@ -60,84 +50,7 @@ public function __construct($data, $bot_name = '') */ public function toJson() { - return json_encode($this->reflect($this)); - } - - /** - * Reflect - * - * @param null $object - * - * @return array - */ - public function reflect($object = null) - { - if ($object === null) { - $object = $this; - } - - $reflection = new ReflectionObject($object); - $properties = $reflection->getProperties(); - - $fields = []; - - foreach ($properties as $property) { - $name = $property->getName(); - if ($name === 'bot_name') { - continue; - } - - if (!$property->isPrivate()) { - $array_of_obj = false; - $array_of_array_obj = false; - if (is_array($object->$name)) { - $array_of_obj = true; - $array_of_array_obj = true; - foreach ($object->$name as $elm) { - if (!is_object($elm)) { - //echo $name . " not array of object \n"; - $array_of_obj = false; - //break; - } - if (is_array($elm)) { - foreach ($elm as $more_net) { - if (!is_object($more_net)) { - $array_of_array_obj = false; - } - } - } - } - } - - if (is_object($object->$name)) { - $fields[$name] = $this->reflect($object->$name); - } elseif ($array_of_obj) { - foreach ($object->$name as $elm) { - $fields[$name][] = $this->reflect($elm); - } - } elseif ($array_of_array_obj) { - foreach ($object->$name as $elm) { - $temp = null; - if (!is_array($elm) && !is_object($elm)) { - continue; - } - foreach ($elm as $obj) { - $temp[] = $this->reflect($obj); - } - $fields[$name][] = $temp; - } - } else { - $property->setAccessible(true); - $value = $property->getValue($object); - if (null === $value) { - continue; - } - $fields[$name] = $value; - } - } - } - - return $fields; + return json_encode($this->getRawData()); } /** diff --git a/src/Entities/ServerResponse.php b/src/Entities/ServerResponse.php index 144dfbe76..9d50f9cf2 100644 --- a/src/Entities/ServerResponse.php +++ b/src/Entities/ServerResponse.php @@ -40,7 +40,7 @@ class ServerResponse extends Entity */ public function __construct(array $data, $bot_name) { - $this->raw_json = isset($data['raw_json']) ? $data['raw_json'] : null; + $this->raw_data = $data; if (isset($data['ok'], $data['result'])) { if (is_array($data['result'])) { if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { diff --git a/src/Request.php b/src/Request.php index a49c2c8db..e13dd2d6f 100644 --- a/src/Request.php +++ b/src/Request.php @@ -313,14 +313,12 @@ public static function send($action, array $data = []) self::ensureNonEmptyData($data); - $raw_json = self::execute($action, $data); - $response = json_decode($raw_json, true); + $response = json_decode(self::execute($action, $data), true); if (null === $response) { throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); } - $response['raw_json'] = $raw_json; return new ServerResponse($response, $bot_name); } From be54377e5496eeee8f829b455e3ef1575dd11c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 18:17:30 +0300 Subject: [PATCH 29/31] Use new Entity schema for ServerResponse and save raw_data in Entities. --- src/Entities/Entity.php | 10 +- src/Entities/ServerResponse.php | 200 ++++++++++++++++---------------- 2 files changed, 106 insertions(+), 104 deletions(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index c3bb4585e..0ba0bd528 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -37,7 +37,15 @@ abstract class Entity */ public function __construct($data, $bot_name = '') { - $data['raw_data'] = $data; + //Make sure we're not raw_data inception-ing + if (array_key_exists('raw_data', $data)) { + if ($data['raw_data'] === null) { + unset($data['raw_data']); + } + } else { + $data['raw_data'] = $data; + } + $data['bot_name'] = $bot_name; $this->assignMemberVariables($data); $this->validate(); diff --git a/src/Entities/ServerResponse.php b/src/Entities/ServerResponse.php index 9d50f9cf2..704960a30 100644 --- a/src/Entities/ServerResponse.php +++ b/src/Entities/ServerResponse.php @@ -8,117 +8,74 @@ namespace Longman\TelegramBot\Entities; +/** + * Class ServerResponse + * + * @method bool getOk() If the request was successful + * @method mixed getResult() The result of the query + * @method int getErrorCode() Error code of the unsuccessful request + * @method string getDescription() Human-readable description of the result / unsuccessful request + * + * @method_todo ResponseParameters getParameters() Field which can help to automatically handle the error + */ class ServerResponse extends Entity { - /** - * @var bool - */ - protected $ok; - - /** - * @var null - */ - protected $result; - - /** - * @var null - */ - protected $error_code; - - /** - * @var null - */ - protected $description; - /** * ServerResponse constructor. * - * @param array $data - * @param $bot_name + * @param array $data + * @param string $bot_name * * @throws \Longman\TelegramBot\Exception\TelegramException */ public function __construct(array $data, $bot_name) { - $this->raw_data = $data; - if (isset($data['ok'], $data['result'])) { - if (is_array($data['result'])) { - if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { - //Get Update - foreach ($data['result'] as $update) { - $this->result[] = new Update($update, $bot_name); - } - } elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) { - //Response from getChatAdministrators - $this->result = []; - foreach ($data['result'] as $user) { - $this->result[] = new ChatMember($user); - } - } elseif ($data['ok'] && $this->isAssoc($data['result'])) { - if (isset($data['result']['total_count'])) { - //Response from getUserProfilePhotos - $this->result = new UserProfilePhotos($data['result']); - } elseif (isset($data['result']['file_id'])) { - //Response from getFile - $this->result = new File($data['result']); - } elseif (isset($data['result']['username'])) { - //Response from getMe - $this->result = new User($data['result']); - } elseif (isset($data['result']['id'])) { - //Response from getChat - $this->result = new Chat($data['result']); - } elseif (isset($data['result']['user'])) { - //Response from getChatMember - $this->result = new ChatMember($data['result']); - } elseif (isset($data['result']['has_custom_certificate'])) { - //Response from getWebhookInfo - $this->result = new WebhookInfo($data['result']); - } else { - //Response from sendMessage - $this->result = new Message($data['result'], $bot_name); - } + // Make sure we don't double-save the raw_data. + unset($data['raw_data']); + $data['raw_data'] = $data; + + $is_ok = isset($data['ok']) ? (bool)$data['ok'] : false; + $result = isset($data['result']) ? $data['result'] : null; + + if ($is_ok && $result !== null) { + $data['ok'] = true; + + if (is_array($result)) { + if ($this->isAssoc($result)) { + $data['result'] = $this->createResultObject($result, $bot_name); + } else { + $data['result'] = $this->createResultObjects($result, $bot_name); } - $this->ok = $data['ok']; - $this->error_code = null; - $this->description = null; + unset($data['error_code'], $data['description']); } else { - if ($data['ok'] && $data['result'] === true) { + $data['result'] = $result; + if ($result === true) { //Response from setWebhook set - $this->ok = $data['ok']; - $this->result = true; - $this->error_code = null; - $this->description = isset($data['description']) ? $data['description'] : ''; - } elseif (is_numeric($data['result'])) { - //Response from getChatMembersCount - $this->result = $data['result']; - } else { - $this->ok = false; - $this->result = null; - $this->error_code = $data['error_code']; - $this->description = $data['description']; + unset($data['error_code']); + } elseif (!is_numeric($result)) { //Not response from getChatMembersCount + $data['ok'] = false; + unset($data['result']); } } } else { //webHook not set - $this->ok = false; - $this->result = isset($data['result']) ? $data['result'] : null; - $this->error_code = isset($data['error_code']) ? $data['error_code'] : null; - $this->description = isset($data['description']) ? $data['description'] : null; - - //throw new TelegramException('ok(variable) is not set!'); + $data['ok'] = false; } + + parent::__construct($data, $bot_name); } /** * Check if array is associative * * @param array $array + * * @return bool */ protected function isAssoc(array $array) { - return (bool) count(array_filter(array_keys($array), 'is_string')); + return (bool)count(array_filter(array_keys($array), 'is_string')); } /** @@ -128,46 +85,83 @@ protected function isAssoc(array $array) */ public function isOk() { - return $this->ok; + return (bool)$this->getOk(); } /** - * Get result + * Print error * * @return string */ - public function getResult() + public function printError() { - return $this->result; + return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); } /** - * Get error code + * Create and return the object of the received result * - * @return string - */ - public function getErrorCode() - { - return $this->error_code; - } - - /** - * Get description + * @param array $result + * @param string $bot_name * - * @return null + * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function getDescription() + private function createResultObject($result, $bot_name) { - return $this->description; + // We don't need to save the raw_data of the response object! + $result['raw_data'] = null; + + $result_object_types = [ + 'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos + 'file_id' => 'File', //Response from getFile + 'username' => 'User', //Response from getMe + 'id' => 'Chat', //Response from getChat + 'user' => 'ChatMember', //Response from getChatMember + 'url' => 'WebhookInfo', //Response from getWebhookInfo + ]; + foreach ($result_object_types as $type => $object_class) { + if (isset($result[$type])) { + $object_class = __NAMESPACE__ . '\\' . $object_class; + + return new $object_class($result); + } + } + + //Response from sendMessage + return new Message($result, $bot_name); } /** - * Print error + * Create and return the objects array of the received result * - * @return string + * @param array $result + * @param string $bot_name + * + * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] + * @throws \Longman\TelegramBot\Exception\TelegramException */ - public function printError() + private function createResultObjects($result, $bot_name) { - return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); + $results = []; + if (isset($result[0]['user'])) { + //Response from getChatAdministrators + foreach ($result as $user) { + // We don't need to save the raw_data of the response object! + $user['raw_data'] = null; + + $results[] = new ChatMember($user); + } + } else { + //Get Update + foreach ($result as $update) { + // We don't need to save the raw_data of the response object! + $update['raw_data'] = null; + + $results[] = new Update($update, $bot_name); + } + } + + return $results; } } From b1cc1f0654707f383d6adfd6ad328d360ea01995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 20:47:07 +0300 Subject: [PATCH 30/31] Update docblock comments for Request methods. Clean out useless code from ServerResponse. Fix ServerResponseTest. --- src/Entities/ServerResponse.php | 36 +-- src/Request.php | 270 ++++++++++++++------- tests/unit/Entities/ServerResponseTest.php | 2 +- 3 files changed, 197 insertions(+), 111 deletions(-) diff --git a/src/Entities/ServerResponse.php b/src/Entities/ServerResponse.php index 704960a30..49e7161af 100644 --- a/src/Entities/ServerResponse.php +++ b/src/Entities/ServerResponse.php @@ -11,12 +11,14 @@ /** * Class ServerResponse * + * @link https://core.telegram.org/bots/api#making-requests + * * @method bool getOk() If the request was successful * @method mixed getResult() The result of the query * @method int getErrorCode() Error code of the unsuccessful request * @method string getDescription() Human-readable description of the result / unsuccessful request * - * @method_todo ResponseParameters getParameters() Field which can help to automatically handle the error + * @todo method ResponseParameters getParameters() Field which can help to automatically handle the error */ class ServerResponse extends Entity { @@ -30,37 +32,19 @@ class ServerResponse extends Entity */ public function __construct(array $data, $bot_name) { - // Make sure we don't double-save the raw_data. + // Make sure we don't double-save the raw_data unset($data['raw_data']); $data['raw_data'] = $data; $is_ok = isset($data['ok']) ? (bool)$data['ok'] : false; $result = isset($data['result']) ? $data['result'] : null; - if ($is_ok && $result !== null) { - $data['ok'] = true; - - if (is_array($result)) { - if ($this->isAssoc($result)) { - $data['result'] = $this->createResultObject($result, $bot_name); - } else { - $data['result'] = $this->createResultObjects($result, $bot_name); - } - - unset($data['error_code'], $data['description']); + if ($is_ok && is_array($result)) { + if ($this->isAssoc($result)) { + $data['result'] = $this->createResultObject($result, $bot_name); } else { - $data['result'] = $result; - if ($result === true) { - //Response from setWebhook set - unset($data['error_code']); - } elseif (!is_numeric($result)) { //Not response from getChatMembersCount - $data['ok'] = false; - unset($data['result']); - } + $data['result'] = $this->createResultObjects($result, $bot_name); } - } else { - //webHook not set - $data['ok'] = false; } parent::__construct($data, $bot_name); @@ -69,13 +53,15 @@ public function __construct(array $data, $bot_name) /** * Check if array is associative * + * @link https://stackoverflow.com/a/4254008 + * * @param array $array * * @return bool */ protected function isAssoc(array $array) { - return (bool)count(array_filter(array_keys($array), 'is_string')); + return count(array_filter(array_keys($array), 'is_string')) > 0; } /** diff --git a/src/Request.php b/src/Request.php index e13dd2d6f..79ebf42ee 100644 --- a/src/Request.php +++ b/src/Request.php @@ -367,9 +367,11 @@ private static function assignEncodedFile(&$data, $field, $file) } /** - * Get me + * Returns basic information about the bot in form of a User object * - * @return mixed + * @link https://core.telegram.org/bots/api#getme + * + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getMe() @@ -380,11 +382,13 @@ public static function getMe() } /** - * Send message + * Use this method to send text messages. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#sendmessage * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendMessage(array $data) @@ -404,11 +408,13 @@ public static function sendMessage(array $data) } /** - * Forward message + * Use this method to forward messages of any kind. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#forwardmessage * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function forwardMessage(array $data) @@ -417,12 +423,14 @@ public static function forwardMessage(array $data) } /** - * Send photo + * Use this method to send photos. On success, the sent Message is returned + * + * @link https://core.telegram.org/bots/api#sendphoto * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendPhoto(array $data, $file = null) @@ -433,12 +441,18 @@ public static function sendPhoto(array $data, $file = null) } /** - * Send audio + * Use this method to send audio files + * + * Your audio must be in the .mp3 format. On success, the sent Message is returned. + * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. + * For sending voice messages, use the sendVoice method instead. + * + * @link https://core.telegram.org/bots/api#sendaudio * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendAudio(array $data, $file = null) @@ -449,12 +463,16 @@ public static function sendAudio(array $data, $file = null) } /** - * Send document + * Use this method to send general files. On success, the sent Message is returned. + * + * Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future. + * + * @link https://core.telegram.org/bots/api#senddocument * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendDocument(array $data, $file = null) @@ -465,12 +483,14 @@ public static function sendDocument(array $data, $file = null) } /** - * Send sticker + * Use this method to send .webp stickers. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendsticker * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendSticker(array $data, $file = null) @@ -481,12 +501,17 @@ public static function sendSticker(array $data, $file = null) } /** - * Send video + * Use this method to send video files. On success, the sent Message is returned. + * + * Telegram clients support mp4 videos (other formats may be sent as Document). + * Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future. + * + * @link https://core.telegram.org/bots/api#sendvideo * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVideo(array $data, $file = null) @@ -497,12 +522,18 @@ public static function sendVideo(array $data, $file = null) } /** - * Send voice + * Use this method to send audio files. On success, the sent Message is returned. + * + * Telegram clients will display the file as a playable voice message. + * For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). + * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. + * + * @link https://core.telegram.org/bots/api#sendvoice * * @param array $data * @param string $file * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVoice(array $data, $file = null) @@ -513,11 +544,13 @@ public static function sendVoice(array $data, $file = null) } /** - * Send location + * Use this method to send point on the map. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendlocation * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendLocation(array $data) @@ -526,11 +559,13 @@ public static function sendLocation(array $data) } /** - * Send venue + * Use this method to send information about a venue. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendvenue * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendVenue(array $data) @@ -539,11 +574,13 @@ public static function sendVenue(array $data) } /** - * Send contact + * Use this method to send phone contacts. On success, the sent Message is returned. + * + * @link https://core.telegram.org/bots/api#sendcontact * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendContact(array $data) @@ -552,11 +589,16 @@ public static function sendContact(array $data) } /** - * Send chat action + * Use this method when you need to tell the user that something is happening on the bot's side. + * + * The status is set for 5 seconds or less. + * (when a message arrives from your bot, Telegram clients clear its typing status) + * + * @link https://core.telegram.org/bots/api#sendchataction * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function sendChatAction(array $data) @@ -565,11 +607,11 @@ public static function sendChatAction(array $data) } /** - * Get user profile photos + * Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object. * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getUserProfilePhotos(array $data) @@ -578,42 +620,19 @@ public static function getUserProfilePhotos(array $data) } /** - * Get updates + * Use this method to get basic info about a file and prepare it for downloading. On success, a File object is returned. * - * @param array $data - * - * @return mixed - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public static function getUpdates(array $data) - { - return self::send('getUpdates', $data); - } - - /** - * Set webhook - * - * @param string $url - * @param string $file + * For the moment, bots can download files of up to 20MB in size. + * The file can then be downloaded via the link https://api.telegram.org/file/bot/, + * where is taken from the response. + * It is guaranteed that the link will be valid for at least 1 hour. + * When the link expires, a new one can be requested by calling getFile again. * - * @return mixed - * @throws \Longman\TelegramBot\Exception\TelegramException - */ - public static function setWebhook($url = '', $file = null) - { - $data = ['url' => $url]; - - self::assignEncodedFile($data, 'certificate', $file); - - return self::send('setWebhook', $data); - } - - /** - * Get file + * @link https://core.telegram.org/bots/api#getfile * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getFile(array $data) @@ -622,11 +641,16 @@ public static function getFile(array $data) } /** - * Kick Chat Member + * Use this method to kick a user from a group or a supergroup. Returns True on success. + * + * In the case of supergroups, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. + * The bot must be an administrator in the group for this to work. + * + * @link https://core.telegram.org/bots/api#kickchatmember * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function kickChatMember(array $data) @@ -635,11 +659,13 @@ public static function kickChatMember(array $data) } /** - * Leave Chat + * Use this method for your bot to leave a group, supergroup or channel. Returns True on success. + * + * @link https://core.telegram.org/bots/api#leavechat * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function leaveChat(array $data) @@ -648,11 +674,16 @@ public static function leaveChat(array $data) } /** - * Unban Chat Member + * Use this method to unban a previously kicked user in a supergroup. Returns True on success. + * + * The user will not return to the group automatically, but will be able to join via link, etc. + * The bot must be an administrator in the group for this to work. + * + * @link https://core.telegram.org/bots/api#unbanchatmember * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function unbanChatMember(array $data) @@ -661,13 +692,15 @@ public static function unbanChatMember(array $data) } /** - * Get Chat + * Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchat + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChat(array $data) @@ -676,13 +709,18 @@ public static function getChat(array $data) } /** - * Get Chat Administrators + * Use this method to get a list of administrators in a chat. + * + * On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. + * If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatadministrators + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatAdministrators(array $data) @@ -691,13 +729,15 @@ public static function getChatAdministrators(array $data) } /** - * Get Chat Members Count + * Use this method to get the number of members in a chat. Returns Int on success. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatmemberscount + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatMembersCount(array $data) @@ -706,13 +746,15 @@ public static function getChatMembersCount(array $data) } /** - * Get Chat Member + * Use this method to get information about a member of a chat. Returns a ChatMember object on success. * * @todo add get response in ServerResponse.php? * + * @link https://core.telegram.org/bots/api#getchatmember + * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getChatMember(array $data) @@ -721,11 +763,15 @@ public static function getChatMember(array $data) } /** - * Answer callback query + * Use this method to send answers to callback queries sent from inline keyboards. On success, True is returned. + * + * The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. + * + * @link https://core.telegram.org/bots/api#answercallbackquery * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function answerCallbackQuery(array $data) @@ -734,24 +780,50 @@ public static function answerCallbackQuery(array $data) } /** - * Answer inline query + * Get updates + * + * @link https://core.telegram.org/bots/api#getupdates * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ - public static function answerInlineQuery(array $data) + public static function getUpdates(array $data) { - return self::send('answerInlineQuery', $data); + return self::send('getUpdates', $data); } /** - * Edit message text + * Set webhook + * + * @link https://core.telegram.org/bots/api#setwebhook + * + * @param string $url + * @param string $file + * + * @return \Longman\TelegramBot\Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function setWebhook($url = '', $file = null) + { + $data = ['url' => $url]; + + self::assignEncodedFile($data, 'certificate', $file); + + return self::send('setWebhook', $data); + } + + /** + * Use this method to edit text and game messages sent by the bot or via the bot (for inline bots). + * + * On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. + * + * @link https://core.telegram.org/bots/api#editmessagetext * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageText(array $data) @@ -760,11 +832,15 @@ public static function editMessageText(array $data) } /** - * Edit message caption + * Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). + * + * On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. + * + * @link https://core.telegram.org/bots/api#editmessagecaption * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageCaption(array $data) @@ -773,11 +849,15 @@ public static function editMessageCaption(array $data) } /** - * Edit message reply markup + * Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). + * + * On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned. + * + * @link https://core.telegram.org/bots/api#editmessagereplymarkup * * @param array $data * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function editMessageReplyMarkup(array $data) @@ -785,6 +865,23 @@ public static function editMessageReplyMarkup(array $data) return self::send('editMessageReplyMarkup', $data); } + /** + * Use this method to send answers to an inline query. On success, True is returned. + * + * No more than 50 results per query are allowed. + * + * @link https://core.telegram.org/bots/api#answerinlinequery + * + * @param array $data + * + * @return \Longman\TelegramBot\Entities\ServerResponse + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function answerInlineQuery(array $data) + { + return self::send('answerInlineQuery', $data); + } + /** * Return an empty Server Response * @@ -843,11 +940,14 @@ public static function sendToActiveChats( /** * Use this method to get current webhook status. * + * @link https://core.telegram.org/bots/api#getwebhookinfo + * * @return Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function getWebhookInfo() { + // Must send some arbitrary data for this to work for now... return self::send('getWebhookInfo', ['info']); } } diff --git a/tests/unit/Entities/ServerResponseTest.php b/tests/unit/Entities/ServerResponseTest.php index 8f27a268f..783ca6697 100644 --- a/tests/unit/Entities/ServerResponseTest.php +++ b/tests/unit/Entities/ServerResponseTest.php @@ -187,7 +187,7 @@ public function testGetUpdatesEmpty() { $result = $this->getUpdatesEmpty(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertNull($this->server->getResult()); + $this->assertEmpty($this->server->getResult()); } public function getUserProfilePhotos() From 38a8658447d91671509eecb07c2167f82f84c449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 11 Oct 2016 20:59:28 +0300 Subject: [PATCH 31/31] Clean up Entity tests. --- tests/unit/Entities/InlineKeyboardTest.php | 6 +- tests/unit/Entities/KeyboardTest.php | 1 - tests/unit/Entities/ReplyToMessageTest.php | 21 +-- tests/unit/Entities/ServerResponseTest.php | 161 +++++++++++---------- tests/unit/Entities/UpdateTest.php | 6 +- 5 files changed, 91 insertions(+), 104 deletions(-) diff --git a/tests/unit/Entities/InlineKeyboardTest.php b/tests/unit/Entities/InlineKeyboardTest.php index 731e4a628..b3007affe 100644 --- a/tests/unit/Entities/InlineKeyboardTest.php +++ b/tests/unit/Entities/InlineKeyboardTest.php @@ -90,7 +90,7 @@ public function testInlineKeyboardMultipleButtonsSingleRow() { $keyboard = (new InlineKeyboard([ $this->getRandomButton('Button Text 1'), - $this->getRandomButton('Button Text 2') + $this->getRandomButton('Button Text 2'), ]))->getProperty('inline_keyboard'); self::assertSame('Button Text 1', $keyboard[0][0]->getText()); self::assertSame('Button Text 2', $keyboard[0][1]->getText()); @@ -101,11 +101,11 @@ public function testInlineKeyboardMultipleButtonsMultipleRows() $keyboard = (new InlineKeyboard( [ $this->getRandomButton('Button Text 1'), - $this->getRandomButton('Button Text 2') + $this->getRandomButton('Button Text 2'), ], [ $this->getRandomButton('Button Text 3'), - $this->getRandomButton('Button Text 4') + $this->getRandomButton('Button Text 4'), ] ))->getProperty('inline_keyboard'); diff --git a/tests/unit/Entities/KeyboardTest.php b/tests/unit/Entities/KeyboardTest.php index bc3024f65..ce93c2ed1 100644 --- a/tests/unit/Entities/KeyboardTest.php +++ b/tests/unit/Entities/KeyboardTest.php @@ -12,7 +12,6 @@ use Longman\TelegramBot\Entities\Keyboard; use Longman\TelegramBot\Entities\KeyboardButton; -use Longman\TelegramBot\Exception\TelegramException; /** * @package TelegramTest diff --git a/tests/unit/Entities/ReplyToMessageTest.php b/tests/unit/Entities/ReplyToMessageTest.php index 112e21843..c8287126c 100644 --- a/tests/unit/Entities/ReplyToMessageTest.php +++ b/tests/unit/Entities/ReplyToMessageTest.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Tests\Unit; -use \Longman\TelegramBot\Entities\Update; +use Longman\TelegramBot\Entities\Update; /** * @package TelegramTest @@ -21,29 +21,16 @@ */ class ReplyToMessageTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $reply_to_message; - - /** - * @var \Longman\TelegramBot\Entities\Message - */ - private $message; - public function testChatType() { $json = ' {"update_id":137809335, "message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}} '; - $struct = json_decode($json, true); - $update = new Update($struct, 'mybot'); - - $this->message = $update->getMessage(); - $this->reply_to_message = $this->message->getReplyToMessage(); - $this->assertNull($this->reply_to_message->getReplyToMessage()); + $update = new Update(json_decode($json, true), 'mybot'); + $reply_to_message = $update->getMessage()->getReplyToMessage(); + self::assertNull($reply_to_message->getReplyToMessage()); } } diff --git a/tests/unit/Entities/ServerResponseTest.php b/tests/unit/Entities/ServerResponseTest.php index 783ca6697..c14d7db0c 100644 --- a/tests/unit/Entities/ServerResponseTest.php +++ b/tests/unit/Entities/ServerResponseTest.php @@ -13,8 +13,8 @@ namespace Longman\TelegramBot\Tests\Unit; use Longman\TelegramBot\Entities\Message; -use \Longman\TelegramBot\Entities\ServerResponse; -use \Longman\TelegramBot\Request; +use Longman\TelegramBot\Entities\ServerResponse; +use Longman\TelegramBot\Request; /** * @package TelegramTest @@ -25,11 +25,6 @@ */ class ServerResponseTest extends TestCase { - /** - * @var \Longman\TelegramBot\Entities\ServerResponse - */ - private $server; - public function sendMessageOk() { return '{ @@ -44,26 +39,27 @@ public function sendMessageOk() }'; } - public function testSendMessageOk() { - $result = $this->sendMessageOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - $server_result = $this->server->getResult(); + public function testSendMessageOk() + { + $result = $this->sendMessageOk(); + $server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $server->getResult(); - $this->assertTrue($this->server->isOk()); - $this->assertNull($this->server->getErrorCode()); - $this->assertNull($this->server->getDescription()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); + self::assertTrue($server->isOk()); + self::assertNull($server->getErrorCode()); + self::assertNull($server->getDescription()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $server_result->getMessageId()); - $this->assertEquals('123456789', $server_result->getFrom()->getId()); - $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUsername()); - $this->assertEquals('123456789', $server_result->getChat()->getId()); - $this->assertEquals('john', $server_result->getChat()->getFirstName()); - $this->assertEquals('Mjohn', $server_result->getChat()->getUsername()); - $this->assertEquals('1441378360', $server_result->getDate()); - $this->assertEquals('hello', $server_result->getText()); + self::assertEquals('1234', $server_result->getMessageId()); + self::assertEquals('123456789', $server_result->getFrom()->getId()); + self::assertEquals('botname', $server_result->getFrom()->getFirstName()); + self::assertEquals('namebot', $server_result->getFrom()->getUsername()); + self::assertEquals('123456789', $server_result->getChat()->getId()); + self::assertEquals('john', $server_result->getChat()->getFirstName()); + self::assertEquals('Mjohn', $server_result->getChat()->getUsername()); + self::assertEquals('1441378360', $server_result->getDate()); + self::assertEquals('hello', $server_result->getText()); //... they are not finished... } @@ -77,29 +73,31 @@ public function sendMessageFail() }'; } - public function testSendMessageFail() { + public function testSendMessageFail() + { $result = $this->sendMessageFail(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertFalse($this->server->isOk()); - $this->assertNull($this->server->getResult()); - $this->assertEquals('400', $this->server->getErrorCode()); - $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription()); + self::assertFalse($server->isOk()); + self::assertNull($server->getResult()); + self::assertEquals('400', $server->getErrorCode()); + self::assertEquals('Error: Bad Request: wrong chat id', $server->getDescription()); } - public function setWebhookOk() + public function setWebhookOk() { return '{"ok":true,"result":true,"description":"Webhook was set"}'; } - public function testSetWebhookOk() { + public function testSetWebhookOk() + { $result = $this->setWebhookOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertTrue($this->server->isOk()); - $this->assertTrue($this->server->getResult()); - $this->assertNull($this->server->getErrorCode()); - $this->assertEquals('Webhook was set', $this->server->getDescription()); + self::assertTrue($server->isOk()); + self::assertTrue($server->getResult()); + self::assertNull($server->getErrorCode()); + self::assertEquals('Webhook was set', $server->getDescription()); } public function setWebhookFail() @@ -111,14 +109,15 @@ public function setWebhookFail() }'; } - public function testSetWebhookFail() { + public function testSetWebhookFail() + { $result = $this->setWebhookFail(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertFalse($this->server->isOk()); - $this->assertNull($this->server->getResult()); - $this->assertEquals(400, $this->server->getErrorCode()); - $this->assertEquals("Error: Bad request: htttps://domain.host.org/dir/hook.php", $this->server->getDescription()); + self::assertFalse($server->isOk()); + self::assertNull($server->getResult()); + self::assertEquals(400, $server->getErrorCode()); + self::assertEquals('Error: Bad request: htttps://domain.host.org/dir/hook.php', $server->getDescription()); } public function getUpdatesArray() @@ -170,12 +169,13 @@ public function getUpdatesArray() }'; } - public function testGetUpdatesArray() { + public function testGetUpdatesArray() + { $result = $this->getUpdatesArray(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertCount(4, $this->server->getResult()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Update', $this->server->getResult()[0]); + self::assertCount(4, $server->getResult()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Update', $server->getResult()[0]); } public function getUpdatesEmpty() @@ -183,11 +183,12 @@ public function getUpdatesEmpty() return '{"ok":true,"result":[]}'; } - public function testGetUpdatesEmpty() { + public function testGetUpdatesEmpty() + { $result = $this->getUpdatesEmpty(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertEmpty($this->server->getResult()); + self::assertEmpty($server->getResult()); } public function getUserProfilePhotos() @@ -219,20 +220,20 @@ public function getUserProfilePhotos() public function testGetUserProfilePhotos() { - $result = $this->getUserProfilePhotos(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - $server_result = $this->server->getResult(); + $result = $this->getUserProfilePhotos(); + $server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $server->getResult(); $photos = $server_result->getPhotos(); //Photo count - $this->assertEquals(3, $server_result->getTotalCount()); - $this->assertCount(3, $photos); + self::assertEquals(3, $server_result->getTotalCount()); + self::assertCount(3, $photos); //Photo size count - $this->assertCount(3, $photos[0]); + self::assertCount(3, $photos[0]); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $server_result); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $photos[0][0]); + self::assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $server_result); + self::assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $photos[0][0]); } public function getFile() @@ -250,9 +251,9 @@ public function getFile() public function testGetFile() { $result = $this->getFile(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server = new ServerResponse(json_decode($result, true), 'testbot'); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\File', $this->server->getResult()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\File', $server->getResult()); } public function testSetGeneralTestFakeResponse() @@ -260,40 +261,40 @@ public function testSetGeneralTestFakeResponse() //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); - $this->server = new ServerResponse($fake_response, 'testbot'); + $server = new ServerResponse($fake_response, 'testbot'); - $this->assertTrue($this->server->isOk()); - $this->assertTrue($this->server->getResult()); - $this->assertNull($this->server->getErrorCode()); - $this->assertEquals('', $this->server->getDescription()); + self::assertTrue($server->isOk()); + self::assertTrue($server->getResult()); + self::assertNull($server->getErrorCode()); + self::assertEquals('', $server->getDescription()); //sendMessage ok $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']); - $this->server = new ServerResponse($fake_response, 'testbot'); + $server = new ServerResponse($fake_response, 'testbot'); /** @var Message $server_result */ - $server_result = $this->server->getResult(); + $server_result = $server->getResult(); - $this->assertTrue($this->server->isOk()); - $this->assertNull($this->server->getErrorCode()); - $this->assertNull($this->server->getDescription()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); + self::assertTrue($server->isOk()); + self::assertNull($server->getErrorCode()); + self::assertNull($server->getDescription()); + self::assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $server_result->getMessageId()); - $this->assertEquals('1441378360', $server_result->getDate()); - $this->assertEquals('hello', $server_result->getText()); + self::assertEquals('1234', $server_result->getMessageId()); + self::assertEquals('1441378360', $server_result->getDate()); + self::assertEquals('hello', $server_result->getText()); //Message //User - $this->assertEquals('123456789', $server_result->getFrom()->getId()); - $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); - $this->assertEquals('namebot', $server_result->getFrom()->getUsername()); + self::assertEquals('123456789', $server_result->getFrom()->getId()); + self::assertEquals('botname', $server_result->getFrom()->getFirstName()); + self::assertEquals('namebot', $server_result->getFrom()->getUsername()); //Message //Chat - $this->assertEquals('123456789', $server_result->getChat()->getId()); - $this->assertEquals('', $server_result->getChat()->getFirstName()); - $this->assertEquals('', $server_result->getChat()->getUsername()); + self::assertEquals('123456789', $server_result->getChat()->getId()); + self::assertEquals('', $server_result->getChat()->getFirstName()); + self::assertEquals('', $server_result->getChat()->getUsername()); //... they are not finished... } diff --git a/tests/unit/Entities/UpdateTest.php b/tests/unit/Entities/UpdateTest.php index dacf9fff0..81c32039b 100644 --- a/tests/unit/Entities/UpdateTest.php +++ b/tests/unit/Entities/UpdateTest.php @@ -10,7 +10,7 @@ namespace Longman\TelegramBot\Tests\Unit; -use \Longman\TelegramBot\Entities\Update; +use Longman\TelegramBot\Entities\Update; /** * @package TelegramTest @@ -23,7 +23,7 @@ class UpdateTest extends TestCase { public function testUpdateCast() { - $json = ' + $json = ' {"update_id":137809336, "message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}} '; @@ -31,6 +31,6 @@ public function testUpdateCast() $update = new Update($struct, 'mybot'); $array_string_after = json_decode($update->toJson(), true); - $this->assertEquals($struct, $array_string_after); + self::assertEquals($struct, $array_string_after); } }