From c2ba0bf68e87f87ff8a706c1ee27c48707db6e89 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 1 Oct 2024 12:41:37 -0400 Subject: [PATCH] Add BackgroundType objects --- src/TgBotLib/Enums/Types/BackgroundType.php | 11 ++ .../Objects/Telegram/BackgroundType.php | 49 +++++++++ .../BackgroundTypeChatTheme.php | 46 ++++++++ .../BackgroundType/BackgroundTypeFill.php | 60 +++++++++++ .../BackgroundType/BackgroundTypePattern.php | 102 ++++++++++++++++++ .../BackgroundTypeWallpaper.php | 86 +++++++++++++++ 6 files changed, 354 insertions(+) create mode 100644 src/TgBotLib/Enums/Types/BackgroundType.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundType.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeChatTheme.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeFill.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypePattern.php create mode 100644 src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeWallpaper.php diff --git a/src/TgBotLib/Enums/Types/BackgroundType.php b/src/TgBotLib/Enums/Types/BackgroundType.php new file mode 100644 index 0000000..e12846e --- /dev/null +++ b/src/TgBotLib/Enums/Types/BackgroundType.php @@ -0,0 +1,11 @@ +type; + } + + /** + * @inheritDoc + */ + public abstract function toArray(): array; + + /** + * @inheritDoc + */ + public static function fromArray(array $data): BackgroundType + { + if (!isset($data['type'])) + { + throw new InvalidArgumentException('BackgroundType expected type'); + } + + return match (type::tryFrom($data['type'])) + { + type::WALLPAPER => BackgroundTypeWallpaper::fromArray($data), + type::FILL => BackgroundTypeFill::fromArray($data), + type::CHAT_THEME => BackgroundTypeChatTheme::fromArray($data), + default => throw new InvalidArgumentException("Invalid BackgroundType Type") + }; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeChatTheme.php b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeChatTheme.php new file mode 100644 index 0000000..b9d2c6d --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeChatTheme.php @@ -0,0 +1,46 @@ +theme_name; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'theme_name' => $this->theme_name + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): BackgroundTypeChatTheme + { + $object = new self(); + + $object->type = type::CHAT_THEME; + $object->theme_name = $data['theme_name']; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeFill.php b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeFill.php new file mode 100644 index 0000000..d0aa3c1 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeFill.php @@ -0,0 +1,60 @@ +fill; + } + + /** + * Dimming of the background in dark themes, as a percentage; 0-100 + * + * @return int + */ + public function getDarkThemeDimming(): int + { + return $this->dark_theme_dimming; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'fill' => $this->fill->toArray(), + 'dark_theme_dimming' => $this->dark_theme_dimming + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): BackgroundTypeFill + { + $object = new self(); + + $object->type = type::FILL; + $object->fill = BackgroundFill::fromArray($data['fill']); + $object->dark_theme_dimming = $data['dark_theme_dimming']; + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypePattern.php b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypePattern.php new file mode 100644 index 0000000..58326a4 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypePattern.php @@ -0,0 +1,102 @@ +document; + } + + /** + * The background fill that is combined with the pattern + * + * @return BackgroundFill + */ + public function getFill(): BackgroundFill + { + return $this->fill; + } + + /** + * Intensity of the pattern when it is shown above the filled background; 0-100 + * + * @return int + */ + public function getIntensity(): int + { + return $this->intensity; + } + + /** + * Optional. True, if the background fill must be applied only to the pattern itself. + * All other pixels are black in this case. For dark themes only + * + * @return bool + */ + public function isInverted(): bool + { + return $this->is_inverted; + } + + /** + * Optional. True, if the background moves slightly when the device is tilted + * + * @return bool + */ + public function isMoving(): bool + { + return $this->is_moving; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'document' => $this->document->toArray(), + 'fill' => $this->fill, + 'intensity' => $this->intensity, + 'is_inverted' => $this->is_inverted, + 'is_moving' => $this->is_moving + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): BackgroundTypePattern + { + $object = new self(); + + $object->type = type::PATTERN; + $object->document = Document::fromArray($data['document']); + $object->fill = BackgroundFill::fromArray($data['fill']); + $object->intensity = (int)$data['intensity']; + $object->is_inverted = $data['is_inverted'] ?? false; + $object->is_moving = $data['is_moving'] ?? false; + + + return $object; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeWallpaper.php b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeWallpaper.php new file mode 100644 index 0000000..759ad40 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/BackgroundType/BackgroundTypeWallpaper.php @@ -0,0 +1,86 @@ +document; + } + + /** + * Dimming of the background in dark themes, as a percentage; 0-100 + * + * @return int + */ + public function getDarkThemeDimming(): int + { + return $this->dark_theme_dimming; + } + + /** + * Optional. True, if the wallpaper is downscaled to fit in a 450x450 square and then box-blurred with radius 12 + * + * @return bool + */ + public function isBlurred(): bool + { + return $this->is_blurred; + } + + /** + * Optional. True, if the background moves slightly when the device is tilted + * + * @return bool + */ + public function isMoving(): bool + { + return $this->is_moving; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'type' => $this->type->value, + 'document' => $this->document->toArray(), + 'dark_theme_dimming' => $this->dark_theme_dimming, + 'is_blurred' => $this->is_blurred, + 'is_moving' => $this->is_moving + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): BackgroundTypeWallpaper + { + $object = new self(); + + $object->type = type::WALLPAPER; + $object->document = Document::fromArray($data['document']); + $object->dark_theme_dimming = $data['dark_theme_dimming']; + $object->is_blurred = $data['is_blurred'] ?? false; + $object->is_moving = $data['is_moving'] ?? false; + + return $object; + } +} \ No newline at end of file