<?php
namespace App\Model\Query\ConditionQuery;
use JsonSerializable;
/**
* Represents the configuration of a single type of filter.
*
* @see https://coda.io/d/_diyKeRjYYS-/Flexible-Messaging-Prototype_suNzV#_luVi2
*/
final class ConditionSchema implements JsonSerializable
{
use ConditionFilterTrait;
/**
* @var ConditionConfig|null
*/
private ?ConditionConfig $conditionConfig = null;
/**
* @var string|null
*/
private ?string $category = null;
/**
* @var string|null
*/
private ?string $name = null;
/**
* @var AbstractType|null
*/
private ?AbstractType $type = null;
/**
* @param string|null $filter
* @param string|null $name
* @param AbstractType|null $type
*/
public function __construct(
?string $filter = null,
?string $name = null,
?AbstractType $type = null
)
{
$this
->setFilter($filter)
->setName($name)
->setType($type);
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string|null $name
* @return $this
*/
public function setName(?string $name): self
{
$this->name = $name ?: null;
return $this;
}
/**
* @return string|null
*/
public function getCategory(): ?string
{
return $this->category;
}
/**
* @param string|null $category
* @return $this
*/
public function setCategory(?string $category): self
{
$this->category = $category ?: null;
return $this;
}
/**
* @return AbstractType|null
*/
public function getType(): ?AbstractType
{
return $this->type;
}
/**
* @param AbstractType|null $type
* @return $this
*/
public function setType(?AbstractType $type): self
{
$this->type = $type;
return $this;
}
/**
* {@inheritDoc}
*/
public function jsonSerialize(): array
{
return [
'filter' => $this->getFilter(),
'category' => $this->getCategory(),
'name' => $this->getName(),
'type' => $this->getType()->jsonSerialize(),
];
}
/**
* @param array $serialized
* @return $this
*/
public function jsonUnserialize(array $serialized): self
{
return $this
->setFilter($serialized['filter'] ?? null)
->setCategory($serialized['category'] ?? null)
->setName($serialized['name'] ?? null)
->setType(
$serialized['type'] ? AbstractType::factory(
$serialized['type'],
) : null,
);
}
/**
* @param array $serialized
* @return ConditionSchema
*/
static public function factory(array $serialized): ConditionSchema
{
if ( ! isset($serialized['filter']) || ! is_string($serialized['filter'])) {
throw new \Exception('Invalid filter');
}
if ( ! isset($serialized['name']) || ! is_string($serialized['name'])) {
throw new \Exception('Invalid name');
}
if ( ! isset($serialized['type'])) {
throw new \Exception('Invalid type');
}
return (new ConditionSchema())->jsonUnserialize($serialized);
}
/**
* @return void
*/
public function __clone()
{
$this->conditionConfig = null;
}
/**
* @return ConditionConfig|null
*/
public function getConditionConfig(): ?ConditionConfig
{
return $this->conditionConfig;
}
/**
* @param ConditionConfig|null $conditionConfig
* @return $this
*/
public function setConditionConfig(?ConditionConfig $conditionConfig): self
{
if ($conditionConfig && $this->conditionConfig) {
throw new \Exception('Schema has already been assigned to a configuration.');
}
$this->conditionConfig = $conditionConfig;
return $this;
}
}