tgbotlib/src/TgBotLib/Objects/InputMedia.php

89 lines
2.7 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;
protected string $media;
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;
}
/**
* File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
* pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name> to
* upload a new one using multipart/form-data under <file_attach_name> name.
*
* @return string
*/
public function getMedia(): string
{
return $this->media;
}
/**
* Sets the media content to upload
*
* @param string $media
* @return $this
*/
public function setMedia(string $media): self
{
$this->media = $media;
return $this;
}
2023-02-13 23:08:54 -05:00
/**
* @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
if(!isset($data['media']))
{
throw new InvalidArgumentException('media is not provided');
}
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
}
}