Updated InputMedia objects

This commit is contained in:
netkas 2024-10-04 00:36:07 -04:00
parent 1d11ef3b2a
commit 3774b7f8c0
7 changed files with 113 additions and 415 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace TgBotLib\Enums\Types;
enum ParseMode : string
{
case HTML = 'HTML';
case MARKDOWN = 'Markdown';
case MARKDOWN_V2 = 'MarkdownV2';
}

View file

@ -15,17 +15,14 @@
abstract class InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
protected $type;
protected InputMediaType $type;
/**
* Type of the result, can be photo, video, animation, audio or document
*
* @return string
* @return InputMediaType
*/
public function getType(): string
public function getType(): InputMediaType
{
return $this->type;
}

View file

@ -5,71 +5,25 @@
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 implements ObjectTypeInterface
class InputMediaAnimation extends InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $media;
/**
* @var string|null
*/
private $thumb;
/**
* @var string|null
*/
private $caption;
/**
* @var string|null
*/
private $parse_mode;
private string $media;
private ?string $thumb;
private ?string $caption;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var int|null
*/
private $width;
/**
* @var int|null
*/
private $height;
/**
* @var int|null
*/
private $duration;
/**
* @var bool
*/
private $has_spoiler;
/**
* Type of the result, must be animation
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
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
@ -114,9 +68,9 @@
* Optional. Mode for parsing entities in the animation caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return string|null
* @return ParseMode|null
*/
public function getParseMode(): ?string
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
@ -172,25 +126,17 @@
}
/**
* Returns an array representation of the object.
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type,
'type' => $this->type->value,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'caption_entities' => is_array($this->caption_entities) ? array_map(function ($item) {
if($item instanceof ObjectTypeInterface)
{
return $item->toArray();
}
return $item;
}, $this->caption_entities) : null,
'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,
@ -199,24 +145,22 @@
}
/**
* Constructs object from an array representation.
*
* @param array $data
* @return InputMediaAnimation
* @noinspection DuplicatedCode
* @inheritDoc
*/
public static function fromArray(array $data): self
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 = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(function ($item)
{
return MessageEntity::fromArray($item);
}, $data['caption_entities']) : 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;
@ -224,29 +168,4 @@
return $object;
}
/**
* Constructs object from InputMedia
*
* @param InputMedia $inputMedia
* @return InputMediaAnimation
* @noinspection DuplicatedCode
*/
public static function fromInputMedia(InputMedia $inputMedia): InputMediaAnimation
{
$object = new static();
$object->type = $inputMedia->getType();
$object->media = $inputMedia->getMedia();
$object->thumb = $inputMedia->getThumb();
$object->caption = $inputMedia->getCaption();
$object->parse_mode = $inputMedia->getParseMode();
$object->caption_entities = $inputMedia->getCaptionEntities();
$object->width = $inputMedia->getWidth();
$object->height = $inputMedia->getHeight();
$object->duration = $inputMedia->getDuration();
$object->has_spoiler = $inputMedia->hasSpoiler();
return $object;
}
}

View file

