From 0145279516219503af22c334cf0b8e834004e85c Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 10 Oct 2024 12:15:33 -0400 Subject: [PATCH] Refactor media handling in InputMedia classes --- src/TgBotLib/Objects/InputMedia.php | 30 +++++++++++++++++++ .../InputMedia/InputMediaAnimation.php | 2 -- .../Objects/InputMedia/InputMediaAudio.php | 1 - .../Objects/InputMedia/InputMediaDocument.php | 2 -- .../Objects/InputMedia/InputMediaPhoto.php | 3 +- .../Objects/InputMedia/InputMediaVideo.php | 1 - 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/TgBotLib/Objects/InputMedia.php b/src/TgBotLib/Objects/InputMedia.php index 057cac8..fa3cc64 100644 --- a/src/TgBotLib/Objects/InputMedia.php +++ b/src/TgBotLib/Objects/InputMedia.php @@ -15,6 +15,7 @@ abstract class InputMedia implements ObjectTypeInterface { protected InputMediaType $type; + protected string $media; /** * Type of the result, can be photo, video, animation, audio or document @@ -26,6 +27,30 @@ 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://” to + * upload a new one using multipart/form-data under 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; + } + /** * @inheritDoc */ @@ -46,6 +71,11 @@ throw new InvalidArgumentException('type is not provided'); } + if(!isset($data['media'])) + { + throw new InvalidArgumentException('media is not provided'); + } + return match (InputMediaType::tryFrom($data['type'])) { InputMediaType::PHOTO => InputMediaPhoto::fromArray($data), diff --git a/src/TgBotLib/Objects/InputMedia/InputMediaAnimation.php b/src/TgBotLib/Objects/InputMedia/InputMediaAnimation.php index c1c67dd..a103fea 100644 --- a/src/TgBotLib/Objects/InputMedia/InputMediaAnimation.php +++ b/src/TgBotLib/Objects/InputMedia/InputMediaAnimation.php @@ -11,7 +11,6 @@ class InputMediaAnimation extends InputMedia implements ObjectTypeInterface { - private string $media; private ?string $thumb; private ?string $caption; private ?ParseMode $parse_mode; @@ -29,7 +28,6 @@ */ public function __construct() { - $this->media = (string)null; $this->thumb = null; $this->caption = null; $this->parse_mode = null; diff --git a/src/TgBotLib/Objects/InputMedia/InputMediaAudio.php b/src/TgBotLib/Objects/InputMedia/InputMediaAudio.php index dfa3acf..cd8a083 100644 --- a/src/TgBotLib/Objects/InputMedia/InputMediaAudio.php +++ b/src/TgBotLib/Objects/InputMedia/InputMediaAudio.php @@ -10,7 +10,6 @@ class InputMediaAudio extends InputMedia implements ObjectTypeInterface { - private string $media; private ?string $thumb; private ?string $caption; private ?ParseMode $parse_mode; diff --git a/src/TgBotLib/Objects/InputMedia/InputMediaDocument.php b/src/TgBotLib/Objects/InputMedia/InputMediaDocument.php index 49ca4fc..f29ac8c 100644 --- a/src/TgBotLib/Objects/InputMedia/InputMediaDocument.php +++ b/src/TgBotLib/Objects/InputMedia/InputMediaDocument.php @@ -10,7 +10,6 @@ class InputMediaDocument extends InputMedia implements ObjectTypeInterface { - private string $media; private ?string $thumb; private ?string $caption; private ?ParseMode $parse_mode; @@ -25,7 +24,6 @@ */ public function __construct() { - $this->media = (string)null; $this->thumb = null; $this->caption = null; $this->parse_mode = null; diff --git a/src/TgBotLib/Objects/InputMedia/InputMediaPhoto.php b/src/TgBotLib/Objects/InputMedia/InputMediaPhoto.php index 935d4b4..8d1b033 100644 --- a/src/TgBotLib/Objects/InputMedia/InputMediaPhoto.php +++ b/src/TgBotLib/Objects/InputMedia/InputMediaPhoto.php @@ -3,6 +3,7 @@ namespace TgBotLib\Objects\InputMedia; + use TgBotLib\Enums\Types\InputMediaType; use TgBotLib\Enums\Types\ParseMode; use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Objects\InputMedia; @@ -10,7 +11,6 @@ class InputMediaPhoto extends InputMedia implements ObjectTypeInterface { - private string $media; private ?string $caption; private ?ParseMode $parse_mode; /** @@ -24,6 +24,7 @@ */ public function __construct() { + $this->type = InputMediaType::PHOTO; $this->media = ''; $this->caption = null; $this->parse_mode = null; diff --git a/src/TgBotLib/Objects/InputMedia/InputMediaVideo.php b/src/TgBotLib/Objects/InputMedia/InputMediaVideo.php index 6ca8b0a..fa32f3f 100644 --- a/src/TgBotLib/Objects/InputMedia/InputMediaVideo.php +++ b/src/TgBotLib/Objects/InputMedia/InputMediaVideo.php @@ -10,7 +10,6 @@ class InputMediaVideo extends InputMedia implements ObjectTypeInterface { - private string $media; private ?string $thumb; private ?string $caption; private ?ParseMode $parse_mode;