src/Repository/RegionRepository.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (C) Office National de Publication et de Communication - All Rights Reserved
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited by law
  7.  * This file is proprietary and confidential
  8.  */
  9. namespace App\Repository;
  10. use App\Entity\Region;
  11. use App\Service\Tools;
  12. use Doctrine\ORM\Query;
  13. use App\Repository\Helper\CachedTrait;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  16. /**
  17.  * @method null|Region find($id, $lockMode = null, $lockVersion = null)
  18.  * @method null|Region findOneBy(array $criteria, array $orderBy = null)
  19.  * @method Region[]    findAll()
  20.  * @method Region[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  21.  */
  22. final class RegionRepository extends ServiceEntityRepository
  23. {
  24.     use CachedTrait;
  25.     private const CACHE_LIFETIME 60;
  26.     public function __construct(ManagerRegistry $registry)
  27.     {
  28.         parent::__construct($registryRegion::class);
  29.     }
  30.     public function findAllVisibles(string $onglet null): array
  31.     {
  32.         $queryBuilder $this->createQueryBuilder('r');
  33.         
  34.         if ($onglet && in_array($ongletTools::getOngletsList())) {
  35.             $queryBuilder
  36.                 ->andWhere($queryBuilder->expr()->like('r.onglets'':onglet'))
  37.                 ->setParameter('onglet'sprintf('%%%s%%'$onglet));
  38.         }
  39.         return $queryBuilder
  40.             ->addOrderBy('r.pays''DESC')
  41.             ->addOrderBy('r.libelle''ASC')
  42.             ->andWhere($queryBuilder->expr()->isNull('r.ancienne')) // on ignore les anciennes régions
  43.             ->andWhere($queryBuilder->expr()->isNotNull('r.libelle'))
  44.             ->andWhere($queryBuilder->expr()->isNotNull('r.slug'))
  45.             ->getQuery()
  46.             ->setHint(Query::HINT_FORCE_PARTIAL_LOADtrue)
  47.             ->useResultCache(trueself::CACHE_LIFETIME)
  48.             ->getResult();
  49.     }
  50.     public function findAllByTerm(string $termint $max): array
  51.     {
  52.         $term trim($term);
  53.         if (empty($term)) {
  54.             return [];
  55.         }
  56.         $queryBuilder $this->createQueryBuilder('r');
  57.         return $queryBuilder
  58.             ->orderBy('r.libelle''ASC')
  59.             ->andWhere($queryBuilder->expr()->isNull('r.ancienne')) // on ignore les anciennes régions
  60.             ->andWhere($queryBuilder->expr()->isNotNull('r.libelle'))
  61.             ->andWhere($queryBuilder->expr()->isNotNull('r.slug'))
  62.             ->andWhere($queryBuilder->expr()->like('r.libelle'':term'))
  63.             ->andWhere('r.regionAdm IS NOT NULL')
  64.             ->setParameter('term'$term.'%')
  65.             ->setMaxResults($max)
  66.             ->getQuery()
  67.             ->setHint(Query::HINT_FORCE_PARTIAL_LOADtrue)
  68.             ->useResultCache(trueself::CACHE_LIFETIME)
  69.             ->getResult();
  70.     }
  71.     public function getSlugs(): array
  72.     {
  73.         $list $this
  74.             ->createQueryBuilder('r')
  75.             ->select('r.slug')
  76.             ->getQuery()
  77.             ->getResult();
  78.         return array_map(static function ($value) {
  79.             return $value['slug'];
  80.         }, $list);
  81.     }
  82.     // Retourne une liste triée e, supprimant la région DEFAULT et les libellés vides
  83.     public function findAll(): array
  84.     {
  85.         $queryBuilder $this->createQueryBuilder('r');
  86.         return $queryBuilder
  87.             ->orderBy('r.libelle''ASC')
  88.             ->andWhere($queryBuilder->expr()->isNull('r.ancienne')) // on ignore les anciennes régions
  89.             ->andWhere($queryBuilder->expr()->isNotNull('r.libelle'))
  90.             ->andWhere($queryBuilder->expr()->isNotNull('r.slug'))
  91.             ->andWhere("r.libelle != 'DEFAULT'")
  92.             // ->andWhere("r.id != 'AUTR'") // enseignement français à l'éranger
  93.             ->andWhere("r.regionAdm IS NOT NULL")
  94.             ->getQuery()
  95.             ->setHint(Query::HINT_FORCE_PARTIAL_LOADtrue)
  96.             ->useResultCache(trueself::CACHE_LIFETIME)
  97.             ->getResult();
  98.     }
  99.     public function findAllByEtablissement(): array
  100.     {
  101.         $queryBuilder $this->createQueryBuilder('r');
  102.         return $queryBuilder
  103.             ->leftJoin('r.etablissements''e')
  104.             ->addOrderBy('r.libelle''ASC')
  105.             ->andWhere("e.region IS NOT NULL")
  106.             ->andWhere("r.ancienne IS NULL")
  107.             ->getQuery()
  108.             // ->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  109.             // ->useResultCache(true, self::CACHE_LIFETIME)
  110.             ->getResult();
  111.     }
  112. }