feat: implement preview configuration
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?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 TenantPreviewGeneration $generation;
|
||||
protected TenantPreviewVariants $variants;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->generation = new TenantPreviewGeneration();
|
||||
$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('generation', $data)) {
|
||||
if (!is_array($data['generation'])) {
|
||||
throw new InvalidArgumentException('Preview generation configuration must be an object');
|
||||
}
|
||||
$this->generation->jsonDeserialize($data['generation']);
|
||||
}
|
||||
|
||||
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 generation(): TenantPreviewGeneration
|
||||
{
|
||||
return $this->generation;
|
||||
}
|
||||
|
||||
public function variants(): TenantPreviewVariants
|
||||
{
|
||||
return $this->variants;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user