diff --git a/CHANGELOG.md b/CHANGELOG.md index 856e16b..5be6e6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added unit tests * Added `can_connect_to_business` & `has_main_web_app` to User object * Added LinkPreviewOptions object + * Added PaidMedia, PaidMediaPhoto, PaidMediaPreview, PaidMediaVideo & PaidMediaType Enum ### Changed * Refactored the way HTTP requests are handled & methods are called diff --git a/src/TgBotLib/Enums/Types/PaidMediaType.php b/src/TgBotLib/Enums/Types/PaidMediaType.php new file mode 100644 index 0000000..2630342 --- /dev/null +++ b/src/TgBotLib/Enums/Types/PaidMediaType.php @@ -0,0 +1,10 @@ +type; + } + + /** + * @inheritDoc + */ + public abstract function toArray(): array; + + /** + * @inheritDoc + */ + public static function fromArray(array $data): ObjectTypeInterface + { + if(!isset($data['type'])) + { + throw new InvalidArgumentException('Paid media type is required'); + } + + return match (PaidMediaType::tryFrom($data['type'])) + { + PaidMediaType::PHOTO => PaidMediaPhoto::fromArray($data), + PaidMediaType::VIDEO => PaidMediaVideo::fromArray($data), + PaidMediaType::PREVIEW => PaidMediaPreview::fromArray($data), + }; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPhoto.php b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPhoto.php new file mode 100644 index 0000000..21d5637 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPhoto.php @@ -0,0 +1,60 @@ +type; + } + + /** + * The photo + * + * @return PhotoSize[] + */ + public function getPhoto(): array + { + return $this->photo; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'photo' => array_map(fn(PhotoSize $photo) => $photo->toArray(), $this->photo), + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): PaidMediaPhoto + { + $object = new self(); + + $object->type = PaidMediaType::PHOTO; + $object->photo = array_map(fn(array $photo) => PhotoSize::fromArray($photo), $data['photo']); + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPreview.php b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPreview.php new file mode 100644 index 0000000..e8adcb5 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaPreview.php @@ -0,0 +1,72 @@ +width; + } + + /** + * Optional. Media height as defined by the sender + * + * @return int|null + */ + public function getHeight(): ?int + { + return $this->height; + } + + /** + * Optional. Duration of the media in seconds as defined by the sender + * + * @return int|null + */ + public function getDuration(): ?int + { + return $this->duration; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'width' => $this->width, + 'height' => $this->height, + 'duration' => $this->duration, + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): PaidMediaPreview + { + $object = new self(); + + $object->type = PaidMediaType::PREVIEW; + $object->width = $data['width'] ?? null; + $object->height = $data['height'] ?? null; + $object->duration = $data['duration'] ?? null; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaVideo.php b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaVideo.php new file mode 100644 index 0000000..ecf7c1d --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/PaidMedia/PaidMediaVideo.php @@ -0,0 +1,47 @@ +video; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'video' => $this->video->toArray(), + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): PaidMediaVideo + { + $object = new self(); + + $object->type = PaidMediaType::VIDEO; + $object->video = Video::fromArray($data['video']); + + return $object; + } +} \ No newline at end of file