Refactor array handling and InputMedia class structure

This commit is contained in:
netkas 2024-10-03 21:31:04 -04:00
parent 3ed338d49a
commit 9dd11af87d
11 changed files with 56 additions and 339 deletions

View file

@ -273,7 +273,7 @@
$object->type = InlineQueryResultType::ARTICLE;
$object->id = $data['id'] ?? null;
$object->title = $data['title'] ?? null;
$object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null;
$object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content'] ?? null) : null;
$object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;
$object->url = $data['url'] ?? null;
$object->hide_url = $data['hide_url'] ?? null;

View file

@ -326,7 +326,7 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $messageEntity) => MessageEntity::fromArray($messageEntity), $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->performer = $data['performer'] ?? null;
$object->audio_duration = $data['audio_duration'] ?? null;
$object->reply_markup = ($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;

View file

@ -394,12 +394,12 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $entity) => MessageEntity::fromArray($entity), $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->document_url = $data['document_url'] ?? null;
$object->mime_type = $data['mime_type'] ?? null;
$object->description = $data['description'] ?? null;
$object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;
$object->input_message_content = isset($data['input_message_content']) ? InputVenueMessageContent::fromArray($data['input_message_content']) : null;
$object->input_message_content = isset($data['input_message_content']) ? InputVenueMessageContent::fromArray($data['input_message_content'] ?? []) : null;
$object->thumbnail_url = $data['thumbnail_url'] ?? null;
$object->thumbnail_width = $data['thumbnail_width'] ?? null;
$object->thumbnail_height = $data['thumbnail_height'] ?? null;

View file

@ -409,8 +409,8 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null;
$object->reply_markup = isset($data['reply_markup']) ? array_map(fn(array $items) => InlineKeyboardMarkup::fromArray($items), $data['reply_markup']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->reply_markup = isset($data['reply_markup']) ? array_map(fn(array $items) => InlineKeyboardMarkup::fromArray($items), $data['reply_markup'] ?? []) : null;
$object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null;
return $object;

View file

@ -356,7 +356,7 @@
$object->live_period = $data['live_period'] ?? null;
$object->heading = $data['heading'] ?? null;
$object->proximity_alert_radius = $data['proximity_alert_radius'] ?? null;
$object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;
$object->reply_markup = isset($data['reply_markup']) ? InlineKeyboardMarkup::fromArray($data['reply_markup'] ?? []) : null;
$object->input_message_content = isset($data['input_message_content']) ? InputMessageContent::fromArray($data['input_message_content']) : null;
$object->thumbnail_url = $data['thumbnail_url'] ?? null;
$object->thumbnail_width = $data['thumbnail_width'] ?? null;

View file

@ -341,7 +341,7 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->reply_markup = ($data['reply_markup'] ?? null) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;
$object->input_message_content = ($data['input_message_content'] ?? null) ? InputVenueMessageContent::fromArray($data['input_message_content']) : null;

View file

@ -321,7 +321,7 @@
$object->description = $data['description'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->reply_markup = ($data['reply_markup'] !== null) ? InlineKeyboardMarkup::fromArray($data['reply_markup']) : null;
$object->input_message_content = $data['input_message_content'] ?? null;

View file

@ -372,7 +372,7 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']) : null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []) : null;
$object->video_width = $data['video_width'] ?? null;
$object->video_height = $data['video_height'] ?? null;
$object->video_duration = $data['video_duration'] ?? null;

View file

@ -246,7 +246,7 @@
$object->title = $data['title'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities']);
$object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []);
$object->voice_duration = $data['voice_duration'] ?? null;
$object->reply_markup = InlineKeyboardMarkup::fromArray($data['reply_markup'] ?? []);
$object->input_message_content = InputMessageContent::fromArray($data['input_message_content'] ?? []);

View file

@ -4,79 +4,21 @@
namespace TgBotLib\Objects;
use InvalidArgumentException;
use TgBotLib\Enums\Types\InputMediaType;
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;
class InputMedia implements ObjectTypeInterface
abstract class 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;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var int|null
*/
private $width;
/**
* @var int|null
*/
private $height;
/**
* @var int|null
*/
private $duration;
/**
* @var string|null
*/
private $performer;
/**
* @var string|null
*/
private $title;
/**
* @var bool
*/
private $supports_streaming;
/**
* @var bool
*/
private $has_spoiler;
/**
* @var bool
*/
private $disable_content_type_detection;
protected $type;
/**
* Type of the result, can be photo, video, animation, audio or document
@ -89,199 +31,33 @@
}
/**
* 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
* @inheritDoc
*/
public function getMedia(): string
{
return $this->media;
}
public abstract function toArray(): array;
/**
* 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
* @inheritDoc
*/
public function getThumb(): ?string
public static function fromArray(?array $data): ?InputMedia
{
return $this->thumb;
}
/**
* Optional. Caption of the video to be sent, 0-1024 characters after entities parsing
*
* @return string|null
*/
public function getCaption(): ?string
{
return $this->caption;
}
/**
* Optional. Mode for parsing entities in the video caption.
*
* @see https://core.telegram.org/bots/api#formatting-options
* @return string|null
*/
public function getParseMode(): ?string
{
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. Video width
*
* @return int|null
*/
public function getWidth(): ?int
{
return $this->width;
}
/**
* Optional. Video height
*
* @return int|null
*/
public function getHeight(): ?int
{
return $this->height;
}
/**
* Optional. Video duration in seconds
*
* @return int|null
*/
public function getDuration(): ?int
{
return $this->duration;
}
/**
* Optional. Performer of the audio
*
* @return string|null
*/
public function getPerformer(): ?string
{
return $this->performer;
}
/**
* Optional. Title of the audio
*
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* Optional. Pass True if the uploaded video is suitable for streaming
*
* @return bool
*/
public function isSupportsStreaming(): bool
{
return $this->supports_streaming;
}
/**
* Optional. Pass True if the video needs to be covered with a spoiler animation
*
* @return bool
*/
public function hasSpoiler(): bool
{
return $this->has_spoiler;
}
/**
* Optional. Disables automatic server-side content type detection for files uploaded using multipart/form-data.
* Always True, if the document is sent as part of an album.
*
* @return bool
*/
public function isDisableContentTypeDetection(): bool
{
return $this->disable_content_type_detection;
}
/**
* Returns an array representation of the object.
*
* @return array
*/
public function toArray(): array
{
return [
'type' => $this->type,
'media' => $this->media,
'thumb' => $this->thumb,
'caption' => $this->caption,
'parse_mode' => $this->parse_mode,
'caption_entities' => is_array($this->caption_entities) ? array_map(function($entity) {
return $entity->toArray();
}, $this->caption_entities) : null,
'width' => $this->width,
'height' => $this->height,
'duration' => $this->duration,
'performer' => $this->performer,
'title' => $this->title,
'supports_streaming' => $this->supports_streaming,
'has_spoiler' => $this->has_spoiler,
'disable_content_type_detection' => $this->disable_content_type_detection,
];
}
/**
* Constructs a new object from an array representation.
*
* @param array $data
* @return InputMedia
*/
public static function fromArray(array $data): self
{
$object = new self();
$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']) && is_array($data['caption_entities']) ? array_map(function($entity)
if($data === null)
{
return MessageEntity::fromArray($entity);
}, $data['caption_entities']) : null;
$object->width = $data['width'] ?? null;
$object->height = $data['height'] ?? null;
$object->duration = $data['duration'] ?? null;
$object->performer = $data['performer'] ?? null;
$object->title = $data['title'] ?? null;
$object->supports_streaming = $data['supports_streaming'] ?? false;
$object->has_spoiler = $data['has_spoiler'] ?? false;
$object->disable_content_type_detection = $data['disable_content_type_detection'] ?? false;
return null;
}
return $object;
if(!isset($data['type']))
{
throw new InvalidArgumentException('type 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')
};
}
}

