tgbotlib/src/TgBotLib/Objects/MessageOrigin.php

66 lines
1.7 KiB
PHP
Raw Normal View History

2024-09-30 13:04:23 -04:00
<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2024-09-30 13:04:23 -04:00
use InvalidArgumentException;
use TgBotLib\Enums\Types\MessageOriginType;
use TgBotLib\Interfaces\ObjectTypeInterface;
2024-10-02 00:18:12 -04:00
use TgBotLib\Objects\MessageOrigin\MessageOriginChannel;
use TgBotLib\Objects\MessageOrigin\MessageOriginChat;
use TgBotLib\Objects\MessageOrigin\MessageOriginHiddenUser;
use TgBotLib\Objects\MessageOrigin\MessageOriginUser;
2024-09-30 13:04:23 -04:00
abstract class MessageOrigin implements ObjectTypeInterface
{
protected MessageOriginType $type;
protected int $date;
/**
*
* @return MessageOriginType
*/
public function getType(): MessageOriginType
{
return $this->type;
}
/**
* Retrieves the date.
*
* @return int The date value.
*/
public function getDate(): int
{
return $this->date;
}
/**
2024-10-04 00:47:04 -04:00
* @inheritDoc
2024-09-30 13:04:23 -04:00
*/
public abstract function toArray(): array;
/**
2024-10-04 00:47:04 -04:00
* @inheritDoc
2024-09-30 13:04:23 -04:00
*/
2024-10-04 00:47:04 -04:00
public static function fromArray(?array $data): ?MessageOrigin
2024-09-30 13:04:23 -04:00
{
2024-10-04 00:47:04 -04:00
if($data === null)
{
return null;
}
2024-09-30 13:04:23 -04:00
if(!isset($data['type']))
{
throw new InvalidArgumentException('Message origin type is required');
}
return match (MessageOriginType::tryFrom($data['type']))
{
MessageOriginType::USER => MessageOriginUser::fromArray($data),
MessageOriginType::CHAT => MessageOriginChat::fromArray($data),
MessageOriginType::HIDDEN_USER => MessageOriginHiddenUser::fromArray($data),
MessageOriginType::CHANNEL => MessageOriginChannel::fromArray($data),
default => throw new InvalidArgumentException('Unknown message origin type'),
};
}
}