Passed
Push — master ( 0f659e...1ce9aa )
by Shahrad
02:00
created

Keyboard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
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
    /**
27
     * Remove the current custom keyboard and display the default letter-keyboard.
28
     *
29
     * @link https://core.telegram.org/bots/api/#replykeyboardremove
30
     *
31
     * @param array $data
32
     *
33
     * @return Keyboard
34
     */
35
    public static function remove(array $data = []): Keyboard
36
    {
37
        return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data));
38
    }
39
40
    /**
41
     * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
42
     *
43
     * @link https://core.telegram.org/bots/api#forcereply
44
     *
45
     * @param array $data
46
     *
47
     * @return Keyboard
48
     */
49
    public static function forceReply(array $data = []): Keyboard
50
    {
51
        return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
52
    }
53
54
    /**
55
     * Creates instance of Keyboard
56
     *
57
     * @return Keyboard
58
     */
59
    public static function make(): Keyboard
60
    {
61
        return new self([]);
62
    }
63
64
    /**
65
     * @param array $rows
66
     * @return array
67
     */
68
    public function setKeyboard(array $rows): array
69
    {
70
        foreach ($rows as $row) {
71
            $this->addRow($row);
72
        }
73
74
        return $this->getRawData();
75
    }
76
77
    /**
78
     * Create a new row in keyboard and add buttons.
79
     *
80
     * @param array<KeyboardButton> $row
81
     * @return Keyboard
82
     */
83
    public function addRow(array $row): Keyboard
84
    {
85
        $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

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