|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the League\Fractal package. |
| 5 | + * |
| 6 | + * (c) Phil Sturgeon <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace League\Fractal\Pagination; |
| 13 | + |
| 14 | +use Laminas\Paginator\Paginator; |
| 15 | + |
| 16 | +/** |
| 17 | + * A paginator adapter for laminas/laminas-paginator. |
| 18 | + * |
| 19 | + * @author Abdul Malik Ikhsan <[email protected]> |
| 20 | + */ |
| 21 | +class LaminasPaginatorAdapter implements PaginatorInterface |
| 22 | +{ |
| 23 | + protected Paginator $paginator; |
| 24 | + |
| 25 | + /** |
| 26 | + * The route generator. |
| 27 | + * |
| 28 | + * @var callable |
| 29 | + */ |
| 30 | + protected $routeGenerator; |
| 31 | + |
| 32 | + public function __construct(Paginator $paginator, callable $routeGenerator) |
| 33 | + { |
| 34 | + $this->paginator = $paginator; |
| 35 | + $this->routeGenerator = $routeGenerator; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritDoc} |
| 40 | + */ |
| 41 | + public function getCurrentPage(): int |
| 42 | + { |
| 43 | + return $this->paginator->getCurrentPageNumber(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritDoc} |
| 48 | + */ |
| 49 | + public function getLastPage(): int |
| 50 | + { |
| 51 | + return $this->paginator->count(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritDoc} |
| 56 | + */ |
| 57 | + public function getTotal(): int |
| 58 | + { |
| 59 | + return $this->paginator->getTotalItemCount(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritDoc} |
| 64 | + */ |
| 65 | + public function getCount(): int |
| 66 | + { |
| 67 | + return $this->paginator->getCurrentItemCount(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritDoc} |
| 72 | + */ |
| 73 | + public function getPerPage(): int |
| 74 | + { |
| 75 | + return $this->paginator->getItemCountPerPage(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + public function getUrl(int $page): string |
| 82 | + { |
| 83 | + return call_user_func($this->routeGenerator, $page); |
| 84 | + } |
| 85 | + |
| 86 | + public function getPaginator(): Paginator |
| 87 | + { |
| 88 | + return $this->paginator; |
| 89 | + } |
| 90 | + |
| 91 | + public function getRouteGenerator(): callable |
| 92 | + { |
| 93 | + return $this->routeGenerator; |
| 94 | + } |
| 95 | +} |
0 commit comments