tgbotlib/src/TgBotLib/Objects/PollAnswer.php

76 lines
1.8 KiB
PHP
Raw Normal View History

2023-02-12 17:26:16 -05:00
<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2023-02-12 17:26:16 -05:00
use TgBotLib\Interfaces\ObjectTypeInterface;
2023-02-12 17:26:23 -05:00
class PollAnswer implements ObjectTypeInterface
2023-02-12 17:26:16 -05:00
{
2024-10-05 00:48:55 -04:00
private string $poll_id;
private User $user;
2023-02-12 17:26:16 -05:00
/**
* @var int[]
*/
2024-10-05 00:48:55 -04:00
private array $option_ids;
2023-02-12 17:26:16 -05:00
/**
* Unique poll identifier
*
* @return string
*/
public function getPollId(): string
{
return $this->poll_id;
}
/**
* The user, who changed the answer to the poll
*
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* 0-based identifiers of answer options, chosen by the user. May be empty if the user retracted their vote.
*
* @return int[]
*/
public function getOptionIds(): array
{
return $this->option_ids;
}
/**
2024-10-05 00:48:55 -04:00
* @inheritDoc
2023-02-12 17:26:16 -05:00
*/
public function toArray(): array
{
return [
'poll_id' => $this->poll_id,
2023-02-14 17:35:16 -05:00
'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : null,
2023-02-12 17:26:16 -05:00
'option_ids' => $this->option_ids,
];
}
/**
2024-10-05 00:48:55 -04:00
* @inheritDoc
2023-02-12 17:26:16 -05:00
*/
2024-10-05 00:48:55 -04:00
public static function fromArray(?array $data): ?PollAnswer
2023-02-12 17:26:16 -05:00
{
2024-10-05 00:48:55 -04:00
if($data === null)
{
return null;
}
2023-02-12 17:26:16 -05:00
2024-10-05 00:48:55 -04:00
$object = new self();
2023-02-14 17:35:16 -05:00
$object->poll_id = $data['poll_id'] ?? null;
2024-10-05 00:48:55 -04:00
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;
2023-02-12 17:26:16 -05:00
$object->option_ids = $data['option_ids'];
return $object;
}
}