Passed
Push — master ( 9c7b82...f78b46 )
by Shahrad
02:20
created

InlineKeyboardButton   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 7 2
A __construct() 0 4 1
B couldBe() 0 11 9
A make() 0 3 1
A CallbackData() 0 5 1
A validate() 0 4 2
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 setUrl(string $url)                                                         Optional. HTTP url to be opened when button is pressed
27
 * @method InlineKeyboardButton setLoginUrl(LoginUrl $login_url)                                            Optional. HTTP url to be opened when button is pressed
28
 * @method InlineKeyboardButton setCallbackData(string $callback_data)                                      Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
29
 * @method InlineKeyboardButton setWebApp(WebAppInfo $web_app)                                              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.
30
 * @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.
31
 * @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.
32
 * @method InlineKeyboardButton setCallbackGame(CallbackGame $callback_game)                                Optional. Description of the game that will be launched when the user presses the button.
33
 * @method InlineKeyboardButton setPay(bool $pay)                                                           Optional. Specify True, to send a Pay button.
34
 */
35
class InlineKeyboardButton extends KeyboardButton
36
{
37
38
	public function __construct($data)
39
	{
40
41
		parent::__construct($data);
42
	}
43
44
	/**
45
	 * Creates instance of InlineKeyboardButton
46
	 *
47
	 * @param string $string
48
	 * @return InlineKeyboardButton
49
	 */
50
	public static function make(string $string): InlineKeyboardButton
51
	{
52
		return new self($string);
53
	}
54
55
	/**
56
	 * @param string $data
57
	 * @return $this
58
	 */
59
	public function CallbackData(string $data): InlineKeyboardButton
60
	{
61
		$this->raw_data['callback_data'] = $data;
62
63
		return $this;
64
	}
65
66
    /**
67
     * Check if the passed data array could be an InlineKeyboardButton.
68
     *
69
     * @param array $data
70
     *
71
     * @return bool
72
     */
73
    public static function couldBe(array $data): bool
74
    {
75
        return array_key_exists('text', $data) && (
76
                array_key_exists('url', $data) ||
77
                array_key_exists('login_url', $data) ||
78
                array_key_exists('callback_data', $data) ||
79
                array_key_exists('web_app', $data) ||
80
                array_key_exists('switch_inline_query', $data) ||
81
                array_key_exists('switch_inline_query_current_chat', $data) ||
82
                array_key_exists('callback_game', $data) ||
83
                array_key_exists('pay', $data)
84
            );
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function validate(): void
91
    {
92
        if ($this->getProperty('text', '') === '') {
93
            throw new TelegramException('You must add some text to the button!');
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function __call(string $name, array $arguments): mixed
101
    {
102
        if (in_array($name, ['setUrl', 'setLoginUrl', 'setCallbackData', 'web_app', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
103
            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 url 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 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_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 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 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 callback_data does not exist on TelegramBot\Entities\InlineKeyboardButton. Did you maybe forget to declare it?
Loading history...
104
        }
105
106
        return parent::__call($name, $arguments);
107
    }
108
109
}
110