tgbotlib/src/TgBotLib/Objects/BackgroundFill.php

49 lines
1.4 KiB
PHP
Raw Normal View History

2024-10-01 12:41:28 -04:00
<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2024-10-01 12:41:28 -04:00
use InvalidArgumentException;
use TgBotLib\Enums\Types\BackgroundFillType;
use TgBotLib\Interfaces\ObjectTypeInterface;
2024-10-02 00:18:12 -04:00
use TgBotLib\Objects\BackgroundFill\BackgroundFillFreeformGradient;
use TgBotLib\Objects\BackgroundFill\BackgroundFillGradient;
use TgBotLib\Objects\BackgroundFill\BackgroundFillSolid;
2024-10-01 12:41:28 -04:00
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")
};
}
}