Added PaidMedia, PaidMediaPhoto, PaidMediaPreview, PaidMediaVideo & PaidMediaType Enum

This commit is contained in:
netkas 2024-09-30 13:45:38 -04:00
parent b3b3302f00
commit a4bc96d00b
6 changed files with 238 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,10 @@
<?php
namespace TgBotLib\Enums\Types;
enum PaidMediaType : string
{
case PREVIEW = 'preview';
case PHOTO = 'photo';
case VIDEO = 'video';
}

View file

@ -0,0 +1,48 @@
<?php
namespace TgBotLib\Objects\Telegram;
use InvalidArgumentException;
use TgBotLib\Enums\Types\PaidMediaType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\PaidMedia\PaidMediaPhoto;
use TgBotLib\Objects\Telegram\PaidMedia\PaidMediaPreview;
use TgBotLib\Objects\Telegram\PaidMedia\PaidMediaVideo;
abstract class PaidMedia implements ObjectTypeInterface
{
protected PaidMediaType $type;
/**
* Type of the paid media
*
* @return PaidMediaType
*/
public function getType(): PaidMediaType
{
return $this->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),
};
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace TgBotLib\Objects\Telegram\PaidMedia;
use TgBotLib\Enums\Types\PaidMediaType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\PaidMedia;
use TgBotLib\Objects\Telegram\PhotoSize;
class PaidMediaPhoto extends PaidMedia implements ObjectTypeInterface
{
/**
* @var PhotoSize[]
*/
private array $photo;
/**
* Type of the paid media, always “photo”
*
* @return PaidMediaType
*/
public function getType(): PaidMediaType
{
return $this->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;
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace TgBotLib\Objects\Telegram\PaidMedia;
use TgBotLib\Enums\Types\PaidMediaType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\PaidMedia;
class PaidMediaPreview extends PaidMedia implements ObjectTypeInterface
{
private ?int $width;
private ?int $height;
private ?int $duration;
/**
* Optional. Media width as defined by the sender
*
* @return int|null
*/
public function getWidth(): ?int
{
return $this->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;
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace TgBotLib\Objects\Telegram\PaidMedia;
use TgBotLib\Enums\Types\PaidMediaType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\PaidMedia;
use TgBotLib\Objects\Telegram\Video;
class PaidMediaVideo extends PaidMedia implements ObjectTypeInterface
{
private Video $video;
/**
* The video
*
* @return Video
*/
public function getVideo(): Video
{
return $this->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;
}
}