tgbotlib/src/TgBotLib/Objects/InputMedia/InputMediaAnimation.php
2024-10-04 00:36:07 -04:00

171 lines
No EOL
5.5 KiB
PHP

<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace TgBotLib\Objects\InputMedia;
use TgBotLib\Enums\Types\InputMediaType;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
class InputMediaAnimation extends InputMedia implements ObjectTypeInterface
{
private string $media;
private ?string $thumb;
private ?string $caption;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
private ?array $caption_entities;
private ?int $width;
private ?int $height;
private ?int $duration;
private bool $has_spoiler;
/**
* 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.
*
* @see https://core.telegram.org/bots/api#sending-files
* @return string
*/
public function getMedia(): string
{
return $this->media;
}
/**
* Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported
* server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail's width and
* height should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can't
* be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the
* thumbnail was uploaded using multipart/form-data under <file_attach_name>.
*
* @see https://core.telegram.org/bots/api#sending-files
* @return string|null
*/
public function getThumb(): ?string
{
return $this->thumb;
}
/**
* Optional. Caption of the animation to be sent, 0-1024 characters after entities parsing
*
* @see https://core.telegram.org/bots/api#sending-files
* @return string|null
*/
public function getCaption(): ?string
{
return $this->caption;
}
/**
* Optional. Mode for parsing entities in the animation caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return ParseMode|null
*/
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
/**
* Optional. List of special entities that appear in the caption, which can be specified instead of parse_mode
*
* @return MessageEntity[]|null
*/
public function getCaptionEntities(): ?array
{
return $this->caption_entities;
}
/**
* Optional. Animation width
*
* @return int|null
*/
public function getWidth(): ?int
{
return $this->width;
}
/**
* Optional. Animation height
*
* @return int|null
*/
public function getHeight(): ?int
{
return $this->height;
}
/**
* Optional. Animation duration in seconds
*
* @return int|null
*/
public function getDuration(): ?int
{
return $this->duration;
}
/**
* Optional. Pass True if the animation needs to be covered with a spoiler animation
*
* @return bool
*/
public function hasSpoiler(): bool
{
return $this->has_spoiler;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type->value,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode->value,
'caption_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities),
'width' => $this->width,
'height' => $this->height,
'duration' => $this->duration,
'has_spoiler' => $this->has_spoiler,
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?InputMediaAnimation
{
if($data === null)
{
return null;
}
$object = new static();
$object->type = $data['type'] ?? InputMediaType::ANIMATION;
$object->media = $data['media'] ?? null;
$object->thumb = $data['thumb'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = isset($data['parse_mode']) ? ParseMode::tryFrom($data['parse_mode']) ?? null : null;
$object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []);
$object->width = $data['width'] ?? null;
$object->height = $data['height'] ?? null;
$object->duration = $data['duration'] ?? null;
$object->has_spoiler = $data['has_spoiler'] ?? false;
return $object;
}
}