Updated Stickers

This commit is contained in:
netkas 2024-10-04 21:08:22 -04:00
parent f966d6809f
commit c546a44b78
4 changed files with 59 additions and 161 deletions

View file

@ -5,8 +5,6 @@
enum StickerType : string enum StickerType : string
{ {
case REGULAR = 'regular'; case REGULAR = 'regular';
case MASK = 'mask'; case MASK = 'mask';
case CUSTOM_EMOJI = 'custom_emoji'; case CUSTOM_EMOJI = 'custom_emoji';
} }

View file

@ -7,25 +7,10 @@
class MaskPosition implements ObjectTypeInterface class MaskPosition implements ObjectTypeInterface
{ {
/** private string $point;
* @var string private int|float $x_shift;
*/ private int|float $y_shift;
private $point; private int|float $scale;
/**
* @var float|int
*/
private $x_shift;
/**
* @var float|int
*/
private $y_shift;
/**
* @var float|int
*/
private $scale;
/** /**
* The part of the face relative to which the mask should be placed. One of “forehead”, “eyes”, “mouth”, or * The part of the face relative to which the mask should be placed. One of “forehead”, “eyes”, “mouth”, or
@ -71,9 +56,7 @@
} }
/** /**
* Returns an array representation of the object * @inheritDoc
*
* @return array
*/ */
public function toArray(): array public function toArray(): array
{ {
@ -86,15 +69,16 @@
} }
/** /**
* Constructs object from an array representation * @inheritDoc
*
* @param array $data
* @return MaskPosition
*/ */
public static function fromArray(array $data): self public static function fromArray(?array $data): ?MaskPosition
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->point = $data['point'] ?? null; $object->point = $data['point'] ?? null;
$object->x_shift = $data['x_shift'] ?? null; $object->x_shift = $data['x_shift'] ?? null;
$object->y_shift = $data['y_shift'] ?? null; $object->y_shift = $data['y_shift'] ?? null;

View file

@ -3,86 +3,28 @@
namespace TgBotLib\Objects\Stickers; namespace TgBotLib\Objects\Stickers;
use TgBotLib\Enums\Types\StickerType;
use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\File; use TgBotLib\Objects\File;
use TgBotLib\Objects\PhotoSize; use TgBotLib\Objects\PhotoSize;
class Sticker implements ObjectTypeInterface class Sticker implements ObjectTypeInterface
{ {
/** private string $file_id;
* @var string private string $file_unique_id;
*/ private StickerType $type;
private $file_id; private int $width;
private int $height;
/** private bool $is_animated;
* @var string private bool $is_video;
*/ private ?PhotoSize $thumbnail;
private $file_unique_id; private ?string $emoji;
private ?string $set_name;
/** private ?File $premium_animation;
* @var string private ?MaskPosition $mask_position;
*/ private ?string $custom_emoji_id;
private $type; private bool $needs_repainting;
private ?int $file_size;
/**
* @var int
*/
private $width;
/**
* @var int
*/
private $height;
/**
* @var bool
*/
private $is_animated;
/**
* @var bool
*/
private $is_video;
/**
* @var PhotoSize|null
*/
private $thumbnail;
/**
* @var string|null
*/
private $emoji;
/**
* @var string|null
*/
private $set_name;
/**
* @var File|null
*/
private $premium_animation;
/**
* @var MaskPosition|null
*/
private $mask_position;
/**
* @var string|null
*/
private $custom_emoji_id;
/**
* @var bool
*/
private $needs_repainting;
/**
* @var int|null
*/
private $file_size;
/** /**
* Identifier for this file, which can be used to download or reuse the file * Identifier for this file, which can be used to download or reuse the file
@ -109,9 +51,9 @@
* Type of the sticker, currently one of “regular”, “mask”, “custom_emoji”. The type of the sticker is * Type of the sticker, currently one of “regular”, “mask”, “custom_emoji”. The type of the sticker is
* independent of its format, which is determined by the fields is_animated and is_video. * independent of its format, which is determined by the fields is_animated and is_video.
* *
* @return string * @return StickerType
*/ */
public function getType(): string public function getType(): StickerType
{ {
return $this->type; return $this->type;
} }
@ -240,16 +182,14 @@
} }
/** /**
* Returns an array representation of the object. * @inheritDoc
*
* @return array
*/ */
public function toArray(): array public function toArray(): array
{ {
return [ return [
'file_id' => $this->file_id, 'file_id' => $this->file_id,
'file_unique_id' => $this->file_unique_id, 'file_unique_id' => $this->file_unique_id,
'type' => $this->type, 'type' => $this->type->value,
'width' => $this->width, 'width' => $this->width,
'height' => $this->height, 'height' => $this->height,
'is_animated' => $this->is_animated, 'is_animated' => $this->is_animated,
@ -266,18 +206,19 @@
} }
/** /**
* Constructs object from an array representation. * @inheritDoc
*
* @param array $data
* @return Sticker
*/ */
public static function fromArray(array $data): self public static function fromArray(?array $data): ?Sticker
{ {
$object = new static(); if($data === null)
{
return null;
}
$object = new static();
$object->file_id = $data['file_id']; $object->file_id = $data['file_id'];
$object->file_unique_id = $data['file_unique_id']; $object->file_unique_id = $data['file_unique_id'];
$object->type = $data['type']; $object->type = StickerType::tryFrom($data['type']);
$object->width = $data['width']; $object->width = $data['width'];
$object->height = $data['height']; $object->height = $data['height'];
$object->is_animated = $data['is_animated']; $object->is_animated = $data['is_animated'];

View file

@ -9,40 +9,16 @@
class StickerSet implements ObjectTypeInterface class StickerSet implements ObjectTypeInterface
{ {
/** private string $name;
* @var string private string $title;
*/ private StickerType $sticker_type;
private $name; private bool $is_animated;
private bool $is_video;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $sticker_type;
/**
* @var bool
*/
private $is_animated;
/**
* @var bool
*/
private $is_video;
/** /**
* @var Sticker[] * @var Sticker[]
*/ */
private $stickers; private array $stickers;
private ?PhotoSize $thumbnail;
/**
* @var PhotoSize|null
*/
private $thumbnail;
/** /**
* Sticker set name * Sticker set name
@ -67,10 +43,9 @@
/** /**
* Type of stickers in the set, currently one of “regular”, “mask”, “custom_emoji” * Type of stickers in the set, currently one of “regular”, “mask”, “custom_emoji”
* *
* @return string * @return StickerType
* @see StickerType
*/ */
public function getStickerType(): string public function getStickerType(): StickerType
{ {
return $this->sticker_type; return $this->sticker_type;
} }
@ -125,12 +100,10 @@
return [ return [
'name' => $this->name, 'name' => $this->name,
'title' => $this->title, 'title' => $this->title,
'sticker_type' => $this->sticker_type, 'sticker_type' => $this->sticker_type->value,
'is_animated' => $this->is_animated, 'is_animated' => $this->is_animated,
'is_video' => $this->is_video, 'is_video' => $this->is_video,
'stickers' => array_map(function (Sticker $sticker) { 'stickers' => is_null($this->stickers) ? null : array_map(fn(Sticker $sticker) => $sticker->toArray(), $this->stickers),
return $sticker->toArray();
}, $this->stickers),
'thumbnail' => ($this->thumbnail instanceof PhotoSize) ? $this->thumbnail->toArray() : null, 'thumbnail' => ($this->thumbnail instanceof PhotoSize) ? $this->thumbnail->toArray() : null,
]; ];
} }
@ -138,18 +111,20 @@
/** /**
* @inheritDoc * @inheritDoc
*/ */
public static function fromArray(array $data): ObjectTypeInterface public static function fromArray(?array $data): ?StickerSet
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->name = $data['name']; $object->name = $data['name'];
$object->title = $data['title']; $object->title = $data['title'];
$object->sticker_type = $data['sticker_type']; $object->sticker_type = StickerType::tryFrom($data['sticker_type']);
$object->is_animated = $data['is_animated']; $object->is_animated = $data['is_animated'];
$object->is_video = $data['is_video']; $object->is_video = $data['is_video'];
$object->stickers = array_map(function (array $sticker) { $object->stickers = isset($data['stickers']) ? array_map(fn(array $items) => Sticker::fromArray($items), $data['stickers']) : null;
return Sticker::fromArray($sticker);
}, $data['stickers']);
$object->thumbnail = ($data['thumbnail'] !== null) ? PhotoSize::fromArray($data['thumbnail']) : null; $object->thumbnail = ($data['thumbnail'] !== null) ? PhotoSize::fromArray($data['thumbnail']) : null;
return $object; return $object;