Updated BackgroundFill

This commit is contained in:
netkas 2024-10-04 12:05:06 -04:00
parent 68bb270dc1
commit 5cddbc44d8

View file

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