1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\Pager; |
4
|
|
|
|
5
|
|
|
use BenTools\OpenCubes\Component\ComponentInterface; |
6
|
|
|
use BenTools\OpenCubes\Component\Pager\Model\Page; |
7
|
|
|
use BenTools\OpenCubes\Component\Pager\Model\PageSize; |
8
|
|
|
use Countable; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
|
12
|
|
|
final class PagerComponent implements ComponentInterface, Countable, JsonSerializable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var UriInterface |
16
|
|
|
*/ |
17
|
|
|
private $baseUri; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $nbItems; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int|null |
26
|
|
|
*/ |
27
|
|
|
private $perPage; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $currentPageNumber; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $delta; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var PageSize[] |
41
|
|
|
*/ |
42
|
|
|
private $pageSizes; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var PagerUriManagerInterface |
46
|
|
|
*/ |
47
|
|
|
private $uriManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Page[] |
51
|
|
|
*/ |
52
|
|
|
private $pages; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* PagerComponent constructor. |
56
|
|
|
* @param UriInterface $baseUri |
57
|
|
|
* @param int $totalItems |
58
|
|
|
* @param int|null $perPage |
59
|
|
|
* @param int $currentPageNumber |
60
|
|
|
* @param int|null $delta |
61
|
|
|
* @param array $pageSizes |
62
|
|
|
* @param PagerUriManagerInterface|null $uriManager |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
UriInterface $baseUri, |
66
|
|
|
int $totalItems = 0, |
67
|
|
|
?int $perPage = null, |
68
|
|
|
int $currentPageNumber = 1, |
69
|
|
|
?int $delta = null, |
70
|
|
|
array $pageSizes = [], |
71
|
|
|
PagerUriManagerInterface $uriManager = null |
72
|
|
|
) { |
73
|
|
|
$this->baseUri = $baseUri; |
74
|
|
|
$this->nbItems = $totalItems; |
75
|
|
|
$this->perPage = $perPage; |
76
|
|
|
$this->currentPageNumber = $currentPageNumber; |
77
|
|
|
|
78
|
|
|
$this->pageSizes = (function (PageSize ...$pageSizes) { |
79
|
|
|
return $pageSizes; |
80
|
|
|
})(...$pageSizes); |
81
|
|
|
|
82
|
|
|
$this->uriManager = $uriManager ?? new PagerUriManager(); |
83
|
|
|
$this->delta = $delta; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $nbItems |
88
|
|
|
*/ |
89
|
|
|
public function setNbItems(int $nbItems): void |
90
|
|
|
{ |
91
|
|
|
$this->nbItems = $nbItems; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isEnabled() |
98
|
|
|
{ |
99
|
|
|
return null !== $this->perPage; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return PageSize[] |
104
|
|
|
*/ |
105
|
|
|
public function getPageSizes(): array |
106
|
|
|
{ |
107
|
|
|
return $this->pageSizes; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Page[] |
112
|
|
|
*/ |
113
|
|
|
public function getPages(): array |
114
|
|
|
{ |
115
|
|
|
if (null === $this->pages) { |
116
|
|
|
$this->pages = iterator_to_array($this->generatePages()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->pages; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return \Generator |
124
|
|
|
* @throws \InvalidArgumentException |
125
|
|
|
*/ |
126
|
|
|
private function generatePages() |
127
|
|
|
{ |
128
|
|
|
$pageNumbers = []; |
129
|
|
|
$numberOfPages = count($this); |
130
|
|
|
|
131
|
|
|
for ($pageNumber = 1; $pageNumber <= $numberOfPages; $pageNumber++) { |
132
|
|
|
$pageNumbers[] = $pageNumber; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (null !== $this->delta) { |
136
|
|
|
$pageNumbers = array_filter($pageNumbers, function (int $pageNumber) { |
137
|
|
|
return $pageNumber <= ($this->getCurrentPage() + $this->delta) |
138
|
|
|
&& $pageNumber >= ($this->getCurrentPage() - $this->delta); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($pageNumbers as $pageNumber) { |
143
|
|
|
yield $this->createPage($pageNumber); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $pageNumber |
149
|
|
|
* @return Page |
150
|
|
|
* @throws \InvalidArgumentException |
151
|
|
|
*/ |
152
|
|
|
private function createPage(int $pageNumber): Page |
153
|
|
|
{ |
154
|
|
|
return new Page( |
155
|
|
|
$pageNumber, |
156
|
|
|
$this->getPageCount($pageNumber), |
157
|
|
|
$this->getPageOffset($pageNumber), |
158
|
|
|
$this->isFirstPage($pageNumber), |
159
|
|
|
$this->isPreviousPage($pageNumber), |
160
|
|
|
$this->isCurrentPage($pageNumber), |
161
|
|
|
$this->isNextPage($pageNumber), |
162
|
|
|
$this->isLastPage($pageNumber), |
163
|
|
|
$this->uriManager->buildPageUri($this->baseUri, $pageNumber) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public static function getName(): string |
171
|
|
|
{ |
172
|
|
|
return 'pager'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* /** |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
|
|
public function count(): int |
181
|
|
|
{ |
182
|
|
|
if (0 === $this->getPerPage()) { |
183
|
|
|
return 1; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return max(1, ceil($this->getNbItems() / $this->getPerPage())); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
|
|
public function getCurrentPage(): int |
193
|
|
|
{ |
194
|
|
|
return $this->currentPageNumber; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return int |
199
|
|
|
*/ |
200
|
|
|
public function getNbItems(): int |
201
|
|
|
{ |
202
|
|
|
return $this->nbItems; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return int |
207
|
|
|
*/ |
208
|
|
|
public function getCurrentOffset(): int |
209
|
|
|
{ |
210
|
|
|
if (!$this->isEnabled()) { |
211
|
|
|
return 0; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return ($this->getCurrentPage() - 1) * $this->getPerPage(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param int $page |
219
|
|
|
* @return int |
220
|
|
|
*/ |
221
|
|
|
public function getPageOffset(int $page): int |
222
|
|
|
{ |
223
|
|
|
$page = $this->snap($page); |
224
|
|
|
|
225
|
|
|
return max(0, ($page - 1) * $this->getPerPage()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param int $page |
230
|
|
|
* @return int |
231
|
|
|
*/ |
232
|
|
|
public function getPageCount(int $page): int |
233
|
|
|
{ |
234
|
|
|
$page = $this->snap($page); |
235
|
|
|
|
236
|
|
|
if ($this->isLastPage($page)) { |
237
|
|
|
return ($this->getPerPage() - (($page * $this->getPerPage()) - $this->getNbItems())); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $this->getPerPage(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
|
|
public function getPerPage(): int |
247
|
|
|
{ |
248
|
|
|
return $this->perPage ?? $this->nbItems; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param int|null $page |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
public function isFirstPage(?int $page = null): bool |
256
|
|
|
{ |
257
|
|
|
return 1 === $this->snap($page); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param int $page |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
public function isPreviousPage(int $page): bool |
265
|
|
|
{ |
266
|
|
|
return $this->getCurrentPage() - 1 === $page; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param int $page |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
public function isCurrentPage(int $page): bool |
274
|
|
|
{ |
275
|
|
|
return $this->getCurrentPage() === $page; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param int $page |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function isNextPage(int $page): bool |
283
|
|
|
{ |
284
|
|
|
return $this->snap($this->getCurrentPage() + 1) === $page; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param int|null $page |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isLastPage(?int $page = null): bool |
292
|
|
|
{ |
293
|
|
|
return $this->getLastPage() === $this->snap($page); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param int|null $page |
|
|
|
|
298
|
|
|
* @return int |
299
|
|
|
*/ |
300
|
|
|
public function getFirstPage(): int |
301
|
|
|
{ |
302
|
|
|
return 1; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return int |
307
|
|
|
*/ |
308
|
|
|
public function getLastPage(): int |
309
|
|
|
{ |
310
|
|
|
return count($this); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return int |
315
|
|
|
*/ |
316
|
|
|
public function getPreviousPage(?int $page = null): int |
317
|
|
|
{ |
318
|
|
|
$page = $page ?? $this->getCurrentPage(); |
319
|
|
|
$page = $this->snap($page); |
320
|
|
|
return $this->snap($page - 1); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return int |
325
|
|
|
*/ |
326
|
|
|
public function getNextPage(?int $page = null): int |
327
|
|
|
{ |
328
|
|
|
$page = $page ?? $this->getCurrentPage(); |
329
|
|
|
$page = $this->snap($page); |
330
|
|
|
return $this->snap($page + 1); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param int $page |
335
|
|
|
* @return int |
336
|
|
|
*/ |
337
|
|
|
private function snap(?int $page): int |
338
|
|
|
{ |
339
|
|
|
$page = $page ?? $this->getCurrentPage(); |
340
|
|
|
|
341
|
|
|
if ($page < 1) { |
342
|
|
|
return 1; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$lastPage = $this->getLastPage(); |
346
|
|
|
|
347
|
|
|
if ($page > $lastPage) { |
348
|
|
|
return $lastPage; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return $page; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @inheritDoc |
356
|
|
|
*/ |
357
|
|
|
public function jsonSerialize(): array |
358
|
|
|
{ |
359
|
|
|
return [ |
360
|
|
|
'is_enabled' => $this->isEnabled(), |
361
|
|
|
'per_page' => $this->getPerPage(), |
362
|
|
|
'nb_items' => $this->getNbItems(), |
363
|
|
|
'count' => $this->count(), |
364
|
|
|
'first' => $this->createPage($this->getFirstPage()), |
365
|
|
|
'previous' => !$this->isFirstPage() ? $this->createPage($this->getPreviousPage()) : null, |
366
|
|
|
'current' => $this->createPage($this->getCurrentPage()), |
367
|
|
|
'next' => !$this->isLastPage() ? $this->createPage($this->getNextPage()) : null, |
368
|
|
|
'last' => $this->createPage($this->getLastPage()), |
369
|
|
|
'pages' => $this->getPages(), |
370
|
|
|
'page_sizes' => $this->getPageSizes(), |
371
|
|
|
]; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.