2023-02-13 13:57:03 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2024-10-03 21:18:35 -04:00
|
|
|
namespace TgBotLib\Objects;
|
2023-02-13 13:57:03 -05:00
|
|
|
|
|
|
|
use TgBotLib\Interfaces\ObjectTypeInterface;
|
|
|
|
|
|
|
|
class InlineKeyboardMarkup implements ObjectTypeInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var InlineKeyboardButton[][]
|
|
|
|
*/
|
|
|
|
private $inline_keyboard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of button rows, each represented by an Array of InlineKeyboardButton objects
|
|
|
|
*
|
|
|
|
* @see https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
|
|
|
* @return InlineKeyboardButton[][]
|
|
|
|
*/
|
|
|
|
public function getInlineKeyboard(): array
|
|
|
|
{
|
|
|
|
return $this->inline_keyboard;
|
|
|
|
}
|
2023-04-23 16:53:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a row of buttons
|
|
|
|
*
|
|
|
|
* @param InlineKeyboardButton ...$buttons
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addRow(InlineKeyboardButton ...$buttons): self
|
|
|
|
{
|
|
|
|
$this->inline_keyboard[] = $buttons;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a row of buttons by index
|
|
|
|
*
|
|
|
|
* @param int $index
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function removeRow(int $index): self
|
|
|
|
{
|
|
|
|
unset($this->inline_keyboard[$index]);
|
|
|
|
return $this;
|
|
|
|
}
|
2023-02-13 13:57:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array representation of the object
|
|
|
|
*
|
2023-02-14 17:35:16 -05:00
|
|
|
* @return array[][]
|
2023-02-13 13:57:03 -05:00
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
2023-02-14 17:35:16 -05:00
|
|
|
$data = [];
|
|
|
|
|
|
|
|
if ($this->inline_keyboard !== null)
|
|
|
|
{
|
|
|
|
/** @var InlineKeyboardButton $item */
|
|
|
|
foreach ($this->inline_keyboard as $item)
|
|
|
|
{
|
|
|
|
$data[][] = $item->toArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2023-02-13 13:57:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the object from an array representation
|
|
|
|
*
|
2024-10-03 21:14:27 -04:00
|
|
|
* @param array|null $data
|
|
|
|
* @return InlineKeyboardMarkup|null
|
2023-02-13 13:57:03 -05:00
|
|
|
*/
|
2024-10-03 21:14:27 -04:00
|
|
|
public static function fromArray(?array $data): ?InlineKeyboardMarkup
|
2023-02-13 13:57:03 -05:00
|
|
|
{
|
2024-10-03 21:14:27 -04:00
|
|
|
if($data === null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-02-14 17:35:16 -05:00
|
|
|
|
2024-10-03 21:14:27 -04:00
|
|
|
$object = new self();
|
2023-02-14 17:35:16 -05:00
|
|
|
$object->inline_keyboard = [];
|
|
|
|
|
|
|
|
foreach($data as $item)
|
|
|
|
{
|
|
|
|
$object->inline_keyboard[] = InlineKeyboardButton::fromArray($item);
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:57:03 -05:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|