<?php
namespace App\Model\Search;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Util\Exception;
class AdvancedSearch
{
private Collection $searchCriterias;
private bool $isForEmailSending = false;
private bool $isForSmsSending = false;
private bool $isForDisplayArchived = false;
private array $parameters;
public function __construct(
private string $entity,
array $parameters = []
)
{
$this->searchCriterias = new ArrayCollection();
$this->parameters = $parameters;
}
public function getisForEmailSending(): bool
{
return $this->isForEmailSending;
}
public function setIsForEmailSending(bool $isForEmailSending): self
{
$this->isForEmailSending = $isForEmailSending;
return $this;
}
public function getisForSmsSending(): bool
{
return $this->isForSmsSending;
}
public function setIsForSmsSending(bool $isForSmsSending): self
{
$this->isForSmsSending = $isForSmsSending;
return $this;
}
public function getIsForDisplayArchived(): bool
{
return $this->isForDisplayArchived;
}
public function setIsForDisplayArchived(bool $isForDisplayArchived): self
{
$this->isForDisplayArchived = $isForDisplayArchived;
return $this;
}
public function getEntityFormType(): string
{
return $this->entityFormType;
}
public function addSearchCriterium(SearchCriteria $searchCriteria): self
{
$searchCriteria->setAdvancedSearch($this);
$this->searchCriterias[] = $searchCriteria;
return $this;
}
public function getSearchCriteria(): ArrayCollection
{
return $this->searchCriterias;
}
public function removeSearchCriterium(SearchCriteria $searchCriteria): self
{
$this->searchCriterias->removeElement($searchCriteria);
return $this;
}
public function getParameters(): array
{
return $this->parameters;
}
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
public function transformValue(string $entityName, string $fieldName, mixed $value): mixed
{
foreach ($this->parameters['entities'] as $entityParameters) {
if (
trim($entityParameters['value']) === trim($entityName) ||
(isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
) {
foreach ($entityParameters['fields'] as $field) {
if ($field['value'] === $fieldName) {
if (isset($field['params'])) {
$params = $field['params'];
return match ($params['type']) {
default => $value,
'number', 'entity' => (int)$value,
'date' => DateTime::createFromFormat($params['format'] ?? 'Y-m-d', $value),
'datetime' => DateTime::createFromFormat($params['format'] ?? 'Y-m-d H:i:s', $value),
};
}
}
}
}
}
return null;
}
public function __serialize()
{
$datas = [
'entity' => $this->entity,
'isForEmailSending' => $this->getisForEmailSending(),
'isForSmsSending' => $this->getisForSmsSending(),
'criterias' => []
];
/** @var SearchCriteria $criteria */
foreach ($this->getSearchCriteria() as $criteria) {
$datas['criterias'][] = [
$criteria->getEntity(),
$criteria->getField(),
$criteria->getTerm(),
];
}
return $datas;
}
public function __unserialize(array $data)
{
$searchParams = json_decode(file_get_contents(__DIR__.'/../../../config/ffcb/search_params.json'), true);
if (isset($searchParams[strtolower($data['entity'])])) {
$searchParams = $searchParams[strtolower($data['entity'])];
} else {
throw new Exception('L\'entité "'.strtolower($data['entity']).'" n\'a pas de configuration valide !');
}
$advancedSearch = new AdvancedSearch($data['entity'], $searchParams);
$advancedSearch
->setIsForEmailSending($data['isForEmailSending'])
->setIsForSmsSending($data['isForSmsSending'])
;
foreach ($datas['criterias'] as $criteria) {
$criteria = new SearchCriteria();
$criteria
->setAdvancedSearch($advancedSearch)
->setEntity($criteria['entity'])
->setField($criteria['field'])
->setTerm($criteria['term'])
;
}
}
}