src/Pagination/Paginator.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Pagination;
  11. use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
  12. use Doctrine\ORM\Tools\Pagination\CountWalker;
  13. use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
  14. /**
  15.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  16.  */
  17. class Paginator
  18. {
  19.     /**
  20.      * Use constants to define configuration options that rarely change instead
  21.      * of specifying them under parameters section in config/services.yaml file.
  22.      *
  23.      * See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
  24.      */
  25.     public const PAGE_SIZE 10;
  26.     private int $currentPage;
  27.     private \Traversable $results;
  28.     private int $numResults;
  29.     public function __construct(
  30.         private DoctrineQueryBuilder $queryBuilder,
  31.         private int $pageSize self::PAGE_SIZE
  32.     ) {
  33.     }
  34.     public function paginate(int $page 1): self
  35.     {
  36.         $this->currentPage max(1$page);
  37.         $firstResult = ($this->currentPage 1) * $this->pageSize;
  38.         $query $this->queryBuilder
  39.             ->setFirstResult($firstResult)
  40.             ->setMaxResults($this->pageSize)
  41.             ->getQuery();
  42.         if (=== \count($this->queryBuilder->getDQLPart('join'))) {
  43.             $query->setHint(CountWalker::HINT_DISTINCTfalse);
  44.         }
  45.         $paginator = new DoctrinePaginator($querytrue);
  46.         $useOutputWalkers \count($this->queryBuilder->getDQLPart('having') ?: []) > 0;
  47.         $paginator->setUseOutputWalkers($useOutputWalkers);
  48.         $this->results $paginator->getIterator();
  49.         $this->numResults $paginator->count();
  50.         return $this;
  51.     }
  52.     public function getCurrentPage(): int
  53.     {
  54.         return $this->currentPage;
  55.     }
  56.     public function getLastPage(): int
  57.     {
  58.         return (int) ceil($this->numResults $this->pageSize);
  59.     }
  60.     public function getPageSize(): int
  61.     {
  62.         return $this->pageSize;
  63.     }
  64.     public function hasPreviousPage(): bool
  65.     {
  66.         return $this->currentPage 1;
  67.     }
  68.     public function getPreviousPage(): int
  69.     {
  70.         return max(1$this->currentPage 1);
  71.     }
  72.     public function hasNextPage(): bool
  73.     {
  74.         return $this->currentPage $this->getLastPage();
  75.     }
  76.     public function getNextPage(): int
  77.     {
  78.         return min($this->getLastPage(), $this->currentPage 1);
  79.     }
  80.     public function hasToPaginate(): bool
  81.     {
  82.         return $this->numResults $this->pageSize;
  83.     }
  84.     public function getNumResults(): int
  85.     {
  86.         return $this->numResults;
  87.     }
  88.     public function getResults(): \Traversable
  89.     {
  90.         return $this->results;
  91.     }
  92. }