@ -4,66 +4,24 @@
namespace TgBotLib\Objects\InputMedia;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
class InputMediaAudio implements ObjectTypeInterface
class InputMediaAudio extends InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $media;
/**
* @var string|null
*/
private $thumb;
/**
* @var string|null
*/
private $caption;
/**
* @var string|null
*/
private $parse_mode;
private string $media;
private ?string $thumb;
private ?string $caption;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var int|null
*/
private $duration;
/**
* @var string|null
*/
private $performer;
/**
* @var string|null
*/
private $title;
/**
* Type of the result, must be audio
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
private ?array $caption_entities;
private ?int $duration;
private ?string $performer;
private ?string $title;
/**
* File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP
@ -106,9 +64,9 @@
/**
* Optional. Mode for parsing entities in the audio caption.
*
* @return string|null
* @return ParseMode|null
*/
public function getParseMode(): ?string
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
@ -154,25 +112,17 @@
}
/**
* Returns an array representation of the object
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type,
'type' => $this->type?->value,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'caption_entities' => is_array($this->caption_entities) ? array_map(function ($item) {
if($item instanceof ObjectTypeInterface)
{
return $item->toArray();
}
return $item;
}, $this->caption_entities) : null,
'parse_mode' => $this->parse_mode->value,
'caption_entities' => is_null($this->caption_entities) ? null : array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities),
'duration' => $this->duration,
'performer' => $this->performer,
'title' => $this->title
@ -180,51 +130,26 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return InputMediaAudio
* @noinspection DuplicatedCode
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InputMediaAudio
{
if($data === null)
{
return null;
}
$object = new InputMediaAudio();
$object->type = $data['type'] ?? null;
$object->media = $data['media'] ?? null;
$object->thumb = $data['thumb'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(function ($item)
{
return MessageEntity::fromArray($item);
}, $data['caption_entities']) : null;
$object->parse_mode = isset($data['parse_mode']) ? ParseMode::tryFrom($data['parse_mode']) ?? null : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->duration = $data['duration'] ?? null;
$object->performer = $data['performer'] ?? null;
$object->title = $data['title'] ?? null;
return $object;
}
/**
* Constructs object from an InputMedia object
*
* @param InputMedia $inputMedia
* @return InputMediaAudio
*/
public static function fromInputMedia(InputMedia $inputMedia): InputMediaAudio
{
$object = new InputMediaAudio();
$object->type = $inputMedia->getType();
$object->media = $inputMedia->getMedia();
$object->thumb = $inputMedia->getThumb();
$object->caption = $inputMedia->getCaption();
$object->parse_mode = $inputMedia->getParseMode();
$object->caption_entities = $inputMedia->getCaptionEntities();
$object->duration = $inputMedia->getDuration();
$object->performer = $inputMedia->getPerformer();
$object->title = $inputMedia->getTitle();
return $object;
}
}

View file

@ -4,56 +4,22 @@
namespace TgBotLib\Objects\InputMedia;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
class InputMediaDocument implements ObjectTypeInterface
class InputMediaDocument extends InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $media;
/**
* @var string|null
*/
private $thumb;
/**
* @var string|null
*/
private $caption;
/**
* @var string|null
*/
private $parse_mode;
private string $media;
private ?string $thumb;
private ?string $caption;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var bool
*/
private $disable_content_type_detection;
/**
* Type of the result, must be document
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
private ?array $caption_entities;
private bool $disable_content_type_detection;
/**
* File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP
@ -97,9 +63,9 @@
* Optional. Mode for parsing entities in the document caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return string|null
* @return ParseMode|null
*/
public function getParseMode(): ?string
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
@ -126,71 +92,40 @@
}
/**
* Returns an array representation of the object.
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type,
'type' => $this->type->value,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'caption_entities' => is_array($this->caption_entities) ? array_map(function ($item) {
if($item instanceof ObjectTypeInterface)
{
return $item->toArray();
}
return $item;
}, $this->caption_entities) : null,
'parse_mode' => $this->parse_mode->value,
'caption_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities),
'disable_content_type_detection' => $this->disable_content_type_detection,
];
}
/**
* Constructs object from an array representation.
*
* @param array $data
* @return InputMediaDocument
* @noinspection DuplicatedCode
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InputMediaDocument
{
if($data === null)
{
return null;
}
$object = new InputMediaDocument();
$object->type = $data['type'] ?? null;
$object->media = $data['media'] ?? null;
$object->thumb = $data['thumb'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(function ($item)
{
return MessageEntity::fromArray($item);
}, $data['caption_entities']) : 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->disable_content_type_detection = $data['disable_content_type_detection'] ?? false;
return $object;
}
/**
* Constructs object from an InputMedia object.
*
* @param InputMedia $inputMedia
* @return InputMediaDocument
*/
public static function fromInputMedia(InputMedia $inputMedia): InputMediaDocument
{
$object = new InputMediaDocument();
$object->type = $inputMedia->getType();
$object->media = $inputMedia->getMedia();
$object->thumb = $inputMedia->getThumb();
$object->caption = $inputMedia->getCaption();
$object->parse_mode = $inputMedia->getParseMode();
$object->caption_entities = $inputMedia->getCaptionEntities();
$object->disable_content_type_detection = $inputMedia->isDisableContentTypeDetection();
return $object;
}
}

