. * * @author Amir Hossein Jafari * @copyright 2016-2023 Amir Hossein Jafari * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 * @link https://docs.madelineproto.xyz MadelineProto documentation */ namespace danog\MadelineProto\EventHandler; use JsonSerializable; use ReflectionClass; use ReflectionProperty; use danog\MadelineProto\EventHandler\Poll\QuizPoll; use danog\MadelineProto\EventHandler\Poll\SinglePoll; use danog\MadelineProto\EventHandler\Poll\PollAnswer; use danog\MadelineProto\EventHandler\Poll\MultiplePoll; /** Poll */ abstract class AbstractPoll implements JsonSerializable { /** ID of the poll */ public readonly int $id; /** Whether the poll is closed and doesn’t accept any more answers */ public readonly bool $closed; /** The question of the poll */ public readonly string $question; /** @var list The possible answers */ public readonly array $answers; /** Amount of time in seconds the poll will be active after creation, 5-600 */ public readonly ?int $closePeriod; /** Point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 600 seconds in the future */ public readonly ?int $closeDate; /** @var list IDs of the last users that recently voted in the poll */ public readonly array $recentVoters; /** Total number of people that voted in the poll */ public readonly int $totalVoters; /** @internal */ public function __construct(array $rawPoll) { $this->id = $rawPoll['poll']['id']; $this->closed = $rawPoll['poll']['closed']; $this->question = $rawPoll['poll']['question']; $this->closeDate = $rawPoll['poll']['close_date'] ?? null; $this->closePeriod = $rawPoll['poll']['close_period'] ?? null; $this->recentVoters = $rawPoll['poll']['public_voters'] ? $rawPoll['results']['recent_voters'] : []; $this->totalVoters = $rawPoll['results']['total_voters']; $this->answers = $this->getPollAnswers($rawPoll['poll']['answers'], $rawPoll['results']['results'] ?? []); } public static function fromRawPoll(array $rawPoll) { if ($rawPoll['poll']['quiz']) return new QuizPoll($rawPoll); if ($rawPoll['poll']['multiple_choice']) return new MultiplePoll($rawPoll); return new SinglePoll($rawPoll); } private function getPollAnswers(array $answers, array $result): array { $out = []; foreach ($answers as $key => $value) { $merge = array_merge($value, $result[$key] ?? []); $out[] = new PollAnswer($merge); } return $out; } /** @internal */ public function jsonSerialize(): mixed { $res = ['_' => static::class]; $refl = new ReflectionClass($this); foreach ($refl->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) { $res[$prop->getName()] = $prop->getValue($this); } return $res; } }