Add BackgroundType objects

This commit is contained in:
netkas 2024-10-01 12:41:37 -04:00
parent 649004a844
commit c2ba0bf68e
6 changed files with 354 additions and 0 deletions

View file

@ -0,0 +1,11 @@
<?php
namespace TgBotLib\Enums\Types;
enum BackgroundType : string
{
case FILL = 'fill';
case WALLPAPER = 'wallpaper';
case PATTERN = 'pattern';
case CHAT_THEME = 'chat_theme';
}

View file

@ -0,0 +1,49 @@
<?php
namespace TgBotLib\Objects\Telegram;
use InvalidArgumentException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Objects\Telegram\BackgroundType\BackgroundTypeChatTheme;
use TgBotLib\Objects\Telegram\BackgroundType\BackgroundTypeFill;
use TgBotLib\Objects\Telegram\BackgroundType\BackgroundTypeWallpaper;
abstract class BackgroundType implements ObjectTypeInterface
{
protected type $type;
/**
* Type of the background
*
* @return type
*/
public function getType(): type
{
return $this->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")
};
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundType;
class BackgroundTypeChatTheme extends BackgroundType implements ObjectTypeInterface
{
private string $theme_name;
/**
* Name of the chat theme, which is usually an emoji
*
* @return string
*/
public function getThemeName(): string
{
return $this->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;
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill;
use TgBotLib\Objects\Telegram\BackgroundType;
class BackgroundTypeFill extends BackgroundType implements ObjectTypeInterface
{
private BackgroundFill $fill;
private int $dark_theme_dimming;
/**
* The background fill
*
* @return BackgroundFill
*/
public function getFill(): BackgroundFill
{
return $this->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;
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundFill;
use TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Objects\Telegram\Document;
class BackgroundTypePattern extends BackgroundType implements ObjectTypeInterface
{
private Document $document;
private BackgroundFill $fill;
private int $intensity;
private bool $is_inverted;
private bool $is_moving;
/**
* Document with the pattern
*
* @return Document
*/
public function getDocument(): Document
{
return $this->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;
}
}

View file

@ -0,0 +1,86 @@
<?php
namespace TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BackgroundType;
use TgBotLib\Objects\Telegram\Document;
class BackgroundTypeWallpaper extends BackgroundType implements ObjectTypeInterface
{
private Document $document;
private int $dark_theme_dimming;
private bool $is_blurred;
private bool $is_moving;
/**
* Document with the wallpaper
*
* @return Document
*/
public function getDocument(): Document
{
return $this->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;
}
}