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 bool $request_contact |
24
|
|
|
* @property bool $request_location |
25
|
|
|
* @property KeyboardButtonPollType $request_poll |
26
|
|
|
* |
27
|
|
|
* @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 |
28
|
|
|
* @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 |
29
|
|
|
* @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only |
30
|
|
|
* @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 |
31
|
|
|
* |
32
|
|
|
* @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 |
33
|
19 |
|
* @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 |
34
|
|
|
* @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 |
35
|
19 |
|
* @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 |
36
|
8 |
|
*/ |
37
|
|
|
class KeyboardButton extends Entity |
38
|
19 |
|
{ |
39
|
14 |
|
/** |
40
|
|
|
* @param array|string $data |
41
|
|
|
*/ |
42
|
|
|
public function __construct($data) |
43
|
|
|
{ |
44
|
|
|
if (is_string($data)) { |
45
|
|
|
$data = ['text' => $data]; |
46
|
|
|
} |
47
|
|
|
parent::__construct($data); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
/** |
51
|
|
|
* Check if the passed data array could be a KeyboardButton. |
52
|
|
|
* |
53
|
|
|
* @param array $data |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
10 |
|
*/ |
57
|
|
|
public static function couldBe(array $data): bool |
58
|
10 |
|
{ |
59
|
1 |
|
return array_key_exists('text', $data); |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
/** |
63
|
1 |
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
8 |
|
protected function validate(): void |
66
|
|
|
{ |
67
|
|
|
if ($this->getProperty('text', '') === '') { |
68
|
|
|
throw new TelegramException('You must add some text to the button!'); |
69
|
|
|
} |
70
|
15 |
|
|
71
|
|
|
// Make sure only 1 of the optional request fields is set. |
72
|
|
|
$field_count = array_filter([ |
73
|
15 |
|
$this->getRequestContact(), |
74
|
1 |
|
$this->getRequestLocation(), |
75
|
|
|
$this->getRequestPoll(), |
76
|
|
|
]); |
77
|
15 |
|
if (count($field_count) > 1) { |
78
|
|
|
throw new TelegramException('You must use only one of these fields: request_contact, request_location, request_poll!'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function __call($method, $args) |
86
|
|
|
{ |
87
|
|
|
// Only 1 of these can be set, so clear the others when setting a new one. |
88
|
|
|
if (in_array($method, ['setRequestContact', 'setRequestLocation', 'setRequestPoll'], true)) { |
89
|
|
|
unset($this->request_contact, $this->request_location, $this->request_poll); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return parent::__call($method, $args); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|