tgbotlib/src/TgBotLib/Objects/PaidMedia.php

53 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
use InvalidArgumentException;
use TgBotLib\Enums\Types\PaidMediaType;
use TgBotLib\Interfaces\ObjectTypeInterface;
2024-10-02 00:18:12 -04:00
use TgBotLib\Objects\PaidMedia\PaidMediaPhoto;
use TgBotLib\Objects\PaidMedia\PaidMediaPreview;
use TgBotLib\Objects\PaidMedia\PaidMediaVideo;
abstract class PaidMedia implements ObjectTypeInterface
{
protected PaidMediaType $type;
/**
* Type of the paid media
*
* @return PaidMediaType
*/
public function getType(): PaidMediaType
{
return $this->type;
}
/**
* @inheritDoc
*/
public abstract function toArray(): array;
/**
* @inheritDoc
*/
2024-10-04 00:48:28 -04:00
public static function fromArray(?array $data): ?PaidMedia
{
2024-10-04 00:48:28 -04:00
if($data === null)
{
return null;
}
if(!isset($data['type']))
{
throw new InvalidArgumentException('Paid media type is required');
}
return match (PaidMediaType::tryFrom($data['type']))
{
PaidMediaType::PHOTO => PaidMediaPhoto::fromArray($data),
PaidMediaType::VIDEO => PaidMediaVideo::fromArray($data),
PaidMediaType::PREVIEW => PaidMediaPreview::fromArray($data),
};
}
}