feat: implement preview configuration
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Models\Tenant;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use KTXF\Json\JsonSerializableObject;
|
||||
|
||||
final class TenantPreviewVariants extends JsonSerializableObject
|
||||
{
|
||||
/** @var array<string, TenantPreviewVariant> */
|
||||
private array $entries;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->entries = self::defaults();
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
$defaults = self::defaults();
|
||||
$entries = [];
|
||||
foreach ($data as $name => $variant) {
|
||||
if (!is_string($name) || preg_match('/^[a-z][a-z0-9-]*$/', $name) !== 1) {
|
||||
throw new InvalidArgumentException('Invalid preview variant name');
|
||||
}
|
||||
if (!is_array($variant)) {
|
||||
throw new InvalidArgumentException('Preview variant configuration must be an object');
|
||||
}
|
||||
$entries[$name] = TenantPreviewVariant::fromArray($variant, $defaults[$name] ?? null);
|
||||
}
|
||||
$this->entries = $entries;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function variant(string $name): ?TenantPreviewVariant
|
||||
{
|
||||
return $this->entries[$name] ?? null;
|
||||
}
|
||||
|
||||
/** @return array<string, TenantPreviewVariant> */
|
||||
public function all(): array
|
||||
{
|
||||
return $this->entries;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn(TenantPreviewVariant $variant): array => $variant->toArray(),
|
||||
$this->entries,
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, TenantPreviewVariant> */
|
||||
private static function defaults(): array
|
||||
{
|
||||
return [
|
||||
'popover' => new TenantPreviewVariant(640, 440, 'image/webp', 80),
|
||||
'thumbnail' => new TenantPreviewVariant(256, 256, 'image/webp', 75),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user