src/Search/SearchConverter.php line 215

Open in your IDE?
  1. <?php
  2. namespace App\Search;
  3. use App\Entity\User;
  4. use App\Event\FilterBehaviourEvent;
  5. use App\Form\Type\Frontend\VehicleSearchType;
  6. use App\Repository\CategoryRepository;
  7. use App\Repository\EnergyRepository;
  8. use App\Repository\SaleEventRepository;
  9. use App\Repository\SaleRepository;
  10. use App\Repository\TransmissionRepository;
  11. use App\Repository\VehicleRepository;
  12. use Gedmo\Sluggable\Util\Urlizer;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * Provides ways to convert the data from a search or saved search.
  19.  */
  20. class SearchConverter
  21. {
  22.     public const SEPARATOR ', ';
  23.     public const CO2 '< %s g/km CO2';
  24.     public function __construct(private readonly SessionInterface $session, private readonly EventDispatcherInterface $dispatcher, private readonly TokenStorageInterface $tokenStorage, private readonly TranslatorInterface $translator, private readonly EnergyRepository $energyRepository, private readonly SaleRepository $saleRepository, private readonly SaleEventRepository $saleEventRepository, private readonly CategoryRepository $categoryRepository, private readonly TransmissionRepository $transmissionRepository, private readonly VehicleRepository $vehicleRepository)
  25.     {
  26.     }
  27.     public function convertToDBCriteria($search$formType VehicleSearchType::FORM_DEFAULT)
  28.     {
  29.         $criteria = [];
  30.         $sessionSale $this->session->get('sale'null);
  31.         $sessionEvent $this->session->get('event'null);
  32.         if (isset($search['sale'])) {
  33.             if ('all' === $search['sale']) {
  34.                 $criteria['sale'] = null;
  35.                 $this->session->remove('sale');
  36.             } elseif (null !== $sale $this->saleRepository->findOneById($search['sale'])) {
  37.                 $criteria['sale'] = $sale;
  38.                 $this->session->set('sale'$search['sale']);
  39.             }
  40.         } elseif (
  41.             !empty($sessionSale)
  42.             && (null !== $sale $this->saleRepository->findOneById($sessionSale))
  43.         ) {
  44.             $criteria['sale'] = $sale;
  45.         }
  46.         if (isset($search['event'])) {
  47.             if ('all' === $search['event']) {
  48.                 $criteria['event'] = null;
  49.                 $this->session->remove('event');
  50.             } elseif (null !== $event $this->saleEventRepository->findOneById($search['event'])) {
  51.                 $criteria['event'] = $event->getId();
  52.                 $this->session->set('event'$event->getId());
  53.             }
  54.         } elseif (
  55.             !empty($sessionEvent)
  56.             && (null !== $event $this->saleEventRepository->findOneById($sessionEvent))
  57.         ) {
  58.             $criteria['event'] = $event->getId();
  59.         }
  60.         if (isset($sale) && null !== $sale) {
  61.             $this->dispatcher->dispatch(new FilterBehaviourEvent($this->getUser(), $sale), FilterBehaviourEvent::SALE_BEHAVIOUR);
  62.         }
  63.         if (isset($search['sort'])) {
  64.             $criteria['sortedby'] = VehicleSearchType::FORM_SALE_RESULTS === $formType ?
  65.                 str_replace('startingPrice''finalAmount'$search['sort']) : $search['sort'];
  66.         }
  67.         if (isset($search['category'])) {
  68.             if (is_string($search['category'])) {
  69.                 $criteria['category'] = explode(','$search['category']);
  70.             } else {
  71.                 $criteria['category'] = $search['category'];
  72.             }
  73.         }
  74.         $keyPrice VehicleSearchType::FORM_SALE_RESULTS === $formType 'finalAmount' 'price';
  75.         if (isset($search['budget']['min'])) {
  76.             $search['budgetMin'] = $search['budget']['min'];
  77.         }
  78.         if (isset($search['budget']['max'])) {
  79.             $search['budgetMax'] = $search['budget']['max'];
  80.         }
  81.         if (isset($search['budgetMin'])) {
  82.             $search['budgetMin'] = floatval($search['budgetMin']);
  83.             $criteria[$keyPrice]['min'] = $search['budgetMin'];
  84.             $criteria[$keyPrice]['max'] = PHP_INT_MAX;
  85.         }
  86.         if (isset($search['budgetMax'])) {
  87.             $search['budgetMax'] = floatval($search['budgetMax']);
  88.             $criteria[$keyPrice]['max'] = $search['budgetMax'];
  89.             if (!isset($criteria[$keyPrice]['min'])) {
  90.                 $criteria[$keyPrice]['min'] = 0;
  91.             }
  92.         }
  93.         if (isset($search['maker'])) {
  94.             $criteria['makerSlug'] = array_map(fn ($maker) => Urlizer::urlize($maker), explode(',', (string) $search['maker']));
  95.         }
  96.         if (isset($search['cityLocation'])) {
  97.             foreach (explode(',', (string) $search['cityLocation']) as $cityLocation) {
  98.                 if (null !== $v $this->vehicleRepository->findOneByCityLocation(trim($cityLocation))) {
  99.                     $criteria['cityLocation'][] = $v->getCityLocation();
  100.                 } elseif (null !== $v $this->vehicleRepository->findOneByCityLocation(Urlizer::urlize($cityLocation))) {
  101.                     $criteria['cityLocation'][] = $v->getCityLocation();
  102.                 }
  103.             }
  104.         }
  105.         if (isset($search['modelGroup'])) {
  106.             $criteria['modelGroupSlug'] = array_map(fn ($group) => Urlizer::urlize($group), explode(',', (string) $search['modelGroup']));
  107.         }
  108.         if (isset($search['segmentSize'])) {
  109.             $criteria['segmentSizeSlug'] = array_map(fn ($segmenttaille) => Urlizer::urlize($segmenttaille), explode(',', (string) $search['segmentSize']));
  110.         }
  111.         if (isset($search['energy'])) {
  112.             foreach (explode(',', (string) $search['energy']) as $energy) {
  113.                 if (null !== $e $this->energyRepository->findOneByName(trim($energy))) {
  114.                     $criteria['energy'][] = $e->getCode();
  115.                 }
  116.             }
  117.         }
  118.         if (isset($search['segmentTransmission'])) {
  119.             $criteria['segmentTransmission'] = is_array($search['segmentTransmission']) ? $search['segmentTransmission'] : explode(',', (string) $search['segmentTransmission']);
  120.         }
  121.         if (isset($search['fourwheeldrive']) && $search['fourwheeldrive']) {
  122.             $criteria['driveWheel'] = '4X4';
  123.         }
  124.         if (!empty($search['options'])) {
  125.             $criteria['options'] = array_map(fn ($option) => Urlizer::urlize($option), explode(',', (string) $search['options']));
  126.         }
  127.         if (isset($search['kilometersMin'])) {
  128.             $criteria['kilometers']['min'] = $search['kilometersMin'];
  129.         }
  130.         if (isset($search['kilometersMax'])) {
  131.             $criteria['kilometers']['max'] = $search['kilometersMax'];
  132.         }
  133.         if (isset($search['kilometers']['min'])) {
  134.             $search['kilometersMin'] = $search['kilometers']['min'];
  135.         }
  136.         if (isset($search['kilometers']['max'])) {
  137.             $search['kilometersMax'] = $search['kilometers']['max'];
  138.         }
  139.         if (isset($search['yearMin'])) {
  140.             $criteria['millesime']['min'] = $search['yearMin'];
  141.         }
  142.         if (isset($search['yearMax'])) {
  143.             $criteria['millesime']['max'] = $search['yearMax'];
  144.         }
  145.         if (isset($search['broken'])) {
  146.             $criteria['broken'] = $search['broken'];
  147.         }
  148.         if (isset($search['highlight'])) {
  149.             $criteria['highlight'] = $search['highlight'];
  150.         }
  151.         if (isset($search['seatCount'])) {
  152.             foreach (explode(',', (string) $search['seatCount']) as $key => $item) {
  153.                 [$criteria['seatCount'][$key]['min'], $criteria['seatCount'][$key]['max']] = explode('-'trim($item));
  154.             }
  155.         }
  156.         if (isset($search['doorCount'])) {
  157.             $criteria['doorCount'] = $search['doorCount'];
  158.         }
  159.         if (isset($search['co2'])) {
  160.             $criteria['co2'][] = [
  161.                 'min' => '0',
  162.                 'max' => $search['co2'],
  163.             ];
  164.         }
  165.         if (isset($search['estimated'])) {
  166.             $criteria['estimated'] = true;
  167.         }
  168.         if (isset($search['critair'])) {
  169.             $criteria['critair'] = $search['critair'];
  170.         }
  171.         return $criteria;
  172.     }
  173.     public function convertToDisplayableValues($search)
  174.     {
  175.         $values = [];
  176.         $sessionSale $this->session->get('sale'null);
  177.         $sessionEvent $this->session->get('event'null);
  178.         if (isset($search['sale'])) {
  179.             if (null !== $sale $this->saleRepository->findOneById($search['sale'])) {
  180.                 $values['sale'] = $sale->getDisplayedName();
  181.             }
  182.         } elseif (
  183.             !empty($sessionSale)
  184.             && (null !== $sale $this->saleRepository->findOneById($sessionSale))
  185.         ) {
  186.             $values['sale'] = $sale->getDisplayedName();
  187.         }
  188.         if (isset($search['event'])) {
  189.             if (null !== $event $this->saleEventRepository->findOneById($search['event'])) {
  190.                 $values['sale'] = $event->getSale()->getRoom()->getName();
  191.                 $values['event'] = sprintf('%s'substr((string) $event->getStartTime(), 0strrpos((string) $event->getStartTime(), ':')));
  192.             }
  193.         } elseif (
  194.             !empty($sessionEvent)
  195.             && (null !== $event $this->saleEventRepository->findOneById($sessionEvent))
  196.         ) {
  197.             $values['sale'] = $event->getSale()->getRoom()->getName();
  198.             $values['event'] = sprintf('%s'substr((string) $event->getStartTime(), 0strrpos((string) $event->getStartTime(), ':')));
  199.         }
  200.         if (isset($search['category'])) {
  201.             $categories is_array($search['category']) ? $search['category'] : explode(',', (string) $search['category']);
  202.             foreach ($categories as $category) {
  203.                 if (null !== $c $this->categoryRepository->findOneById($category)) {
  204.                     $values['category'][] = (object) [
  205.                         'display' => $c->getName(),
  206.                         'actual' => $c->getId(),
  207.                     ];
  208.                 }
  209.             }
  210.         }
  211.         if (isset($search['budget']['min'])) {
  212.             $search['budgetMin'] = $search['budget']['min'];
  213.         }
  214.         if (isset($search['budget']['max'])) {
  215.             $search['budgetMax'] = $search['budget']['max'];
  216.         }
  217.         if (isset($search['budgetMin'])) {
  218.             if (isset($search['budgetMax'])) {
  219.                 $values['budget'] = sprintf('%s ~ %s €'$search['budgetMin'], $search['budgetMax']);
  220.             } else {
  221.                 $values['budget'] = sprintf('> %s €'$search['budgetMin']);
  222.             }
  223.         } elseif (isset($search['budgetMax'])) {
  224.             $values['budget'] = sprintf('< %s €'$search['budgetMax']);
  225.         }
  226.         if (isset($search['maker'])) {
  227.             $values['maker'] = explode(',', (string) $search['maker']);
  228.         }
  229.         if (isset($search['cityLocation'])) {
  230.             $values['cityLocation'] = explode(',', (string) $search['cityLocation']);
  231.         }
  232.         if (isset($search['modelGroup'])) {
  233.             $values['modelGroup'] = explode(',', (string) $search['modelGroup']);
  234.         }
  235.         if (isset($search['segmentSize'])) {
  236.             $values['segmentSize'] = explode(',', (string) $search['segmentSize']);
  237.         }
  238.         if (isset($search['fourwheeldrive']) && $search['fourwheeldrive']) {
  239.             $values['driveWheel'] = '4X4';
  240.         }
  241.         if (isset($search['energy'])) {
  242.             $energies is_array($search['energy']) ? $search['energy'] : explode(',', (string) $search['energy']);
  243.             foreach ($energies as $energy) {
  244.                 if (null !== $e $this->energyRepository->findOneByName(trim((string) $energy))) {
  245.                     $values['energy'][] = (object) [
  246.                         'display' => $e->getName(),
  247.                         'actual' => $e->getId(),
  248.                     ];
  249.                 }
  250.             }
  251.         }
  252.         if (isset($search['segmentTransmission'])) {
  253.             $transmissions is_array($search['segmentTransmission']) ? $search['segmentTransmission'] : explode(',', (string) $search['segmentTransmission']);
  254.             foreach ($transmissions as $transmission) {
  255.                 if (null != ($t $this->transmissionRepository->findOneById($transmission))) {
  256.                     $values['segmentTransmission'][] = (object) [
  257.                         'display' => $t->getName(),
  258.                         'actual' => $t->getId(),
  259.                     ];
  260.                 }
  261.             }
  262.         }
  263.         if (!empty($search['options'])) {
  264.             $values['options'] = explode(',', (string) $search['options']);
  265.         }
  266.         if (isset($search['kilometers']['min'])) {
  267.             $search['kilometersMin'] = $search['kilometers']['min'];
  268.         }
  269.         if (isset($search['kilometers']['max'])) {
  270.             $search['kilometersMax'] = $search['kilometers']['max'];
  271.         }
  272.         if (isset($search['kilometersMin'])) {
  273.             if (isset($search['kilometersMax'])) {
  274.                 $values['kilometers'] = sprintf('%s ~ %s km'$search['kilometersMin'], $search['kilometersMax']);
  275.             } else {
  276.                 $values['kilometers'] = sprintf('> %s km'$search['kilometersMin']);
  277.             }
  278.         } elseif (isset($search['kilometersMax'])) {
  279.             $values['kilometers'] = sprintf('< %s km'$search['kilometersMax']);
  280.         }
  281.         if (isset($search['yearMin'])) {
  282.             if (isset($search['yearMax'])) {
  283.                 $values['year'] = sprintf('%s ~ %s'$search['yearMin'], $search['yearMax']);
  284.             } else {
  285.                 $values['year'] = sprintf('> %s'$search['yearMin']);
  286.             }
  287.         } elseif (isset($search['yearMax'])) {
  288.             $values['year'] = sprintf('< %s'$search['yearMax']);
  289.         }
  290.         if (isset($search['broken']) && true == $search['broken']) {
  291.             $values['broken'] = $this->translator->trans('badges.search.broken');
  292.         }
  293.         if (isset($search['highlight']) && true == $search['highlight']) {
  294.             $values['highlight'] = $this->translator->trans('badges.search.highlight');
  295.         }
  296.         if (isset($search['seatCount'])) {
  297.             $values['seatCount'] = sprintf('%s %s'$search['seatCount'], $this->translator->trans('badges.search.seats'));
  298.         }
  299.         if (isset($search['doorCount'])) {
  300.             $values['doorCount'] = sprintf('%s %s'$search['doorCount'], $this->translator->trans('badges.search.doors'));
  301.         }
  302.         if (isset($search['co2'])) {
  303.             $values['co2'] = sprintf(self::CO2$search['co2']);
  304.         }
  305.         if (isset($search['estimated'])) {
  306.             $values['estimated'] = $this->translator->trans('badges.search.estimated');
  307.         }
  308.         if (isset($search['critair'])) {
  309.             $values['critair'] = sprintf('Crit\'Air %s'$search['critair']);
  310.         }
  311.         return $values;
  312.     }
  313.     private function getUser()
  314.     {
  315.         if (null === $token $this->tokenStorage->getToken()) {
  316.             return null;
  317.         }
  318.         if (!is_object($user $token->getUser()) or !$user instanceof User) {
  319.             return null;
  320.         }
  321.         return $user;
  322.     }
  323. }