View file

@ -8,47 +8,16 @@
use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\MessageEntity;
class InputMediaPhoto implements ObjectTypeInterface
class InputMediaPhoto extends InputMedia implements ObjectTypeInterface
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $media;
/**
* @var string|null
*/
private $caption;
/**
* @var string|null
*/
private $parse_mode;
private string $media;
private ?string $caption;
private ?string $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $caption_entities;
/**
* @var bool
*/
private $has_spoiler;
/**
* Type of the result, must be photo
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
private ?array $caption_entities;
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
@ -116,56 +85,28 @@
'media' => $this->media,
'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,
'caption_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->caption_entities),
'has_spoiler' => $this->has_spoiler,
];
}
/**
* Constructs the object from an array
*
* @param array $data
* @return InputMediaPhoto
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InputMediaPhoto
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->type = $data['type'] ?? null;
$object->media = $data['media'] ?? null;
$object->caption = $data['caption'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->has_spoiler = $data['has_spoiler'] ?? null;
$object->caption_entities = isset($data['caption_entities']) ? array_map(function ($item)
{
return MessageEntity::fromArray($item);
}, $data['caption_entities']) : null;
return $object;
}
/**
* Constructs object from InputMedia
*
* @param InputMedia $inputMedia
* @return InputMediaPhoto
*/
public static function fromInputMedia(InputMedia $inputMedia): InputMediaPhoto
{
$object = new self();
$object->type = $inputMedia->getType();
$object->media = $inputMedia->getMedia();
$object->caption = $inputMedia->getCaption();
$object->parse_mode = $inputMedia->getParseMode();
$object->has_spoiler = $inputMedia->hasSpoiler();
$object->caption_entities = $inputMedia->getCaptionEntities();
$object->caption_entities = array_map(fn(array $items) => MessageEntity::fromArray($items), $data['caption_entities'] ?? []);
return $object;
}