vendor/sylius/resource-bundle/src/Bundle/Doctrine/ORM/ResourceRepositoryTrait.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Doctrine\ORM;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Mapping\ClassMetadata;
  14. use Doctrine\ORM\QueryBuilder;
  15. use Pagerfanta\Adapter\ArrayAdapter;
  16. use Pagerfanta\Doctrine\ORM\QueryAdapter;
  17. use Pagerfanta\Pagerfanta;
  18. use Sylius\Component\Resource\Model\ResourceInterface;
  19. /**
  20.  * @property EntityManagerInterface $_em
  21.  * @property ClassMetadata          $_class
  22.  *
  23.  * @method QueryBuilder createQueryBuilder(string $alias, string $indexBy = null)
  24.  * @method ?object      find($id, $lockMode = null, $lockVersion = null)
  25.  */
  26. trait ResourceRepositoryTrait
  27. {
  28.     public function add(ResourceInterface $resource): void
  29.     {
  30.         $this->_em->persist($resource);
  31.         $this->_em->flush();
  32.     }
  33.     public function remove(ResourceInterface $resource): void
  34.     {
  35.         if (null !== $this->find($resource->getId())) {
  36.             $this->_em->remove($resource);
  37.             $this->_em->flush();
  38.         }
  39.     }
  40.     /**
  41.      * @return iterable<int, ResourceInterface>
  42.      */
  43.     public function createPaginator(array $criteria = [], array $sorting = []): iterable
  44.     {
  45.         $queryBuilder $this->createQueryBuilder('o');
  46.         $this->applyCriteria($queryBuilder$criteria);
  47.         $this->applySorting($queryBuilder$sorting);
  48.         return $this->getPaginator($queryBuilder);
  49.     }
  50.     protected function getPaginator(QueryBuilder $queryBuilder): Pagerfanta
  51.     {
  52.         if (!class_exists(QueryAdapter::class)) {
  53.             throw new \LogicException('You can not use the "paginator" if Pargefanta Doctrine ORM Adapter is not available. Try running "composer require pagerfanta/doctrine-orm-adapter".');
  54.         }
  55.         // Use output walkers option in the query adapter should be false as it affects performance greatly (see sylius/sylius#3775)
  56.         return new Pagerfanta(new QueryAdapter($queryBuilderfalsefalse));
  57.     }
  58.     /**
  59.      * @param array $objects
  60.      */
  61.     protected function getArrayPaginator($objects): Pagerfanta
  62.     {
  63.         return new Pagerfanta(new ArrayAdapter($objects));
  64.     }
  65.     protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void
  66.     {
  67.         foreach ($criteria as $property => $value) {
  68.             if (!in_array($propertyarray_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
  69.                 continue;
  70.             }
  71.             $name $this->getPropertyName($property);
  72.             if (null === $value) {
  73.                 $queryBuilder->andWhere($queryBuilder->expr()->isNull($name));
  74.             } elseif (is_array($value)) {
  75.                 $queryBuilder->andWhere($queryBuilder->expr()->in($name$value));
  76.             } elseif ('' !== $value) {
  77.                 $parameter str_replace('.''_'$property);
  78.                 $queryBuilder
  79.                     ->andWhere($queryBuilder->expr()->eq($name':' $parameter))
  80.                     ->setParameter($parameter$value)
  81.                 ;
  82.             }
  83.         }
  84.     }
  85.     protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void
  86.     {
  87.         foreach ($sorting as $property => $order) {
  88.             if (!in_array($propertyarray_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
  89.                 continue;
  90.             }
  91.             if (!empty($order)) {
  92.                 $queryBuilder->addOrderBy($this->getPropertyName($property), $order);
  93.             }
  94.         }
  95.     }
  96.     protected function getPropertyName(string $name): string
  97.     {
  98.         if (false === strpos($name'.')) {
  99.             return 'o' '.' $name;
  100.         }
  101.         return $name;
  102.     }
  103. }