Passed
Pull Request — develop (#1077)
by Marco
02:09
created

KeyboardButton::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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 PhpTelegramBot\Core\Entities;
13
14
use PhpTelegramBot\Core\Exception\TelegramException;
15
16
/**
17
 * Class KeyboardButton
18
 *
19
 * This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields request_contact, request_location, and request_poll are mutually exclusive.
20
 *
21
 * @link https://core.telegram.org/bots/api#keyboardbutton
22
 *
23
 * @property bool                   $request_contact
24
 * @property bool                   $request_location
25
 * @property KeyboardButtonPollType $request_poll
26
 *
27
 * @method string                 getText()            Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed
28
 * @method bool                   getRequestContact()  Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
29
 * @method bool                   getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
30
 * @method KeyboardButtonPollType getRequestPoll() Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only
31
 *
32
 * @method $this setText(string $text)                                Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed
33
 * @method $this setRequestContact(bool $request_contact)             Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
34
 * @method $this setRequestLocation(bool $request_location)           Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
35
 * @method $this setRequestPoll(KeyboardButtonPollType $request_poll) Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only
36
 */
37
class KeyboardButton extends Entity
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42 21
    public function __construct($data)
43
    {
44 21
        if (is_string($data)) {
45 8
            $data = ['text' => $data];
46
        }
47 21
        parent::__construct($data);
48 16
    }
49
50
    /**
51
     * Check if the passed data array could be a KeyboardButton.
52
     *
53
     * @param array $data
54
     *
55
     * @return bool
56
     */
57 1
    public static function couldBe($data)
58
    {
59 1
        return is_array($data) && array_key_exists('text', $data);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 11
    protected function validate()
66
    {
67 11
        if ($this->getProperty('text', '') === '') {
68 1
            throw new TelegramException('You must add some text to the button!');
69
        }
70
71
        // Make sure only 1 of the optional request fields is set.
72 10
        $field_count = array_filter([
73 10
            $this->getRequestContact(),
74 10
            $this->getRequestLocation(),
75 10
            $this->getRequestPoll(),
76
        ]);
77 10
        if (count($field_count) > 1) {
78 1
            throw new TelegramException('You must use only one of these fields: request_contact, request_location, request_poll!');
79
        }
80 9
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 16
    public function __call($method, $args)
86
    {
87
        // Only 1 of these can be set, so clear the others when setting a new one.
88 16
        if (in_array($method, ['setRequestContact', 'setRequestLocation', 'setRequestPoll'], true)) {
89 1
            unset($this->request_contact, $this->request_location, $this->request_poll);
90
        }
91
92 16
        return parent::__call($method, $args);
93
    }
94
}
95