vendor/sylius/grid-bundle/src/Bundle/Doctrine/ORM/DataSource.php line 71

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\GridBundle\Doctrine\ORM;
  12. use Doctrine\ORM\QueryBuilder;
  13. use Pagerfanta\Adapter\DoctrineORMAdapter;
  14. use Pagerfanta\Pagerfanta;
  15. use Sylius\Component\Grid\Data\DataSourceInterface;
  16. use Sylius\Component\Grid\Data\ExpressionBuilderInterface;
  17. use Sylius\Component\Grid\Parameters;
  18. final class DataSource implements DataSourceInterface
  19. {
  20.     private QueryBuilder $queryBuilder;
  21.     private ExpressionBuilderInterface $expressionBuilder;
  22.     private bool $fetchJoinCollection;
  23.     private bool $useOutputWalkers;
  24.     /**
  25.      * @param bool $fetchJoinCollection must be 'true' when the query fetch-joins a to-many collection,
  26.      *                                  otherwise the pagination will yield incorrect results
  27.      *                                  https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/pagination.html
  28.      * @param bool $useOutputWalkers must be 'true' if the query has an order by statement for a field from
  29.      *                                the to-many association, otherwise it will throw an exception
  30.      *                                might greatly affect the performance (https://github.com/Sylius/Sylius/issues/3775)
  31.      */
  32.     public function __construct(QueryBuilder $queryBuilderbool $fetchJoinCollectionbool $useOutputWalkers)
  33.     {
  34.         $this->queryBuilder $queryBuilder;
  35.         $this->expressionBuilder = new ExpressionBuilder($queryBuilder);
  36.         $this->fetchJoinCollection $fetchJoinCollection;
  37.         $this->useOutputWalkers $useOutputWalkers;
  38.     }
  39.     public function restrict($expressionstring $condition DataSourceInterface::CONDITION_AND): void
  40.     {
  41.         switch ($condition) {
  42.             case DataSourceInterface::CONDITION_AND:
  43.                 $this->queryBuilder->andWhere($expression);
  44.                 break;
  45.             case DataSourceInterface::CONDITION_OR:
  46.                 $this->queryBuilder->orWhere($expression);
  47.                 break;
  48.         }
  49.     }
  50.     public function getExpressionBuilder(): ExpressionBuilderInterface
  51.     {
  52.         return $this->expressionBuilder;
  53.     }
  54.     public function getData(Parameters $parameters)
  55.     {
  56.         $paginator = new Pagerfanta(
  57.             new DoctrineORMAdapter($this->queryBuilder$this->fetchJoinCollection$this->useOutputWalkers)
  58.         );
  59.         $paginator->setNormalizeOutOfRangePages(true);
  60.         $paginator->setCurrentPage($parameters->get('page'1));
  61.         return $paginator;
  62.     }
  63. }