a0093c9547
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?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 [
|
|
'thumbnail' => new TenantPreviewVariant(128, 128, 'image/webp', 70),
|
|
'inline' => new TenantPreviewVariant(640, 640, 'image/webp', 80),
|
|
'fullscreen' => new TenantPreviewVariant(2560, 2560, 'image/webp', 90),
|
|
];
|
|
}
|
|
}
|