From d5289930695071367ee4d3f5160c0cdc73769478 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 9 Oct 2024 14:07:41 -0400 Subject: [PATCH] Add constructors and improve toArray implementations Introduce constructors in InlineKeyboardButton and optimize the toArray methods for both InlineKeyboardButton and InlineKeyboardMarkup classes. This improves object initialization and makes the array conversion process more robust and readable. --- src/TgBotLib/Objects/InlineKeyboardButton.php | 69 +++++++++++++++---- src/TgBotLib/Objects/InlineKeyboardMarkup.php | 30 ++++---- 2 files changed, 74 insertions(+), 25 deletions(-) diff --git a/src/TgBotLib/Objects/InlineKeyboardButton.php b/src/TgBotLib/Objects/InlineKeyboardButton.php index 8547604..137f828 100644 --- a/src/TgBotLib/Objects/InlineKeyboardButton.php +++ b/src/TgBotLib/Objects/InlineKeyboardButton.php @@ -20,6 +20,19 @@ private ?CallbackGame $callback_game; private bool $pay; + public function __construct() + { + $this->text = (string)null; + $this->url = null; + $this->callback_data = null; + $this->web_app = null; + $this->login_url = null; + $this->switch_inline_query = null; + $this->switch_inline_query_current_chat = null; + $this->callback_game = null; + $this->pay = false; + } + /** * Label text on the button * @@ -278,17 +291,49 @@ */ public function toArray(): array { - return [ - 'text' => $this->text ?? null, - 'url' => $this->url ?? null, - 'callback_data' => $this->callback_data ?? null, - 'web_app' => $this->web_app?->toArray(), - 'login_url' => $this->login_url?->toArray(), - 'switch_inline_query' => $this->switch_inline_query ?? null, - 'switch_inline_query_current_chat' => $this->switch_inline_query_current_chat ?? null, - 'callback_game' => $this->callback_game?->toArray(), - 'pay' => $this->pay ?? null - ]; + $array = ['text' => $this->text]; + + if ($this->url !== null) + { + $array['url'] = $this->url; + } + + if ($this->callback_data !== null) + { + $array['callback_data'] = $this->callback_data; + } + + if ($this->web_app !== null) + { + $array['web_app'] = $this->web_app->toArray(); + } + + if ($this->login_url !== null) + { + $array['login_url'] = $this->login_url->toArray(); + } + + if ($this->switch_inline_query !== null) + { + $array['switch_inline_query'] = $this->switch_inline_query; + } + + if ($this->switch_inline_query_current_chat !== null) + { + $array['switch_inline_query_current_chat'] = $this->switch_inline_query_current_chat; + } + + if ($this->callback_game !== null) + { + $array['callback_game'] = $this->callback_game->toArray(); + } + + if ($this->pay) + { + $array['pay'] = true; + } + + return $array; } /** @@ -310,7 +355,7 @@ $object->switch_inline_query = $data['switch_inline_query'] ?? null; $object->switch_inline_query_current_chat = $data['switch_inline_query_current_chat'] ?? null; $object->callback_game = isset($data['callback_game']) && is_array($data['callback_game']) ? CallbackGame::fromArray($data['callback_game']) : null; - $object->pay = $data['pay'] ?? null; + $object->pay = $data['pay'] ?? false; return $object; } diff --git a/src/TgBotLib/Objects/InlineKeyboardMarkup.php b/src/TgBotLib/Objects/InlineKeyboardMarkup.php index b6c96af..f6f4958 100644 --- a/src/TgBotLib/Objects/InlineKeyboardMarkup.php +++ b/src/TgBotLib/Objects/InlineKeyboardMarkup.php @@ -1,6 +1,5 @@ inline_keyboard !== null) + $keyboard = []; + foreach ($this->inline_keyboard as $row) { - /** @var InlineKeyboardButton $item */ - foreach ($this->inline_keyboard as $item) + $buttonRow = []; + foreach ($row as $button) { - $data[][] = $item->toArray(); + $buttonRow[] = $button->toArray(); } + + $keyboard[] = $buttonRow; } - return $data; + return ['inline_keyboard' => $keyboard]; } /** @@ -79,20 +79,24 @@ */ public static function fromArray(?array $data): ?InlineKeyboardMarkup { - if($data === null) + if ($data === null || !isset($data['inline_keyboard'])) { return null; } $object = new self(); - $object->inline_keyboard = []; - foreach($data as $item) + foreach ($data['inline_keyboard'] as $row) { - $object->inline_keyboard[] = InlineKeyboardButton::fromArray($item); + $buttons = []; + foreach ($row as $buttonData) + { + $buttons[] = InlineKeyboardButton::fromArray($buttonData); + } + + $object->addRow(...$buttons); } return $object; } - } \ No newline at end of file