|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SumoCoders\FrameworkCoreBundle\Pagination; |
| 4 | + |
| 5 | +use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; |
| 6 | +use Doctrine\ORM\Tools\Pagination\CountWalker; |
| 7 | +use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; |
| 8 | +use ArrayIterator; |
| 9 | +use IteratorAggregate; |
| 10 | +use Traversable; |
| 11 | +use Countable; |
| 12 | +use Iterator; |
| 13 | + |
| 14 | +class Paginator implements Countable, IteratorAggregate |
| 15 | +{ |
| 16 | + public const PAGE_SIZE = 30; |
| 17 | + |
| 18 | + private DoctrineQueryBuilder $queryBuilder; |
| 19 | + private int $currentPage; |
| 20 | + private int $startPage; |
| 21 | + private int $endPage; |
| 22 | + private int $pageSize; |
| 23 | + private Traversable $results; |
| 24 | + private int $numResults; |
| 25 | + |
| 26 | + public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = self::PAGE_SIZE) |
| 27 | + { |
| 28 | + $this->queryBuilder = $queryBuilder; |
| 29 | + $this->pageSize = $pageSize; |
| 30 | + } |
| 31 | + |
| 32 | + public function paginate(int $page = 1): self |
| 33 | + { |
| 34 | + $this->currentPage = max(1, $page); |
| 35 | + $firstResult = ($this->currentPage - 1) * $this->pageSize; |
| 36 | + |
| 37 | + $query = $this->queryBuilder |
| 38 | + ->setFirstResult($firstResult) |
| 39 | + ->setMaxResults($this->pageSize) |
| 40 | + ->getQuery(); |
| 41 | + |
| 42 | + if (0 === count($this->queryBuilder->getDQLPart('join'))) { |
| 43 | + $query->setHint(CountWalker::HINT_DISTINCT, false); |
| 44 | + } |
| 45 | + |
| 46 | + $paginator = new DoctrinePaginator($query, true); |
| 47 | + |
| 48 | + $useOutputWalkers = count($this->queryBuilder->getDQLPart('having') ?: []) > 0; |
| 49 | + $paginator->setUseOutputWalkers($useOutputWalkers); |
| 50 | + |
| 51 | + $this->results = $paginator->getIterator(); |
| 52 | + $this->numResults = $paginator->count(); |
| 53 | + |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + public function getCurrentPage(): int |
| 58 | + { |
| 59 | + return $this->currentPage; |
| 60 | + } |
| 61 | + |
| 62 | + public function getLastPage(): int |
| 63 | + { |
| 64 | + return (int) ceil($this->numResults / $this->pageSize); |
| 65 | + } |
| 66 | + |
| 67 | + public function getPageSize(): int |
| 68 | + { |
| 69 | + return $this->pageSize; |
| 70 | + } |
| 71 | + |
| 72 | + public function hasPreviousPage(): bool |
| 73 | + { |
| 74 | + return $this->currentPage > 1; |
| 75 | + } |
| 76 | + |
| 77 | + public function getPreviousPage(): int |
| 78 | + { |
| 79 | + return max(1, $this->currentPage - 1); |
| 80 | + } |
| 81 | + |
| 82 | + public function getStartPage(): int |
| 83 | + { |
| 84 | + return $this->startPage; |
| 85 | + } |
| 86 | + |
| 87 | + public function getEndPage(): int |
| 88 | + { |
| 89 | + return $this->endPage; |
| 90 | + } |
| 91 | + |
| 92 | + public function hasNextPage(): bool |
| 93 | + { |
| 94 | + return $this->currentPage < $this->getLastPage(); |
| 95 | + } |
| 96 | + |
| 97 | + public function getNextPage(): int |
| 98 | + { |
| 99 | + return min($this->getLastPage(), $this->currentPage + 1); |
| 100 | + } |
| 101 | + |
| 102 | + public function hasToPaginate(): bool |
| 103 | + { |
| 104 | + return $this->numResults > $this->pageSize; |
| 105 | + } |
| 106 | + |
| 107 | + public function getNumResults(): int |
| 108 | + { |
| 109 | + return $this->numResults; |
| 110 | + } |
| 111 | + |
| 112 | + public function getNumberOfPages(): int |
| 113 | + { |
| 114 | + $numberOfPages = $this->calculateNumberOfPages(); |
| 115 | + |
| 116 | + if (0 === $numberOfPages) { |
| 117 | + return 1; |
| 118 | + } |
| 119 | + |
| 120 | + return $numberOfPages; |
| 121 | + } |
| 122 | + |
| 123 | + private function calculateNumberOfPages(): int |
| 124 | + { |
| 125 | + return (int) ceil($this->getNumResults() / self::PAGE_SIZE); |
| 126 | + } |
| 127 | + |
| 128 | + public function getResults(): Traversable |
| 129 | + { |
| 130 | + return $this->results; |
| 131 | + } |
| 132 | + |
| 133 | + public function count(): int |
| 134 | + { |
| 135 | + return count($this->getResults()); |
| 136 | + } |
| 137 | + |
| 138 | + public function getIterator(): Traversable |
| 139 | + { |
| 140 | + $results = $this->getResults(); |
| 141 | + |
| 142 | + if ($results instanceof Iterator) { |
| 143 | + return $results; |
| 144 | + } |
| 145 | + |
| 146 | + if ($results instanceof IteratorAggregate) { |
| 147 | + return $results->getIterator(); |
| 148 | + } |
| 149 | + |
| 150 | + return new ArrayIterator($results); |
| 151 | + } |
| 152 | + |
| 153 | + public function calculateStartAndEndPage(): void |
| 154 | + { |
| 155 | + $startPage = $this->currentPage - 3; |
| 156 | + $endPage = $this->currentPage + 3; |
| 157 | + |
| 158 | + if ($this->startPageUnderflow($startPage)) { |
| 159 | + $endPage = $this->calculateEndPageForStartPageUnderflow($startPage, $endPage); |
| 160 | + $startPage = 1; |
| 161 | + } |
| 162 | + |
| 163 | + if ($this->endPageOverflow($endPage)) { |
| 164 | + $startPage = $this->calculateStartPageForEndPageOverflow($startPage, $endPage); |
| 165 | + $endPage = $this->getNumberOfPages(); |
| 166 | + } |
| 167 | + |
| 168 | + $this->startPage = $startPage; |
| 169 | + $this->endPage = $endPage; |
| 170 | + } |
| 171 | + |
| 172 | + private function startPageUnderflow($startPage): bool |
| 173 | + { |
| 174 | + return $startPage < 1; |
| 175 | + } |
| 176 | + |
| 177 | + private function endPageOverflow($endPage): bool |
| 178 | + { |
| 179 | + return $endPage > $this->getNumberOfPages(); |
| 180 | + } |
| 181 | + |
| 182 | + private function calculateEndPageForStartPageUnderflow($startPage, $endPage): int |
| 183 | + { |
| 184 | + return min($endPage + (1 - $startPage), $this->getNumberOfPages()); |
| 185 | + } |
| 186 | + |
| 187 | + private function calculateStartPageForEndPageOverflow($startPage, $endPage): int |
| 188 | + { |
| 189 | + return max($startPage - ($endPage - $this->getNumberOfPages()), 1); |
| 190 | + } |
| 191 | +} |
0 commit comments