1 | <?php |
||
5 | class Paginator implements PaginatorInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var AdapterInterface $adapter |
||
9 | */ |
||
10 | private $adapter; |
||
11 | /** |
||
12 | * @var int |
||
13 | */ |
||
14 | private $currentPage = 1; |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | private $lastPage; |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $currentResults; |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $numberOfResults = -1; |
||
27 | /** |
||
28 | * @var array $queryParameters |
||
29 | */ |
||
30 | private $queryParameters; |
||
31 | /** |
||
32 | * @var array $uriParameters |
||
33 | */ |
||
34 | private $uriParameters; |
||
35 | |||
36 | /** |
||
37 | * @param AdapterInterface $adapter |
||
38 | * @param array $queryParameters |
||
39 | * @param array $uriParameters |
||
40 | */ |
||
41 | public function __construct(AdapterInterface $adapter, array $queryParameters = [], array $uriParameters = []) |
||
42 | { |
||
43 | $queryParameters['limit'] = isset($queryParameters['limit']) ? $queryParameters['limit'] : 10; |
||
44 | if (!is_int($queryParameters['limit'])) { |
||
45 | throw new \InvalidArgumentException('Page limit must an integer!'); |
||
46 | } |
||
47 | $this->adapter = $adapter; |
||
48 | $this->queryParameters = $queryParameters; |
||
49 | $this->queryParameters['page'] = $this->currentPage; |
||
50 | $this->uriParameters = $uriParameters; |
||
51 | $this->lastPage = (int) ceil($this->getNumberOfResults() / $queryParameters['limit']); |
||
52 | } |
||
53 | |||
54 | public function getCurrentPageResults() |
||
55 | { |
||
56 | if (!$this->isResultCached()) { |
||
57 | $this->queryParameters['page'] = $this->currentPage; |
||
58 | $this->currentResults = $this->adapter->getResults($this->queryParameters, $this->uriParameters); |
||
59 | } |
||
60 | |||
61 | return $this->currentResults; |
||
62 | } |
||
63 | |||
64 | private function isResultCached() |
||
68 | |||
69 | public function previousPage() |
||
70 | { |
||
71 | if (!$this->hasPreviousPage()) { |
||
72 | throw new \LogicException('There is no previous page.'); |
||
73 | } |
||
74 | $this->currentPage--; |
||
75 | $this->currentResults = null; |
||
76 | } |
||
77 | |||
78 | public function nextPage() |
||
79 | { |
||
80 | if (!$this->hasNextPage()) { |
||
81 | throw new \LogicException('There is no next page.'); |
||
82 | } |
||
83 | $this->currentPage++; |
||
84 | $this->currentResults = null; |
||
85 | } |
||
86 | |||
87 | public function hasPreviousPage() |
||
91 | |||
92 | public function hasNextPage() |
||
96 | |||
97 | public function getNumberOfResults() |
||
98 | { |
||
99 | if (-1 === $this->numberOfResults) { |
||
100 | $this->numberOfResults = $this->adapter->getNumberOfResults($this->queryParameters, $this->uriParameters); |
||
101 | } |
||
102 | |||
103 | return $this->numberOfResults; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return int |
||
108 | */ |
||
109 | public function getCurrentPage() |
||
113 | } |
||
114 |