Passed
Push — master ( 9c7b82...f78b46 )
by Shahrad
02:20
created

Keyboard::setKeyboard()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
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
	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' => null,
32
			'selective' => false
33
		]));
34
	}
35
36
	/**
37
	 * Create a new row in keyboard and add buttons.
38
	 *
39
	 * @param array<KeyboardButton> $row
40
	 * @return Keyboard
41
	 */
42
	public function addRow(array $row): Keyboard
43
	{
44
		$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

44
		/** @scrutinizer ignore-call */ 
45
  $keyboard_type = self::getType();
Loading history...
45
46
		if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) {
47
			$this->raw_data[$keyboard_type] = [];
48
		}
49
50
		$new_row = [];
51
		foreach ($row as $button) {
52
			$new_row[] = $button->getRawData();
53
		}
54
55
		$this->raw_data[$keyboard_type][] = $new_row;
56
57
		return $this;
58
	}
59
60
	/**
61
	 * Remove the current custom keyboard and display the default letter-keyboard.
62
	 *
63
	 * @link https://core.telegram.org/bots/api/#replykeyboardremove
64
	 *
65
	 * @param array $data
66
	 *
67
	 * @return Keyboard
68
	 */
69
	public static function remove(array $data = []): Keyboard
70
	{
71
		return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data));
72
	}
73
74
	/**
75
	 * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
76
	 *
77
	 * @link https://core.telegram.org/bots/api#forcereply
78
	 *
79
	 * @param array $data
80
	 *
81
	 * @return Keyboard
82
	 */
83
	public static function forceReply(array $data = []): Keyboard
84
	{
85
		return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
86
	}
87
88
	/**
89
	 * Get keyboard button type
90
	 *
91
	 * @return string ["keyboard"|"inline_keyboard"]
92
	 */
93
	public function getType(): string
94
	{
95
		$reflection = new \ReflectionClass(static::class);
96
97
		$class_name = $reflection->getShortName();
98
99
		return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_'));
100
	}
101
102
	/**
103
	 * Creates instance of Keyboard
104
	 *
105
	 * @return Keyboard
106
	 */
107
	public static function make(): Keyboard
108
	{
109
		return new self();
110
	}
111
112
	/**
113
	 * @param array $rows
114
	 * @return Keyboard
115
	 */
116
	public function setKeyboard(array $rows): Keyboard
117
	{
118
		foreach ($rows as $row) {
119
			$this->addRow($row);
120
		}
121
122
		return $this;
123
	}
124
125
}
126