Passed
Push — master ( 02a361...e20c00 )
by Shahrad
02:40
created

KeyboardButton::subEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
4
namespace TelegramBot\Entities;
5
6
use TelegramBot\Entity;
7
use TelegramBot\Exception\TelegramException;
8
9
/**
10
 * Class KeyboardButton
11
 *
12
 * 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.
13
 *
14
 * @link https://core.telegram.org/bots/api#keyboardbutton
15
 *
16
 * @property bool $request_contact
17
 * @property bool $request_location
18
 * @property KeyboardButtonPollType $request_poll
19
 * @property WebAppInfo $web_app
20
 *
21
 *
22
 * @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
23
 * @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
24
 * @method bool                   getRequestLocation()  Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
25
 * @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
26
 * @method WebAppInfo             getWebApp()           Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.
27
 *
28
 * @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
29
 * @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
30
 * @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
31
 * @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
32
 * @method $this setWebApp(WebAppInfo $web_app)                         Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.
33
 */
34
class KeyboardButton extends Entity
35
{
36
37
    /**
38
     * @param array|string $data
39
     */
40
    public function __construct($data)
41
    {
42
        if (is_string($data)) {
43
            $data = ['text' => $data];
44
        }
45
        parent::__construct($data);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    protected function subEntities(): array
52
    {
53
        return [
54
            'request_poll' => KeyboardButtonPollType::class,
55
            'web_app' => WebAppInfo::class,
56
        ];
57
    }
58
59
    /**
60
     * Check if the passed data array could be a KeyboardButton.
61
     *
62
     * @param array $data
63
     *
64
     * @return bool
65
     */
66
    public static function couldBe(array $data): bool
67
    {
68
        return array_key_exists('text', $data);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function validate(): void
75
    {
76
        if ($this->getProperty('text', '') === '') {
77
            throw new TelegramException('You must add some text to the button!');
78
        }
79
80
        // Make sure only 1 of the optional request fields is set.
81
        $field_count = array_filter([
82
            $this->getRequestContact(),
83
            $this->getRequestLocation(),
84
            $this->getRequestPoll(),
85
            $this->getWebApp(),
86
        ]);
87
        if (count($field_count) > 1) {
88
            throw new TelegramException('You must use only one of these fields: request_contact, request_location, request_poll, web_app!');
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function __call(string $name, array $arguments): mixed
96
    {
97
        // Only 1 of these can be set, so clear the others when setting a new one.
98
        if (in_array($name, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) {
99
            unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app);
100
        }
101
102
        return parent::__call($name, $arguments);
103
    }
104
105
}
106