Passed
Push — master ( 4c2b59...061776 )
by Shahrad
02:09
created

Keyboard::addRow()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
4
namespace TelegramBot\Entities;
5
6
use TelegramBot\Entity;
7
8
/**
9
 * Class Keyboard
10
 *
11
 * @link https://core.telegram.org/bots/api#replykeyboardmarkup
12
 *
13
 * @method bool   getResizeKeyboard()           Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
14
 * @method bool   getOneTimeKeyboard()          Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
15
 * @method string getInputFieldPlaceholder()    Optional. The placeholder to be shown in the input field when the keyboard is active;
16
 * @method bool   getSelective()                Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object;
17
 *
18
 * @method $this setResizeKeyboard(bool $resize_keyboard)                    Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
19
 * @method $this setOneTimeKeyboard(bool $one_time_keyboard)                 Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
20
 * @method $this setInputFieldPlaceholder(string $input_field_placeholder)   Optional. The placeholder to be shown in the input field when the keyboard is active;
21
 * @method $this setSelective(bool $selective)                               Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object;
22
 */
23
class Keyboard extends Entity
24
{
25
26
    public function __construct(array $data = [])
27
    {
28
        parent::__construct(array_merge($data, [
29
            'resize_keyboard' => false,
30
            'one_time_keyboard' => false,
31
            'input_field_placeholder' => '',
32
            'selective' => false
33
        ]));
34
    }
35
36
    /**
37
     * Remove the current custom keyboard and display the default letter-keyboard.
38
     *
39
     * @link https://core.telegram.org/bots/api/#replykeyboardremove
40
     *
41
     * @param array $data
42
     *
43
     * @return Keyboard
44
     */
45
    public static function remove(array $data = []): Keyboard
46
    {
47
        return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data));
48
    }
49
50
    /**
51
     * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
52
     *
53
     * @link https://core.telegram.org/bots/api#forcereply
54
     *
55
     * @param array $data
56
     *
57
     * @return Keyboard
58
     */
59
    public static function forceReply(array $data = []): Keyboard
60
    {
61
        return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
62
    }
63
64
    /**
65
     * Creates instance of Keyboard
66
     *
67
     * @return Keyboard
68
     */
69
    public static function make(): Keyboard
70
    {
71
        return new self();
72
    }
73
74
    /**
75
     * @param array $rows
76
     * @return Keyboard
77
     */
78
    public function setKeyboard(array $rows): Keyboard
79
    {
80
        foreach ($rows as $row) {
81
            $this->addRow($row);
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * Create a new row in keyboard and add buttons.
89
     *
90
     * @param array<KeyboardButton> $row
91
     * @return Keyboard
92
     */
93
    public function addRow(array $row): Keyboard
94
    {
95
        $keyboard_type = self::getType();
0 ignored issues
show
Bug Best Practice introduced by
The method TelegramBot\Entities\Keyboard::getType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        /** @scrutinizer ignore-call */ 
96
        $keyboard_type = self::getType();
Loading history...
96
97
        if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) {
98
            $this->raw_data[$keyboard_type] = [];
99
        }
100
101
        $new_row = [];
102
        foreach ($row as $button) {
103
            $new_row[] = $button->getRawData();
104
        }
105
106
        $this->raw_data[$keyboard_type][] = $new_row;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Get keyboard button type
113
     *
114
     * @return string ["keyboard"|"inline_keyboard"]
115
     */
116
    public function getType(): string
117
    {
118
        $reflection = new \ReflectionClass(static::class);
119
120
        $class_name = $reflection->getShortName();
121
122
        return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_'));
123
    }
124
125
}
126