Completed
Push — refactor_entities ( c38610...2711d6 )
by Armando
03:36
created

Keyboard::isInlineKeyboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
 * Written by Marco Boretto <[email protected]>
11
 */
12
13
namespace Longman\TelegramBot\Entities;
14
15
use Longman\TelegramBot\Exception\TelegramException;
16
17
/**
18
 * Class ReplyKeyboardMarkup
19
 *
20
 * @link https://core.telegram.org/bots/api#replykeyboardmarkup
21
 *
22
 * @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.
23
 * @method bool getOneTimeKeyboard() Optional. Requests clients to hide 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.
24
 * @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; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
25
 *
26
 * @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.
27
 * @method $this setOneTimeKeyboard(bool $one_time_keyboard) Optional. Requests clients to hide 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.
28
 * @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; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
29
 */
30
class Keyboard extends Entity
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct($data = [])
36
    {
37
        $data = call_user_func_array([$this, 'createFromParams'], func_get_args());
38
        parent::__construct($data);
39
    }
40
41
    /**
42
     * If this keyboard is an inline keyboard.
43
     *
44
     * @return bool
45
     */
46
    public function isInlineKeyboard()
47
    {
48
        return $this instanceof InlineKeyboard;
49
    }
50
51
    /**
52
     * Get the proper keyboard button class for this keyboard.
53
     *
54
     * @return mixed
55
     */
56
    public function getKeyboardButtonClass()
57
    {
58
        return $this->isInlineKeyboard() ? InlineKeyboardButton::class : KeyboardButton::class;
59
    }
60
61
    /**
62
     * Get the type of keyboard, either "inline_keyboard" or "keyboard".
63
     *
64
     * @return string
65
     */
66
    public function getKeyboardType()
67
    {
68
        return $this->isInlineKeyboard() ? 'inline_keyboard' : 'keyboard';
69
    }
70
71
    /**
72
     * If no explicit keyboard is passed, try to create one from the parameters.
73
     *
74
     * @return array
75
     */
76
    protected function createFromParams()
77
    {
78
        /** @var KeyboardButton|InlineKeyboardButton $button_class */
79
        $button_class  = $this->getKeyboardButtonClass();
80
        $keyboard_type = $this->getKeyboardType();
81
82
        // If the inline_keyboard isn't set directly, try to create one from the arguments.
83
        $data = func_get_arg(0);
84
        if (!array_key_exists($keyboard_type, $data)) {
85
            $new_keyboard = [];
86
            foreach (func_get_args() as $row) {
87
                if (is_array($row)) {
88
                    $new_row = [];
89
                    if ($button_class::couldBe($row)) {
90
                        $new_row[] = new $button_class($row);
91
                    } else {
92
                        foreach ($row as $button) {
93
                            if ($button instanceof $button_class) {
94
                                $new_row[] = $button;
95
                            } elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) {
96
                                $new_row[] = new $button_class($button);
97
                            }
98
                        }
99
                    }
100
                } else {
101
                    $new_row = [new $button_class($row)];
102
                }
103
                $new_keyboard[] = $new_row;
104
            }
105
106
            if (!empty($new_keyboard)) {
107
                $data = [$keyboard_type => $new_keyboard];
108
            }
109
        }
110
111
        return $data;
112
    }
113
114
    /**
115
     * Create a new row in keyboard and add buttons.
116
     *
117
     * @return $this
118
     */
119
    public function addRow()
120
    {
121
        $keyboard_type          = $this->getKeyboardType();
122
        $this->$keyboard_type[] = func_get_args();
123
124
        return $this;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 View Code Duplication
    protected function validate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $keyboard = $this->getProperty('keyboard');
133
134
        if ($keyboard !== null) {
135
            if (!is_array($keyboard)) {
136
                throw new TelegramException('Keyboard field is not an array!');
137
            }
138
139
            foreach ($keyboard as $item) {
140
                if (!is_array($item)) {
141
                    throw new TelegramException('Keyboard subfield is not an array!');
142
                }
143
            }
144
        }
145
    }
146
147
    /**
148
     * Hide the current custom keyboard and display the default letter-keyboard.
149
     *
150
     * @link https://core.telegram.org/bots/api#replykeyboardhide
151
     *
152
     * @param array $data
153
     *
154
     * @return \Longman\TelegramBot\Entities\Keyboard
155
     * @throws \Longman\TelegramBot\Exception\TelegramException
156
     */
157
    public static function hide(array $data = [])
158
    {
159
        return new static(array_merge(['keyboard' => null, 'hide_keyboard' => true, 'selective' => false], $data));
160
    }
161
162
    /**
163
     * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
164
     *
165
     * @link https://core.telegram.org/bots/api#forcereply
166
     *
167
     * @param array $data
168
     *
169
     * @return \Longman\TelegramBot\Entities\Keyboard
170
     * @throws \Longman\TelegramBot\Exception\TelegramException
171
     */
172
    public static function forceReply(array $data = [])
173
    {
174
        return new static(array_merge(['keyboard' => null, 'force_reply' => true, 'selective' => false], $data));
175
    }
176
}
177