KeyboardButton::setWebApp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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
 */
33
class KeyboardButton extends Entity
34
{
35
36
    /**
37
     * @param array|string $data
38
     */
39
    public function __construct($data)
40
    {
41
        if (is_string($data)) {
42
            $data = ['text' => $data];
43
        }
44
45
        parent::__construct($data ?? []);
46
    }
47
48
    /**
49
     * Creates instance of KeyboardButton
50
     *
51
     * @param string|null $label
52
     * @return KeyboardButton
53
     */
54
    public static function make(string|null $label = null): KeyboardButton
55
    {
56
        return new self($label);
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
     * Optional. If specified, the described Web App will be launched when the button is pressed.
73
     * The Web App will be able to send a “web_app_data” service message. Available in private chats only.
74
     *
75
     * @param string $url
76
     * @return $this
77
     */
78
    public function setWebApp(string $url): KeyboardButton
79
    {
80
        $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]);
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function __call(string $name, array $arguments): mixed
89
    {
90
        if (in_array($name, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) {
91
            unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app);
92
        }
93
94
        return parent::__call($name, $arguments);
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    protected function subEntities(): array
101
    {
102
        return [
103
            'request_poll' => KeyboardButtonPollType::class,
104
            'web_app' => WebAppInfo::class,
105
        ];
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    protected function validate(): void
112
    {
113
        if ($this->getProperty('text', '') === '') {
114
            throw new TelegramException('You must add some text to the button!');
115
        }
116
    }
117
118
}
119