1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Entities; |
12
|
|
|
|
13
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class InlineKeyboardButton |
17
|
|
|
* |
18
|
|
|
* @link https://core.telegram.org/bots/api#inlinekeyboardbutton |
19
|
|
|
* |
20
|
|
|
* @method string getText() Label text on the button |
21
|
|
|
* @method string getUrl() Optional. HTTP url to be opened when button is pressed |
22
|
|
|
* @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes |
23
|
|
|
* @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. |
24
|
|
|
* |
25
|
|
|
* @method $this setText(string $text) Label text on the button |
26
|
|
|
* @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed |
27
|
|
|
* @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 |
28
|
|
|
* @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. |
29
|
|
|
*/ |
30
|
|
|
class InlineKeyboardButton extends KeyboardButton |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Check if the passed data array could be an InlineKeyboardButton. |
34
|
|
|
* |
35
|
|
|
* @param array $data |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
1 |
|
public static function couldBe($data) |
40
|
|
|
{ |
41
|
1 |
|
return is_array($data) && |
42
|
1 |
|
array_key_exists('text', $data) && ( |
43
|
1 |
|
array_key_exists('url', $data) || |
44
|
1 |
|
array_key_exists('callback_data', $data) || |
45
|
1 |
|
array_key_exists('switch_inline_query', $data) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
10 |
|
protected function validate() |
53
|
|
|
{ |
54
|
10 |
|
if ($this->getProperty('text', '') === '') { |
55
|
1 |
|
throw new TelegramException('You must add some text to the button!'); |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
$num_params = 0; |
59
|
|
|
|
60
|
9 |
|
foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { |
61
|
9 |
|
if (!empty($this->getProperty($param))) { |
62
|
9 |
|
$num_params++; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
if ($num_params !== 1) { |
67
|
2 |
|
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!'); |
68
|
|
|
} |
69
|
7 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
6 |
View Code Duplication |
public function __call($method, $args) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
// Only 1 of these can be set, so clear the others when setting a new one. |
77
|
6 |
|
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery'], true)) { |
78
|
1 |
|
unset($this->url, $this->callback_data, $this->switch_inline_query); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
return parent::__call($method, $args); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.