Issues (36)

src/Entities/InlineKeyboardButton.php (7 issues)

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\Entities\Games\CallbackGame;
15
use Longman\TelegramBot\Exception\TelegramException;
16
17
/**
18
 * Class InlineKeyboardButton
19
 *
20
 * @link https://core.telegram.org/bots/api#inlinekeyboardbutton
21
 *
22
 * @method string       getText()                         Label text on the button
23
 * @method string       getUrl()                          Optional. HTTP url to be opened when button is pressed
24
 * @method LoginUrl     getLoginUrl()                     Optional. An HTTP URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
25
 * @method string       getCallbackData()                 Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
26
 * @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.
27
 * @method string       getSwitchInlineQueryCurrentChat() Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
28
 * @method CallbackGame getCallbackGame()                 Optional. Description of the game that will be launched when the user presses the button.
29
 * @method bool         getPay()                          Optional. Specify True, to send a Pay button.
30
 *
31
 * @method $this setText(string $text)                                                     Label text on the button
32
 * @method $this setUrl(string $url)                                                       Optional. HTTP url to be opened when button is pressed
33
 * @method $this setLoginUrl(LoginUrl $login_url)                                                       Optional. HTTP url to be opened when button is pressed
34
 * @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
35
 * @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.
36
 * @method $this setSwitchInlineQueryCurrentChat(string $switch_inline_query_current_chat) Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
37
 * @method $this setCallbackGame(CallbackGame $callback_game)                              Optional. Description of the game that will be launched when the user presses the button.
38
 * @method $this setPay(bool $pay)                                                         Optional. Specify True, to send a Pay button.
39
 */
40
class InlineKeyboardButton extends KeyboardButton
41
{
42
    /**
43
     * Check if the passed data array could be an InlineKeyboardButton.
44
     *
45
     * @param array $data
46
     *
47
     * @return bool
48
     */
49 1
    public static function couldBe(array $data): bool
50
    {
51 1
        return array_key_exists('text', $data) && (
52 1
                array_key_exists('url', $data) ||
53 1
                array_key_exists('login_url', $data) ||
54 1
                array_key_exists('callback_data', $data) ||
55 1
                array_key_exists('switch_inline_query', $data) ||
56 1
                array_key_exists('switch_inline_query_current_chat', $data) ||
57 1
                array_key_exists('callback_game', $data) ||
58 1
                array_key_exists('pay', $data)
59
            );
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 10
    protected function validate(): void
66
    {
67 10
        if ($this->getProperty('text', '') === '') {
68 1
            throw new TelegramException('You must add some text to the button!');
69
        }
70
71 9
        $num_params = 0;
72
73 9
        foreach (['url', 'login_url', 'callback_data', 'callback_game', 'pay'] as $param) {
74 9
            if ($this->getProperty($param, '') !== '') {
75 7
                $num_params++;
76
            }
77
        }
78
79 9
        foreach (['switch_inline_query', 'switch_inline_query_current_chat'] as $param) {
80 9
            if ($this->getProperty($param) !== null) {
81 6
                $num_params++;
82
            }
83
        }
84
85 9
        if ($num_params !== 1) {
86 2
            throw new TelegramException('You must use only one of these fields: url, login_url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!');
87
        }
88 7
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 6
    public function __call($method, $args)
94
    {
95
        // Only 1 of these can be set, so clear the others when setting a new one.
96 6
        if (in_array($method, ['setUrl', 'setLoginUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
97 1
            unset($this->url, $this->login_url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay);
0 ignored issues
show
Bug Best Practice introduced by
The property switch_inline_query does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property login_url does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property switch_inline_query_current_chat does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property callback_data does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property url does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property pay does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property callback_game does not exist on Longman\TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
98
        }
99
100 6
        return parent::__call($method, $args);
101
    }
102
}
103