InlineKeyboardButton::couldBe()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
c 0
b 0
f 0
rs 8.0555
cc 9
nc 9
nop 1
1
<?php
2
3
namespace TelegramBot\Entities;
4
5
use TelegramBot\Entities\Games\CallbackGame;
6
use TelegramBot\Exception\TelegramException;
7
8
/**
9
 * Class InlineKeyboardButton
10
 *
11
 * This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
12
 *
13
 * @link https://core.telegram.org/bots/api#inlinekeyboardbutton
14
 *
15
 * @method string       getText()                           Label text on the button
16
 * @method string       getUrl()                            Optional. HTTP url to be opened when button is pressed
17
 * @method LoginUrl     getLoginUrl()                       Optional. An HTTP URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
18
 * @method string       getCallbackData()                   Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
19
 * @method WebAppInfo   getWebApp()                         Optional. Description of the Web App that will be launched when the user presses the button. The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only in private chats between a user and the bot.
20
 * @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.
21
 * @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.
22
 * @method CallbackGame getCallbackGame()                   Optional. Description of the game that will be launched when the user presses the button.
23
 * @method bool         getPay()                            Optional. Specify True, to send a Pay button.
24
 *
25
 * @method InlineKeyboardButton setText(string $text)                                                       Label text on the button
26
 * @method InlineKeyboardButton setLoginUrl(LoginUrl $login_url)                                            Optional. HTTP url to be opened when button is pressed
27
 * @method InlineKeyboardButton 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.
28
 * @method InlineKeyboardButton 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.
29
 * @method InlineKeyboardButton setCallbackGame(CallbackGame $callback_game)                                Optional. Description of the game that will be launched when the user presses the button.
30
 * @method InlineKeyboardButton setPay(bool $pay)                                                           Optional. Specify True, to send a Pay button.
31
 */
32
class InlineKeyboardButton extends KeyboardButton
33
{
34
35
    /**
36
     * Creates instance of InlineKeyboardButton
37
     *
38
     * @param string|null $label
39
     * @return InlineKeyboardButton
40
     */
41
    public static function make(string|null $label = null): InlineKeyboardButton
42
    {
43
        return new self($label);
44
    }
45
46
    /**
47
     * Check if the passed data array could be an InlineKeyboardButton.
48
     *
49
     * @param array $data
50
     *
51
     * @return bool
52
     */
53
    public static function couldBe(array $data): bool
54
    {
55
        return array_key_exists('text', $data) && (
56
                array_key_exists('url', $data) ||
57
                array_key_exists('login_url', $data) ||
58
                array_key_exists('callback_data', $data) ||
59
                array_key_exists('web_app', $data) ||
60
                array_key_exists('switch_inline_query', $data) ||
61
                array_key_exists('switch_inline_query_current_chat', $data) ||
62
                array_key_exists('callback_game', $data) ||
63
                array_key_exists('pay', $data)
64
            );
65
    }
66
67
    /**
68
     * Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
69
     *
70
     * @param string $data
71
     * @return $this
72
     */
73
    public function setCallbackData(string $data): InlineKeyboardButton
74
    {
75
        $this->raw_data['callback_data'] = $data;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Optional. HTTP url to be opened when button is pressed
82
     *
83
     * @param string $data
84
     * @return $this
85
     */
86
    public function setUrl(string $data): InlineKeyboardButton
87
    {
88
        $this->raw_data['url'] = $data;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Optional. Description of the Web App that will be launched when the user presses the button. The Web App will
95
     * be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only
96
     * in private chats between a user and the bot.
97
     *
98
     * @param string $url
99
     * @return $this
100
     */
101
    public function setWebApp(string $url): KeyboardButton
102
    {
103
        $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]);
104
105
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function __call(string $name, array $arguments): mixed
112
    {
113
        if (in_array($name, ['setUrl', 'setLoginUrl', 'setCallbackData', 'web_app', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
114
            unset($this->url, $this->login_url, $this->callback_data, $this->web_app, $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 callback_data does not exist on 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 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 TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property switch_inline_query does not exist on 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 TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property pay does not exist on TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property url does not exist on TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
115
        }
116
117
        return parent::__call($name, $arguments);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    protected function validate(): void
124
    {
125
        if ($this->getProperty('text', '') === '') {
126
            throw new TelegramException('You must add some text to the button!');
127
        }
128
    }
129
130
}
131