View file

@ -4,6 +4,7 @@
namespace TgBotLib\Objects\InputMedia;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
@ -12,7 +13,7 @@
{
private string $media;
private ?string $caption;
private ?string $parse_mode;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
@ -46,9 +47,9 @@
* Optional. Mode for parsing entities in the photo caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return string|null
* @return ParseMode|null
*/
public function getParseMode(): ?string
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
@ -81,10 +82,10 @@
public function toArray(): array
{
return [
'type' => $this->type,
'type' => $this->type->value,
'media' => $this->media,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'parse_mode' => $this->parse_mode->value,
'caption_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities),
'has_spoiler' => $this->has_spoiler,
];
@ -104,7 +105,7 @@
$object->type = $data['type'] ?? null;
$object->media = $data['media'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->parse_mode = isset($data['parse_mode']) ? ParseMode::tryFrom($data['parse_mode']) ?? null : null;
$object->has_spoiler = $data['has_spoiler'] ?? null;
$object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []);

View file

@ -4,76 +4,26 @@
namespace TgBotLib\Objects\InputMedia;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
class InputMediaVideo implements ObjectTypeInterface
class InputMediaVideo extends InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $media;
/**
* @var string|null
*/
private $thumb;
/**
* @var string|null
*/
private $caption;
/**
* @var string|null
*/
private $parse_mode;
private string $media;
private ?string $thumb;
private ?string $caption;
private ?ParseMode $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var int|null
*/
private $width;
/**
* @var int|null
*/
private $height;
/**
* @var int|null
*/
private $duration;
/**
* @var bool
*/
private $supports_streaming;
/**
* @var bool
*/
private $has_spoiler;
/**
* Type of the result, must be video
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
private ?array $caption_entities;
private ?int $width;
private ?int $height;
private ?string $duration;
private bool $supports_streaming;
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
@ -117,9 +67,9 @@
* Optional. Mode for parsing entities in the video caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return string|null
* @return ParseMode|null
*/
public function getParseMode(): ?string
public function getParseMode(): ?ParseMode
{
return $this->parse_mode;
}
@ -192,18 +142,12 @@
public function toArray(): array
{
return [
'type' => $this->type,
'type' => $this->type->value,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'caption_entities' => is_array($this->caption_entities) ? array_map(function ($item) {
if($item instanceof ObjectTypeInterface)
{
return $item->toArray();
}
return $item;
}, $this->caption_entities) : null,
'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,
@ -213,13 +157,9 @@
}
/**
* Constructs object from an array representation.
*
* @param array $data
* @return InputMediaVideo
* @noinspection DuplicatedCode
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): InputMediaVideo
{
$object = new InputMediaVideo();
@ -228,10 +168,7 @@
$object->thumb = $data['thumb'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(function ($item)
{
return MessageEntity::fromArray($item);
}, $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->width = $data['width'] ?? null;
$object->height = $data['height'] ?? null;
$object->duration = $data['duration'] ?? null;
@ -240,30 +177,4 @@
return $object;
}
/**
* Constructs object from an InputMedia object.
*
* @param InputMedia $inputMedia
* @return InputMediaVideo
* @noinspection DuplicatedCode
*/
public static function fromInputMedia(InputMedia $inputMedia): InputMediaVideo
{
$object = new InputMediaVideo();
$object->type = $inputMedia->getType();
$object->media = $inputMedia->getMedia();
$object->thumb = $inputMedia->getThumb();
$object->caption = $inputMedia->getCaption();
$object->parse_mode = $inputMedia->getParseMode();
$object->caption_entities = $inputMedia->getCaptionEntities();
$object->width = $inputMedia->getWidth();
$object->height = $inputMedia->getHeight();
$object->duration = $inputMedia->getDuration();
$object->supports_streaming = $inputMedia->isSupportsStreaming();
$object->has_spoiler = $inputMedia->hasSpoiler();
return $object;
}
}