Add BackgroundFill objects

This commit is contained in:
netkas 2024-10-01 12:41:28 -04:00
parent eb15ca3ee9
commit 649004a844
5 changed files with 223 additions and 0 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace TgBotLib\Enums\Types;
enum BackgroundFillType : string
{
case SOLID = 'solid';
case GRADIENT = 'gradient';
case FREEFORM_GRADIENT = 'freeform_gradient';
}

View file

@ -0,0 +1,49 @@
<?php
namespace TgBotLib\Objects\Telegram;
use InvalidArgumentException;
use TgBotLib\Enums\Types\BackgroundFillType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill\BackgroundFillFreeformGradient;
use TgBotLib\Objects\Telegram\BackgroundFill\BackgroundFillGradient;
use TgBotLib\Objects\Telegram\BackgroundFill\BackgroundFillSolid;
abstract class BackgroundFill implements ObjectTypeInterface
{
protected BackgroundFillType $type;
/**
* Type of the background fill
*
* @return BackgroundFillType
*/
public function getType(): BackgroundFillType
{
return $this->type;
}
/**
* @inheritDoc
*/
public abstract function toArray(): array;
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
{
if(!isset($data['type']))
{
throw new InvalidArgumentException('BackgroundFill expected type');
}
return match(BackgroundFillType::tryFrom($data['type']))
{
BackgroundFillType::SOLID => BackgroundFillSolid::fromArray($data),
BackgroundFillType::GRADIENT => BackgroundFillGradient::fromArray($data),
BackgroundFillType::FREEFORM_GRADIENT => BackgroundFillFreeformGradient::fromArray($data),
default => throw new InvalidArgumentException("Invalid BackgroundFill Type")
};
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundFill;
use TgBotLib\Enums\Types\BackgroundFillType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill;
class BackgroundFillFreeformGradient extends BackgroundFill implements ObjectTypeInterface
{
/**
* @var int[]
*/
private array $colors;
/**
* A list of the 3 or 4 base colors that are used to generate the freeform gradient in the RGB24 format
*
* @return array
*/
public function getColors(): array
{
return $this->toArray();
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type->value,
'colors' => $this->colors
];
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
{
$object = new self();
$object->type = BackgroundFillType::FREEFORM_GRADIENT;
$object->colors = $data['colors'];
return $object;
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundFill;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill;
class BackgroundFillGradient extends BackgroundFill implements ObjectTypeInterface
{
private int $top_color;
private int $bottom_color;
private int $rotation_angle;
/**
* Top color of the gradient in the RGB24 format
*
* @return int
*/
public function getTopColor(): int
{
return $this->top_color;
}
/**
* Bottom color of the gradient in the RGB24 format
*
* @return int
*/
public function getBottomColor(): int
{
return $this->bottom_color;
}
/**
* Clockwise rotation angle of the background fill in degrees; 0-359
*
* @return int
*/
public function getRotationAngle(): int
{
return $this->rotation_angle;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type->value,
'top_color' => $this->top_color,
'bottom_color' => $this->bottom_color,
'rotation_angle' => $this->rotation_angle
];
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFillGradient
{
$object = new self();
$object->type = $data['type'] ?? null;
$object->top_color = $data['top_color'] ?? 0;
$object->bottom_color = $data['bottom_color'] ?? 0;
$object->rotation_angle = $data['rotation_angle'] ?? 0;
return $object;
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundFill;
use TgBotLib\Enums\Types\BackgroundFillType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill;
class BackgroundFillSolid extends BackgroundFill implements ObjectTypeInterface
{
private int $color;
/**
* The color of the background fill in the RGB24 format
*
* @return int
*/
public function getColor(): int
{
return $this->color;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'type' => $this->type->value,
'color' => $this->color
];
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
{
$object = new self();
$object->type = BackgroundFillType::SOLID;
$object->color = $data['color'];
return $object;
}
}