From 93b4c68d2909559e65d2fb879b60dd98d589357b Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 7 Oct 2024 13:33:56 -0400 Subject: [PATCH] Add setter methods and constructor to ReplyParameters --- src/TgBotLib/Objects/ReplyParameters.php | 138 +++++++++++++++++++++-- 1 file changed, 130 insertions(+), 8 deletions(-) diff --git a/src/TgBotLib/Objects/ReplyParameters.php b/src/TgBotLib/Objects/ReplyParameters.php index 14ddd79..7acb205 100644 --- a/src/TgBotLib/Objects/ReplyParameters.php +++ b/src/TgBotLib/Objects/ReplyParameters.php @@ -18,6 +18,20 @@ private ?array $quote_entities; private ?int $quote_position; + /** + * ReplyParameters constructor. + */ + public function __construct() + { + $this->message_id = 0; + $this->chat_id = null; + $this->allow_sending_without_reply = false; + $this->quote = null; + $this->quote_parse_mode = null; + $this->quote_entities = null; + $this->quote_position = null; + } + /** * Identifier of the message that will be replied to in the current chat, * or in the chat chat_id if it is specified @@ -29,6 +43,19 @@ return $this->message_id; } + /** + * Sets the message id. + * + * @param int $messageId The unique identifier for the message. + * + * @return ReplyParameters Returns the current instance of the ReplyParameters class. + */ + public function setMessageId(int $messageId): ReplyParameters + { + $this->message_id = $messageId; + return $this; + } + /** * Optional. If the message to be replied to is from a different chat, unique identifier for the chat or * username of the channel (in the format @channelusername). Not supported for messages sent on behalf @@ -41,6 +68,20 @@ return $this->chat_id; } + /** + * Sets the chat id. + * + * @param int|string|null $chatId The unique identifier for the chat. + * It can be an integer or string (which may be a username) or null. + * + * @return ?ReplyParameters Returns the current instance of the ReplyParameters class. + */ + public function setChatId(int|string|null $chatId): ?ReplyParameters + { + $this->chat_id = $chatId; + return $this; + } + /** * Optional. Pass True if the message should be sent even if the specified message to be replied to is * not found. Always False for replies in another chat or forum topic. Always True for messages sent on @@ -48,11 +89,23 @@ * * @return bool */ - public function isAllowSendingWithoutReply(): bool + public function allowSendingWithoutReply(): bool { return $this->allow_sending_without_reply; } + /** + * Sets the value of allow_sending_without_reply + * + * @param bool $allow_sending_without_reply + * @return ReplyParameters + */ + public function setAllowSendingWithoutReply(bool $allow_sending_without_reply): ReplyParameters + { + $this->allow_sending_without_reply = $allow_sending_without_reply; + return $this; + } + /** * Optional. Quoted part of the message to be replied to; 0-1024 characters after entities parsing. * The quote must be an exact substring of the message to be replied to, including bold, italic, underline, @@ -66,6 +119,18 @@ return $this->quote; } + /** + * Sets the value of quote + * + * @param string|null $quote + * @return ReplyParameters + */ + public function setQuote(?string $quote): ReplyParameters + { + $this->quote = $quote; + return $this; + } + /** * Optional. Mode for parsing entities in the quote. See formatting options for more details. * @@ -76,6 +141,18 @@ return $this->quote_parse_mode; } + /** + * Sets the value of quote_parse_mode + * + * @param ParseMode|null $quote_parse_mode + * @return ReplyParameters + */ + public function setQuoteParseMode(?ParseMode $quote_parse_mode): ReplyParameters + { + $this->quote_parse_mode = $quote_parse_mode; + return $this; + } + /** * Optional. A list of special entities that appear in the quote. It can be specified * instead of quote_parse_mode. @@ -87,6 +164,18 @@ return $this->quote_entities; } + /** + * Sets the value of quote_entities + * + * @param MessageEntity[]|null $quote_entities + * @return ReplyParameters + */ + public function setQuoteEntities(?array $quote_entities): ReplyParameters + { + $this->quote_entities = $quote_entities; + return $this; + } + /** * Optional. Position of the quote in the original message in UTF-16 code units * @@ -97,20 +186,54 @@ return $this->quote_position; } + /** + * Sets the value of quote_position + * + * @param int|null $quote_position + * @return ReplyParameters + */ + public function setQuotePosition(?int $quote_position): ReplyParameters + { + $this->quote_position = $quote_position; + return $this; + } + /** * @inheritDoc */ public function toArray(): ?array { - return [ + $array = [ 'message_id' => $this->message_id, - 'chat_id' => $this->chat_id, 'allow_sending_without_reply' => $this->allow_sending_without_reply, - 'quote' => $this->quote, - 'quote_parse_mode' => $this->quote_parse_mode?->value, - 'quote_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->quote_entities), - 'quote_position' => $this->quote_position, ]; + + if($this->chat_id !== null) + { + $array['chat_id'] = $this->chat_id; + } + + if($this->quote !== null) + { + $array['quote'] = $this->quote; + } + + if($this->quote_parse_mode !== null) + { + $array['quote_parse_mode'] = $this->quote_parse_mode->value; + } + + if($this->quote_entities !== null) + { + $array['quote_entities'] = array_map(fn(MessageEntity $item) => $item->toArray(), $this->quote_entities); + } + + if($this->quote_position !== null) + { + $array['quote_position'] = $this->quote_position; + } + + return $array; } /** @@ -124,7 +247,6 @@ } $object = new self; - $object->message_id = $data['message_id']; $object->chat_id = $data['chat_id']; $object->allow_sending_without_reply = $data['allow_sending_without_reply'];