From 649004a8444fa1218ee30a794d714eb2964c890c Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 1 Oct 2024 12:41:28 -0400 Subject: [PATCH] Add BackgroundFill objects --- .../Enums/Types/BackgroundFillType.php | 10 +++ .../Objects/Telegram/BackgroundFill.php | 49 +++++++++++++ .../BackgroundFillFreeformGradient.php | 49 +++++++++++++ .../BackgroundFill/BackgroundFillGradient.php | 69 +++++++++++++++++++ .../BackgroundFill/BackgroundFillSolid.php | 46 +++++++++++++ 5 files changed, 223 insertions(+) create mode 100644 src/TgBotLib/Enums/Types/BackgroundFillType.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundFill.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillFreeformGradient.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillGradient.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillSolid.php diff --git a/src/TgBotLib/Enums/Types/BackgroundFillType.php b/src/TgBotLib/Enums/Types/BackgroundFillType.php new file mode 100644 index 0000000..1a0a82f --- /dev/null +++ b/src/TgBotLib/Enums/Types/BackgroundFillType.php @@ -0,0 +1,10 @@ +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") + }; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillFreeformGradient.php b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillFreeformGradient.php new file mode 100644 index 0000000..86720e5 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillFreeformGradient.php @@ -0,0 +1,49 @@ +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; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillGradient.php b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillGradient.php new file mode 100644 index 0000000..1853e52 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillGradient.php @@ -0,0 +1,69 @@ +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; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillSolid.php b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillSolid.php new file mode 100644 index 0000000..00a7574 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundFill/BackgroundFillSolid.php @@ -0,0 +1,46 @@ +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; + } +} \ No newline at end of file