1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-11 07:48:20 +01:00
MadelineProto/src/EventHandler/AbstractPoll.php

104 lines
3.8 KiB
PHP
Raw Normal View History

2024-01-05 16:12:55 +01:00
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
* @copyright 2016-2023 Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler;
2024-01-17 11:03:20 +01:00
use danog\MadelineProto\EventHandler\Poll\MultiplePoll;
use danog\MadelineProto\EventHandler\Poll\PollAnswer;
use danog\MadelineProto\EventHandler\Poll\QuizPoll;
use danog\MadelineProto\EventHandler\Poll\SinglePoll;
2024-01-05 16:12:55 +01:00
use JsonSerializable;
use ReflectionClass;
use ReflectionProperty;
/** Poll */
abstract class AbstractPoll implements JsonSerializable
{
/** ID of the poll */
public readonly int $id;
/** Whether the poll is closed and doesnt accept any more answers */
public readonly bool $closed;
/** The question of the poll */
public readonly string $question;
/** @var list<PollAnswer> 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<int> IDs of the last users that recently voted in the poll */
public readonly array $recentVoters;
2024-01-17 11:03:20 +01:00
/** Total number of people that voted in the poll */
2024-01-05 16:12:55 +01:00
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;
2024-01-17 11:03:20 +01:00
$this->recentVoters = $rawPoll['poll']['public_voters'] ?: [];
2024-01-05 16:12:55 +01:00
$this->totalVoters = $rawPoll['results']['total_voters'];
2024-01-17 11:03:20 +01:00
$this->answers = self::getPollAnswers($rawPoll['poll']['answers'], $rawPoll['results']['results'] ?? []);
2024-01-05 16:12:55 +01:00
}
2024-01-17 11:03:20 +01:00
public static function fromRawPoll(array $rawPoll): AbstractPoll
2024-01-05 16:12:55 +01:00
{
2024-01-17 11:03:20 +01:00
if ($rawPoll['poll']['quiz']) {
2024-01-05 16:12:55 +01:00
return new QuizPoll($rawPoll);
2024-01-17 11:03:20 +01:00
}
2024-01-05 16:12:55 +01:00
2024-01-17 11:03:20 +01:00
if ($rawPoll['poll']['multiple_choice']) {
2024-01-05 16:12:55 +01:00
return new MultiplePoll($rawPoll);
2024-01-17 11:03:20 +01:00
}
2024-01-05 16:12:55 +01:00
return new SinglePoll($rawPoll);
}
2024-01-17 11:03:20 +01:00
/**
* @return list<PollAnswer>
*/
private static function getPollAnswers(array $answers, array $result): array
2024-01-05 16:12:55 +01:00
{
$out = [];
foreach ($answers as $key => $value) {
2024-01-17 11:03:20 +01:00
$merge = array_merge($value, $result[$key] ?? []);
2024-01-05 16:12:55 +01:00
$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;
}
}