From 492a45a49d3be6d84c343fc8a96bb65d7bc8cce0 Mon Sep 17 00:00:00 2001 From: Netkas Date: Sun, 12 Feb 2023 17:36:18 -0500 Subject: [PATCH] Added \TgBotLib\Objects > Poll --- src/TgBotLib/Objects/Poll.php | 270 ++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 src/TgBotLib/Objects/Poll.php diff --git a/src/TgBotLib/Objects/Poll.php b/src/TgBotLib/Objects/Poll.php new file mode 100644 index 0000000..2f88747 --- /dev/null +++ b/src/TgBotLib/Objects/Poll.php @@ -0,0 +1,270 @@ +id; + } + + /** + * Poll question, 1-300 characters + * + * @return string + */ + public function getQuestion(): string + { + return $this->question; + } + + /** + * List of poll options + * + * @return PollOption[] + */ + public function getOptions(): array + { + return $this->options; + } + + /** + * Total number of users that voted in the poll + * + * @return int + */ + public function getTotalVoterCount(): int + { + return $this->total_voter_count; + } + + /** + * True, if the poll is closed + * + * @return bool + */ + public function isClosed(): bool + { + return $this->is_closed; + } + + /** + * True, if the poll is anonymous + * + * @return bool + */ + public function isAnonymous(): bool + { + return $this->is_anonymous; + } + + /** + * Poll type, currently can be “regular” or “quiz” + * + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * True, if the poll allows multiple answers + * + * @return bool + */ + public function isAllowMultipleAnswers(): bool + { + return $this->allow_multiple_answers; + } + + /** + * Optional. 0-based identifier of the correct answer option. Available only for polls in the quiz mode, which + * are closed, or was sent (not forwarded) by the bot or to the private chat with the bot. + * + * @return int|null + */ + public function getCorrectOptionId(): ?int + { + return $this->correct_option_id; + } + + /** + * Optional. Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in + * a quiz-style poll, 0-200 characters + * + * @return string + */ + public function getExplanation(): string + { + return $this->explanation; + } + + /** + * Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the explanation + * + * @return MessageEntity[]|null + */ + public function getExplanationEntities(): ?array + { + return $this->explanation_entities; + } + + /** + * Optional. Amount of time in seconds the poll will be active after creation + * + * @return int|null + */ + public function getOpenPeriod(): ?int + { + return $this->open_period; + } + + /** + * Optional. Point in time (Unix timestamp) when the poll will be automatically closed + * + * @return int|null + */ + public function getCloseDate(): ?int + { + return $this->close_date; + } + + /** + * Returns an array representation of the object + * + * @return array + */ + public function toArray(): array + { + $options = null; + foreach($this->options as $option) + { + $options[] = $option->toArray(); + } + + return [ + 'id' => $this->id ?? null, + 'question' => $this->question ?? null, + 'options' => $options ?? null, + 'total_voter_count' => $this->total_voter_count ?? null, + 'is_closed' => $this->is_closed ?? null, + 'is_anonymous' => $this->is_anonymous ?? null, + 'type' => $this->type ?? null, + 'allow_multiple_answers' => $this->allow_multiple_answers ?? null, + 'correct_option_id' => $this->correct_option_id ?? null, + 'explanation' => $this->explanation ?? null, + 'explanation_entities' => $this->explanation_entities ?? null, + 'open_period' => $this->open_period ?? null, + 'close_date' => $this->close_date ?? null, + ]; + } + + /** + * Constructs object from array representation + * + * @param array $data + * @return ObjectTypeInterface + */ + public static function fromArray(array $data): ObjectTypeInterface + { + $object = new self(); + + $object->id = $data['id'] ?? null; + $object->question = $data['question'] ?? null; + $object->options = $data['options'] ?? null; + $object->total_voter_count = $data['total_voter_count'] ?? null; + $object->is_closed = $data['is_closed'] ?? null; + $object->is_anonymous = $data['is_anonymous'] ?? null; + $object->type = $data['type'] ?? null; + $object->allow_multiple_answers = $data['allow_multiple_answers'] ?? null; + $object->correct_option_id = $data['correct_option_id'] ?? null; + $object->explanation = $data['explanation'] ?? null; + $object->open_period = $data['open_period'] ?? null; + $object->close_date = $data['close_date'] ?? null; + + $explanation_entities = null; + foreach($data['explanation_entities'] as $explanation_entity) + { + $explanation_entities[] = MessageEntity::fromArray($explanation_entity); + } + $object->explanation_entities = $explanation_entities ?? null; + + return $object; + } + } \ No newline at end of file