Completed
Push — master ( 3e7af9...f7cac5 )
by Armando
90:45 queued 54:10
created

InlineKeyboardButton::validate()   C

Complexity

Conditions 7
Paths 19

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 19
nop 0
crap 7
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
 * @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.
25
 * @method bool   getPay()                          Optional. Specify True, to send a Pay button.
26
 *
27
 * @method $this setText(string $text)                                                     Label text on the button
28
 * @method $this setUrl(string $url)                                                       Optional. HTTP url to be opened when button is pressed
29
 * @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
30
 * @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.
31
 * @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.
32
 * @method $this setPay(bool $pay)                                                         Optional. Specify True, to send a Pay button.
33
 */
34
class InlineKeyboardButton extends KeyboardButton
35
{
36
    /**
37
     * Check if the passed data array could be an InlineKeyboardButton.
38
     *
39
     * @param array $data
40
     *
41
     * @return bool
42
     */
43 1
    public static function couldBe($data)
44
    {
45 1
        return is_array($data) &&
46 1
               array_key_exists('text', $data) && (
47 1
                   array_key_exists('url', $data) ||
48 1
                   array_key_exists('callback_data', $data) ||
49 1
                   array_key_exists('switch_inline_query', $data) ||
50 1
                   array_key_exists('switch_inline_query_current_chat', $data) ||
51 1
                   array_key_exists('pay', $data)
52
               );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 10
    protected function validate()
59
    {
60 10
        if ($this->getProperty('text', '') === '') {
61 1
            throw new TelegramException('You must add some text to the button!');
62
        }
63
64 9
        $num_params = 0;
65
66 9
        foreach (['url', 'callback_data', 'pay'] as $param) {
67 9
            if ($this->getProperty($param, '') !== '') {
68 9
                $num_params++;
69
            }
70
        }
71
72 9
        foreach (['switch_inline_query', 'switch_inline_query_current_chat'] as $param) {
73 9
            if ($this->getProperty($param) !== null) {
74 9
                $num_params++;
75
            }
76
        }
77
78 9
        if ($num_params !== 1) {
79 2
            throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, pay!');
80
        }
81 7
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 6
    public function __call($method, $args)
87
    {
88
        // Only 1 of these can be set, so clear the others when setting a new one.
89 6
        if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setPay'], true)) {
90 1
            unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->pay);
91
        }
92
93 6
        return parent::__call($method, $args);
94
    }
95
}
96