KeyboardButton::couldBe()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 Longman\TelegramBot\Entities;
13
14
/**
15
 * Class KeyboardButton
16
 *
17
 * 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.
18
 *
19
 * @link https://core.telegram.org/bots/api#keyboardbutton
20
 *
21
 * @property KeyboardButtonRequestUsers $request_users
22
 * @property KeyboardButtonRequestChat  $request_chat
23
 * @property bool                       $request_contact
24
 * @property bool                       $request_location
25
 * @property KeyboardButtonPollType     $request_poll
26
 * @property WebAppInfo                 $web_app
27
 *
28
 * @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
29
 * @method KeyboardButtonRequestUsers getRequestUsers()    Optional. If specified, pressing the button will open a list of suitable users. Identifiers of selected users will be sent to the bot in a “users_shared” service message. Available in private chats only.
30
 * @method KeyboardButtonRequestChat  getRequestChat()     Optional. If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only.
31
 * @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
32
 * @method bool                       getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
33
 * @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
34
 * @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.
35
 *
36
 * @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
37
 * @method $this setRequestUsers(KeyboardButtonRequestUsers $request_users) Optional. If specified, pressing the button will open a list of suitable users. Identifiers of selected users will be sent to the bot in a “users_shared” service message. Available in private chats only.
38
 * @method $this setRequestChat(KeyboardButtonRequestChat $request_chat)    Optional. If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only.
39
 * @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
40
 * @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
41
 * @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
42
 * @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.
43
 */
44
class KeyboardButton extends Entity
45
{
46
    /**
47
     * @param array|string $data
48
     */
49
    public function __construct($data)
50
    {
51 16
        if (is_string($data)) {
52
            $data = ['text' => $data];
53 16
        }
54 8
        parent::__construct($data);
55
    }
56 16
57
    protected function subEntities(): array
58
    {
59 13
        return [
60
            'request_users' => KeyboardButtonRequestUsers::class,
61 13
            'request_chat'  => KeyboardButtonRequestChat::class,
62 13
            'request_poll'  => KeyboardButtonPollType::class,
63 13
            'web_app'       => WebAppInfo::class,
64 13
        ];
65 13
    }
66 13
67
    /**
68
     * Check if the passed data array could be a KeyboardButton.
69
     *
70
     * @param array $data
71
     *
72
     * @return bool
73
     */
74
    public static function couldBe(array $data): bool
75
    {
76 1
        return array_key_exists('text', $data);
77
    }
78 1
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function __call($method, $args)
83
    {
84 13
        // Only 1 of these can be set, so clear the others when setting a new one.
85
        if (in_array($method, ['setRequestUsers', 'setRequestChat', 'setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) {
86
            unset($this->request_users, $this->request_chat, $this->request_contact, $this->request_location, $this->request_poll, $this->web_app);
87 13
        }
88
89
        return parent::__call($method, $args);
90
    }
91
}
92