tgbotlib/src/TgBotLib/Objects/BackgroundFill.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2024-10-01 12:41:28 -04:00
<?php
2024-10-04 12:05:06 -04:00
namespace TgBotLib\Objects;
use InvalidArgumentException;
use TgBotLib\Enums\Types\BackgroundFillType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\BackgroundFill\BackgroundFillFreeformGradient;
use TgBotLib\Objects\BackgroundFill\BackgroundFillGradient;
use TgBotLib\Objects\BackgroundFill\BackgroundFillSolid;
abstract class BackgroundFill implements ObjectTypeInterface
2024-10-01 12:41:28 -04:00
{
2024-10-04 12:05:06 -04:00
protected BackgroundFillType $type;
/**
* Type of the background fill
*
* @return BackgroundFillType
*/
public function getType(): BackgroundFillType
{
2024-10-04 12:05:06 -04:00
return $this->type;
}
2024-10-04 12:05:06 -04:00
/**
* @inheritDoc
*/
public abstract function toArray(): array;
2024-10-01 12:41:28 -04:00
2024-10-04 12:05:06 -04:00
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?BackgroundFill
2024-10-01 12:41:28 -04:00
{
2024-10-04 12:05:06 -04:00
if($data === null)
{
return null;
}
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")
};
}
}