|
| 1 | +
|
| 2 | +/* |
| 3 | + * This file is part of the League\Fractal package. |
| 4 | + * |
| 5 | + * (c) Phil Sturgeon |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace League\Fractal\Pagination; |
| 12 | + |
| 13 | +use Doctrine\ORM\Tools\Pagination\Paginator; |
| 14 | + |
| 15 | +/** |
| 16 | + * A paginator adapter for doctrine pagination. |
| 17 | + * |
| 18 | + * @author Fraser Stockley |
| 19 | + */ |
| 20 | +class DoctrinePaginatorAdapter implements PaginatorInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * The paginator instance. |
| 24 | + * @var Paginator |
| 25 | + */ |
| 26 | + private $paginator; |
| 27 | + |
| 28 | + /** |
| 29 | + * The route generator. |
| 30 | + * |
| 31 | + * @var callable |
| 32 | + */ |
| 33 | + private $routeGenerator; |
| 34 | + |
| 35 | + /** |
| 36 | + * Create a new DoctrinePaginatorAdapter. |
| 37 | + * @param Paginator $paginator |
| 38 | + * @param callable $routeGenerator |
| 39 | + * |
| 40 | + */ |
| 41 | + public function __construct(Paginator $paginator, callable $routeGenerator) |
| 42 | + { |
| 43 | + $this->paginator = $paginator; |
| 44 | + $this->routeGenerator = $routeGenerator; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the current page. |
| 49 | + * |
| 50 | + * @return int |
| 51 | + */ |
| 52 | + public function getCurrentPage() |
| 53 | + { |
| 54 | + return ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the last page. |
| 59 | + * |
| 60 | + * @return int |
| 61 | + */ |
| 62 | + public function getLastPage() |
| 63 | + { |
| 64 | + return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the total. |
| 69 | + * |
| 70 | + * @return int |
| 71 | + */ |
| 72 | + public function getTotal() |
| 73 | + { |
| 74 | + return count($this->paginator); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the count. |
| 79 | + * |
| 80 | + * @return int |
| 81 | + */ |
| 82 | + public function getCount() |
| 83 | + { |
| 84 | + return $this->paginator->getIterator()->count(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the number per page. |
| 89 | + * |
| 90 | + * @return int |
| 91 | + */ |
| 92 | + public function getPerPage() |
| 93 | + { |
| 94 | + return $this->paginator->getQuery()->getMaxResults(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the url for the given page. |
| 99 | + * |
| 100 | + * @param int $page |
| 101 | + * |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + public function getUrl($page) |
| 105 | + { |
| 106 | + return call_user_func($this->getRouteGenerator(), $page); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the the route generator. |
| 111 | + * |
| 112 | + * @return callable |
| 113 | + */ |
| 114 | + private function getRouteGenerator() |
| 115 | + { |
| 116 | + return $this->routeGenerator; |
| 117 | + } |
| 118 | +} |
0 commit comments