src/Model/Search/AdvancedSearch.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Model\Search;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use PHPUnit\Util\Exception;
  7. class AdvancedSearch
  8. {
  9.     private Collection $searchCriterias;
  10.     private bool $isForEmailSending false;
  11.     private bool $isForSmsSending false;
  12.     private bool $isForDisplayArchived false;
  13.     private array $parameters;
  14.     public function __construct(
  15.         private string $entity,
  16.         array $parameters = []
  17.     )
  18.     {
  19.         $this->searchCriterias = new ArrayCollection();
  20.         $this->parameters $parameters;
  21.     }
  22.     public function getisForEmailSending(): bool
  23.     {
  24.         return $this->isForEmailSending;
  25.     }
  26.     public function setIsForEmailSending(bool $isForEmailSending): self
  27.     {
  28.         $this->isForEmailSending $isForEmailSending;
  29.         return $this;
  30.     }
  31.     public function getisForSmsSending(): bool
  32.     {
  33.         return $this->isForSmsSending;
  34.     }
  35.     public function setIsForSmsSending(bool $isForSmsSending): self
  36.     {
  37.         $this->isForSmsSending $isForSmsSending;
  38.         return $this;
  39.     }
  40.     public function getIsForDisplayArchived(): bool
  41.     {
  42.         return $this->isForDisplayArchived;
  43.     }
  44.     public function setIsForDisplayArchived(bool $isForDisplayArchived): self
  45.     {
  46.         $this->isForDisplayArchived $isForDisplayArchived;
  47.         return $this;
  48.     }
  49.     public function getEntityFormType(): string
  50.     {
  51.         return $this->entityFormType;
  52.     }
  53.     public function addSearchCriterium(SearchCriteria $searchCriteria): self
  54.     {
  55.         $searchCriteria->setAdvancedSearch($this);
  56.         $this->searchCriterias[] = $searchCriteria;
  57.         return $this;
  58.     }
  59.     public function getSearchCriteria(): ArrayCollection
  60.     {
  61.         return $this->searchCriterias;
  62.     }
  63.     public function removeSearchCriterium(SearchCriteria $searchCriteria): self
  64.     {
  65.         $this->searchCriterias->removeElement($searchCriteria);
  66.         return $this;
  67.     }
  68.     public function getParameters(): array
  69.     {
  70.         return $this->parameters;
  71.     }
  72.     public function setParameters(array $parameters): void
  73.     {
  74.         $this->parameters $parameters;
  75.     }
  76.     public function transformValue(string $entityNamestring $fieldNamemixed $value): mixed
  77.     {
  78.         foreach ($this->parameters['entities'] as $entityParameters) {
  79.             if (
  80.                 trim($entityParameters['value']) === trim($entityName) ||
  81.                 (isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
  82.             ) {
  83.                 foreach ($entityParameters['fields'] as $field) {
  84.                     if ($field['value'] === $fieldName) {
  85.                         if (isset($field['params'])) {
  86.                             $params $field['params'];
  87.                             return match ($params['type']) {
  88.                                 default => $value,
  89.                                 'number''entity' => (int)$value,
  90.                                 'date' => DateTime::createFromFormat($params['format'] ?? 'Y-m-d'$value),
  91.                                 'datetime' => DateTime::createFromFormat($params['format'] ?? 'Y-m-d H:i:s'$value),
  92.                             };
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         return null;
  99.     }
  100.     public function __serialize()
  101.     {
  102.         $datas = [
  103.             'entity' => $this->entity,
  104.             'isForEmailSending' => $this->getisForEmailSending(),
  105.             'isForSmsSending' => $this->getisForSmsSending(),
  106.             'criterias' => []
  107.         ];
  108.         /** @var SearchCriteria $criteria */
  109.         foreach ($this->getSearchCriteria() as $criteria) {
  110.             $datas['criterias'][] = [
  111.                 $criteria->getEntity(),
  112.                 $criteria->getField(),
  113.                 $criteria->getTerm(),
  114.             ];
  115.         }
  116.         return $datas;
  117.     }
  118.     public function __unserialize(array $data)
  119.     {
  120.         $searchParams json_decode(file_get_contents(__DIR__.'/../../../config/ffcb/search_params.json'), true);
  121.         if (isset($searchParams[strtolower($data['entity'])])) {
  122.             $searchParams $searchParams[strtolower($data['entity'])];
  123.         } else {
  124.             throw new Exception('L\'entité "'.strtolower($data['entity']).'" n\'a pas de configuration valide !');
  125.         }
  126.         $advancedSearch = new AdvancedSearch($data['entity'], $searchParams);
  127.         $advancedSearch
  128.             ->setIsForEmailSending($data['isForEmailSending'])
  129.             ->setIsForSmsSending($data['isForSmsSending'])
  130.         ;
  131.         foreach ($datas['criterias'] as $criteria) {
  132.             $criteria = new SearchCriteria();
  133.             $criteria
  134.                 ->setAdvancedSearch($advancedSearch)
  135.                 ->setEntity($criteria['entity'])
  136.                 ->setField($criteria['field'])
  137.                 ->setTerm($criteria['term'])
  138.             ;
  139.         }
  140.     }
  141. }