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
{
case REGULAR = 'regular';
case MASK = 'mask';
case CUSTOM_EMOJI = 'custom_emoji';
}

View file

@ -7,25 +7,10 @@
class MaskPosition implements ObjectTypeInterface
{
/**
* @var string
*/
private $point;
/**
* @var float|int
*/
private $x_shift;
/**
* @var float|int
*/
private $y_shift;
/**
* @var float|int
*/
private $scale;
private string $point;
private int|float $x_shift;
private int|float $y_shift;
private int|float $scale;
/**
* 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
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
@ -86,15 +69,16 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return MaskPosition
* @inheritDoc
*/
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->x_shift = $data['x_shift'] ?? null;
$object->y_shift = $data['y_shift'] ?? null;

View file

@ -3,86 +3,28 @@
namespace TgBotLib\Objects\Stickers;
use TgBotLib\Enums\Types\StickerType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\File;
use TgBotLib\Objects\PhotoSize;
class Sticker implements ObjectTypeInterface
{
/**
* @var string
*/
private $file_id;
/**
* @var string
*/
private $file_unique_id;
/**
* @var string
*/
private $type;
/**
* @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;
private string $file_id;
private string $file_unique_id;
private StickerType $type;
private int $width;
private int $height;
private bool $is_animated;
private bool $is_video;
private ?PhotoSize $thumbnail;
private ?string $emoji;
private ?string $set_name;
private ?File $premium_animation;
private ?MaskPosition $mask_position;
private ?string $custom_emoji_id;
private bool $needs_repainting;
private ?int $file_size;
/**
* 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
* 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;
}
@ -240,16 +182,14 @@
}
/**
* Returns an array representation of the object.
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
return [
'file_id' => $this->file_id,
'file_unique_id' => $this->file_unique_id,
'type' => $this->type,
'type' => $this->type->value,
'width' => $this->width,
'height' => $this->height,
'is_animated' => $this->is_animated,
@ -266,18 +206,19 @@
}
/**
* Constructs object from an array representation.
*
* @param array $data
* @return Sticker
* @inheritDoc
*/
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_unique_id = $data['file_unique_id'];
$object->type = $data['type'];
$object->type = StickerType::tryFrom($data['type']);
$object->width = $data['width'];
$object->height = $data['height'];
$object->is_animated = $data['is_animated'];

View file

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