KeyboardButton   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 52
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A couldBe() 0 4 2
A validate() 0 10 4
A __call() 0 9 2
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 KeyboardButton
17
 *
18
 * @link https://core.telegram.org/bots/api#keyboardbutton
19
 *
20
 * @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
21
 * @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
22
 * @method bool   getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
23
 *
24
 * @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
25
 * @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
26
 * @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
27
 */
28
class KeyboardButton extends Entity
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 21
    public function __construct($data)
34
    {
35 21
        if (is_string($data)) {
36 8
            $data = ['text' => $data];
37
        }
38 21
        parent::__construct($data);
39 16
    }
40
41
    /**
42
     * Check if the passed data array could be a KeyboardButton.
43
     *
44
     * @param array $data
45
     *
46
     * @return bool
47
     */
48 1
    public static function couldBe($data)
49
    {
50 1
        return is_array($data) && array_key_exists('text', $data);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 11
    protected function validate()
57
    {
58 11
        if ($this->getProperty('text', '') === '') {
59 1
            throw new TelegramException('You must add some text to the button!');
60
        }
61
62 10
        if ($this->getRequestContact() && $this->getRequestLocation()) {
63 1
            throw new TelegramException('You must use only one of these fields: request_contact, request_location!');
64
        }
65 9
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 16
    public function __call($method, $args)
71
    {
72
        // Only 1 of these can be set, so clear the others when setting a new one.
73 16
        if (in_array($method, ['setRequestContact', 'setRequestLocation'], true)) {
74 1
            unset($this->request_contact, $this->request_location);
75
        }
76
77 16
        return parent::__call($method, $args);
78
    }
79
}
80