tgbotlib/src/TgBotLib/Objects/InputMedia.php

59 lines
1.8 KiB
PHP
Raw Normal View History

2023-02-13 23:08:54 -05:00
<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2023-02-13 23:08:54 -05:00
use InvalidArgumentException;
use TgBotLib\Enums\Types\InputMediaType;
2023-02-13 23:08:54 -05:00
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia\InputMediaAnimation;
use TgBotLib\Objects\InputMedia\InputMediaAudio;
use TgBotLib\Objects\InputMedia\InputMediaDocument;
use TgBotLib\Objects\InputMedia\InputMediaPhoto;
use TgBotLib\Objects\InputMedia\InputMediaVideo;
2023-02-13 23:08:54 -05:00
abstract class InputMedia implements ObjectTypeInterface
2023-02-13 23:08:54 -05:00
{
2024-10-04 00:36:07 -04:00
protected InputMediaType $type;
2023-02-13 23:08:54 -05:00
/**
* Type of the result, can be photo, video, animation, audio or document
*
2024-10-04 00:36:07 -04:00
* @return InputMediaType
2023-02-13 23:08:54 -05:00
*/
2024-10-04 00:36:07 -04:00
public function getType(): InputMediaType
2023-02-13 23:08:54 -05:00
{
return $this->type;
}
/**
* @inheritDoc
2023-02-13 23:08:54 -05:00
*/
public abstract function toArray(): array;
2023-02-13 23:08:54 -05:00
/**
* @inheritDoc
2023-02-13 23:08:54 -05:00
*/
public static function fromArray(?array $data): ?InputMedia
2023-02-13 23:08:54 -05:00
{
if($data === null)
{
return null;
}
2023-02-13 23:08:54 -05:00
if(!isset($data['type']))
2023-02-14 17:35:16 -05:00
{
throw new InvalidArgumentException('type is not provided');
}
2023-02-13 23:08:54 -05:00
return match (InputMediaType::tryFrom($data['type']))
{
InputMediaType::PHOTO => InputMediaPhoto::fromArray($data),
InputMediaType::VIDEO => InputMediaVideo::fromArray($data),
InputMediaType::ANIMATION => InputMediaAnimation::fromArray($data),
InputMediaType::AUDIO => InputMediaAudio::fromArray($data),
InputMediaType::DOCUMENT => InputMediaDocument::fromArray($data),
default => throw new InvalidArgumentException('Unknown type')
};
2023-02-13 23:08:54 -05:00
}
}