@@ -23,103 +23,103 @@ |
||
| 23 | 23 | class Keyboard extends Entity |
| 24 | 24 | { |
| 25 | 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 | - * 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(); |
|
| 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 | - } |
|
| 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 | + * 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(); |
|
| 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 | 124 | |
| 125 | 125 | } |