tgbotlib/src/TgBotLib/Objects/Inline/InlineKeyboardMarkup.php

92 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/** @noinspection PhpMissingFieldTypeInspection */
2024-10-03 19:55:48 -04:00
namespace TgBotLib\Objects\Inline;
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;
}
/**
* 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;
}
/**
* Returns an array representation of the object
*
2023-02-14 17:35:16 -05:00
* @return array[][]
*/
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;
}
/**
* Constructs the object from an array representation
*
* @param array $data
2023-02-16 15:27:57 -05:00
* @return InlineKeyboardMarkup
*/
2023-02-16 15:27:57 -05:00
public static function fromArray(array $data): self
{
$object = new self();
2023-02-14 17:35:16 -05:00
$object->inline_keyboard = [];
foreach($data as $item)
{
$object->inline_keyboard[] = InlineKeyboardButton::fromArray($item);
}
return $object;
}
}