f39ad1804d
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Models\Tenant;
|
|
|
|
use InvalidArgumentException;
|
|
use KTXF\Json\JsonSerializableObject;
|
|
|
|
final class TenantPreview extends JsonSerializableObject
|
|
{
|
|
protected bool $enabled = true;
|
|
protected string $store = 'previews';
|
|
protected int $maxSourceSize = 26214400;
|
|
protected TenantPreviewVariants $variants;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->variants = new TenantPreviewVariants();
|
|
}
|
|
|
|
public function jsonDeserialize(array|string $data): static
|
|
{
|
|
if (is_string($data)) {
|
|
$data = json_decode($data, true, flags: JSON_THROW_ON_ERROR);
|
|
}
|
|
|
|
if (array_key_exists('enabled', $data)) {
|
|
if (!is_bool($data['enabled'])) {
|
|
throw new InvalidArgumentException('Preview enabled must be a boolean');
|
|
}
|
|
$this->enabled = $data['enabled'];
|
|
}
|
|
|
|
if (array_key_exists('store', $data)) {
|
|
if (!is_string($data['store']) || preg_match('/^[a-z][a-z0-9-]*$/', $data['store']) !== 1) {
|
|
throw new InvalidArgumentException('Preview store must be a logical system-store name');
|
|
}
|
|
$this->store = $data['store'];
|
|
}
|
|
|
|
if (array_key_exists('maxSourceSize', $data)) {
|
|
if (!is_int($data['maxSourceSize']) || $data['maxSourceSize'] < 1) {
|
|
throw new InvalidArgumentException('Preview maximum source size must be a positive integer');
|
|
}
|
|
$this->maxSourceSize = $data['maxSourceSize'];
|
|
}
|
|
|
|
if (array_key_exists('variants', $data)) {
|
|
if (!is_array($data['variants'])) {
|
|
throw new InvalidArgumentException('Preview variants configuration must be an object');
|
|
}
|
|
$this->variants->jsonDeserialize($data['variants']);
|
|
}
|
|
|
|
if ($this->enabled && $this->variants->all() === []) {
|
|
throw new InvalidArgumentException('At least one preview variant is required when previews are enabled');
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function enabled(): bool
|
|
{
|
|
return $this->enabled;
|
|
}
|
|
|
|
public function store(): string
|
|
{
|
|
return $this->store;
|
|
}
|
|
|
|
public function maxSourceSize(): int
|
|
{
|
|
return $this->maxSourceSize;
|
|
}
|
|
|
|
public function variants(): TenantPreviewVariants
|
|
{
|
|
return $this->variants;
|
|
}
|
|
}
|