src/Pagination/Paginator.php line 41

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\Tools\Pagination\CountWalker;
  12. use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
  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 = 1;
  26. private $currentPage;
  27. private $results;
  28. private $numResults;
  29. private $pageSize;
  30. private $queryBuilder;
  31. public function __construct(DoctrineQueryBuilder $queryBuilder) {
  32. $this->queryBuilder = $queryBuilder;
  33. // $this->pageSize = $pageSize;
  34. }
  35. public function paginate(int $page = 1,$page_size): self
  36. {
  37. $this->pageSize = $page_size;
  38. $this->currentPage = max(1, $page);
  39. $firstResult = ($this->currentPage - 1) * $this->pageSize;
  40. $query = $this->queryBuilder
  41. ->setFirstResult($firstResult)
  42. ->setMaxResults($this->pageSize)
  43. ->getQuery();
  44. if (0 === \count($this->queryBuilder->getDQLPart('join'))) {
  45. $query->setHint(CountWalker::HINT_DISTINCT, false);
  46. }
  47. $paginator = new DoctrinePaginator($query, true);
  48. $useOutputWalkers = \count($this->queryBuilder->getDQLPart('having') ?: []) > 0;
  49. $paginator->setUseOutputWalkers($useOutputWalkers);
  50. $this->results = $paginator->getIterator();
  51. $this->numResults = $paginator->count();
  52. return $this;
  53. }
  54. public function getCurrentPage(): int
  55. {
  56. return $this->currentPage;
  57. }
  58. public function getLastPage(): int
  59. {
  60. return (int) ceil($this->numResults / $this->pageSize);
  61. }
  62. public function getPageSize(): int
  63. {
  64. return $this->pageSize;
  65. }
  66. public function setPageSize(?int $page_size): self
  67. {
  68. $this->page_size = $page_size;
  69. return $this;
  70. }
  71. public function hasPreviousPage(): bool
  72. {
  73. return $this->currentPage > 1;
  74. }
  75. public function getPreviousPage(): int
  76. {
  77. return max(1, $this->currentPage - 1);
  78. }
  79. public function hasNextPage(): bool
  80. {
  81. return $this->currentPage < $this->getLastPage();
  82. }
  83. public function getNextPage(): int
  84. {
  85. return min($this->getLastPage(), $this->currentPage + 1);
  86. }
  87. public function hasToPaginate(): bool
  88. {
  89. return $this->numResults > $this->pageSize;
  90. }
  91. public function getNumResults(): int
  92. {
  93. return $this->numResults;
  94. }
  95. public function getResults(): \Traversable
  96. {
  97. return $this->results;
  98. }
  99. }