1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of the TelegramBot package. |
||
5 | * |
||
6 | * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Longman\TelegramBot\Entities; |
||
13 | |||
14 | use Longman\TelegramBot\Exception\TelegramException; |
||
15 | |||
16 | /** |
||
17 | * Class KeyboardButton |
||
18 | * |
||
19 | * This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields request_contact, request_location, and request_poll are mutually exclusive. |
||
20 | * |
||
21 | * @link https://core.telegram.org/bots/api#keyboardbutton |
||
22 | * |
||
23 | * @property KeyboardButtonRequestUser $request_user |
||
24 | * @property KeyboardButtonRequestChat $request_chat |
||
25 | * @property bool $request_contact |
||
26 | * @property bool $request_location |
||
27 | * @property KeyboardButtonPollType $request_poll |
||
28 | * @property WebAppInfo $web_app |
||
29 | * |
||
30 | * @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 |
||
31 | * @method KeyboardButtonRequestUser getRequestUser() Optional. If specified, pressing the button will open a list of suitable users. Tapping on any user will send their identifier to the bot in a “user_shared” service message. Available in private chats only. |
||
32 | * @method KeyboardButtonRequestChat getRequestChat() Optional. If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only. |
||
33 | * @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 |
||
34 | * @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only |
||
35 | * @method KeyboardButtonPollType getRequestPoll() Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only |
||
36 | * @method WebAppInfo getWebApp() Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only. |
||
37 | * |
||
38 | * @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 |
||
39 | * @method $this setRequestUser(KeyboardButtonRequestUser $request_user) Optional. If specified, pressing the button will open a list of suitable users. Tapping on any user will send their identifier to the bot in a “user_shared” service message. Available in private chats only. |
||
40 | * @method $this setRequestChat(KeyboardButtonRequestChat $request_chat) Optional. If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only. |
||
41 | * @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 |
||
42 | * @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 |
||
43 | * @method $this setRequestPoll(KeyboardButtonPollType $request_poll) Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only |
||
44 | * @method $this setWebApp(WebAppInfo $web_app) Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only. |
||
45 | */ |
||
46 | class KeyboardButton extends Entity |
||
47 | { |
||
48 | /** |
||
49 | * @param array|string $data |
||
50 | */ |
||
51 | 16 | public function __construct($data) |
|
52 | { |
||
53 | 16 | if (is_string($data)) { |
|
54 | 8 | $data = ['text' => $data]; |
|
55 | } |
||
56 | 16 | parent::__construct($data); |
|
57 | } |
||
58 | |||
59 | 13 | protected function subEntities(): array |
|
60 | { |
||
61 | 13 | return [ |
|
62 | 13 | 'request_user' => KeyboardButtonRequestUser::class, |
|
63 | 13 | 'request_chat' => KeyboardButtonRequestChat::class, |
|
64 | 13 | 'request_poll' => KeyboardButtonPollType::class, |
|
65 | 13 | 'web_app' => WebAppInfo::class, |
|
66 | 13 | ]; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Check if the passed data array could be a KeyboardButton. |
||
71 | * |
||
72 | * @param array $data |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 1 | public static function couldBe(array $data): bool |
|
77 | { |
||
78 | 1 | return array_key_exists('text', $data); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 13 | public function __call($method, $args) |
|
85 | { |
||
86 | // Only 1 of these can be set, so clear the others when setting a new one. |
||
87 | 13 | if (in_array($method, ['setRequestUser', 'setRequestChat', 'setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) { |
|
88 | unset($this->reqest_user, $this->request_chat, $this->request_contact, $this->request_location, $this->request_poll, $this->web_app); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
89 | } |
||
90 | |||
91 | 13 | return parent::__call($method, $args); |
|
92 | } |
||
93 | } |
||
94 |