vendor/sylius/resource-bundle/src/Bundle/Doctrine/ORM/EntityRepository.php line 27

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\EntityRepository as BaseEntityRepository;
  13. use Doctrine\ORM\QueryBuilder;
  14. use Pagerfanta\Adapter\ArrayAdapter;
  15. use Pagerfanta\Doctrine\ORM\QueryAdapter;
  16. use Pagerfanta\Pagerfanta;
  17. use Sylius\Component\Resource\Model\ResourceInterface;
  18. use Sylius\Component\Resource\Repository\RepositoryInterface;
  19. /** @psalm-suppress DeprecatedInterface */
  20. class EntityRepository extends BaseEntityRepository implements RepositoryInterface
  21. {
  22.     public function add(ResourceInterface $resource): void
  23.     {
  24.         $this->_em->persist($resource);
  25.         $this->_em->flush();
  26.     }
  27.     public function remove(ResourceInterface $resource): void
  28.     {
  29.         if (null !== $this->find($resource->getId())) {
  30.             $this->_em->remove($resource);
  31.             $this->_em->flush();
  32.         }
  33.     }
  34.     public function createPaginator(array $criteria = [], array $sorting = []): iterable
  35.     {
  36.         $queryBuilder $this->createQueryBuilder('o');
  37.         $this->applyCriteria($queryBuilder$criteria);
  38.         $this->applySorting($queryBuilder$sorting);
  39.         return $this->getPaginator($queryBuilder);
  40.     }
  41.     protected function getPaginator(QueryBuilder $queryBuilder): Pagerfanta
  42.     {
  43.         // Use output walkers option in the query adapter should be false as it affects performance greatly (see sylius/sylius#3775)
  44.         return new Pagerfanta(new QueryAdapter($queryBuilderfalsefalse));
  45.     }
  46.     /**
  47.      * @param array $objects
  48.      */
  49.     protected function getArrayPaginator($objects): Pagerfanta
  50.     {
  51.         return new Pagerfanta(new ArrayAdapter($objects));
  52.     }
  53.     protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void
  54.     {
  55.         foreach ($criteria as $property => $value) {
  56.             if (!in_array($propertyarray_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
  57.                 continue;
  58.             }
  59.             $name $this->getPropertyName($property);
  60.             if (null === $value) {
  61.                 $queryBuilder->andWhere($queryBuilder->expr()->isNull($name));
  62.             } elseif (is_array($value)) {
  63.                 $queryBuilder->andWhere($queryBuilder->expr()->in($name$value));
  64.             } elseif ('' !== $value) {
  65.                 $parameter str_replace('.''_'$property);
  66.                 $queryBuilder
  67.                     ->andWhere($queryBuilder->expr()->eq($name':' $parameter))
  68.                     ->setParameter($parameter$value)
  69.                 ;
  70.             }
  71.         }
  72.     }
  73.     protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void
  74.     {
  75.         foreach ($sorting as $property => $order) {
  76.             if (!in_array($propertyarray_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
  77.                 continue;
  78.             }
  79.             if (!empty($order)) {
  80.                 $queryBuilder->addOrderBy($this->getPropertyName($property), $order);
  81.             }
  82.         }
  83.     }
  84.     protected function getPropertyName(string $name): string
  85.     {
  86.         if (false === strpos($name'.')) {
  87.             return 'o' '.' $name;
  88.         }
  89.         return $name;
  90.     }
  91. }