-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPagination.php
More file actions
579 lines (509 loc) · 12.4 KB
/
Copy pathPagination.php
File metadata and controls
579 lines (509 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<?php
namespace Bow\Database;
use ArrayAccess;
use Bow\Database\Collection as DatabaseCollection;
use Bow\Support\Collection as SupportCollection;
use Countable;
use IteratorAggregate;
use Traversable;
class Pagination implements ArrayAccess, Countable, IteratorAggregate
{
/**
* The base URL for pagination links.
*
* @var string|null
*/
private ?string $baseUrl = null;
/**
* The query string parameters to append to pagination URLs.
*
* @var array
*/
private array $queryParams = [];
/**
* The page query parameter name.
*
* @var string
*/
private string $pageParam = 'page';
/**
* Pagination constructor.
*
* @param int $next The next page number.
* @param int $previous The previous page number.
* @param int $total The total number of items.
* @param int $perPage The number of items per page.
* @param int $current The current page number.
* @param SupportCollection|DatabaseCollection $data The collection of items for the current page.
*/
public function __construct(
private readonly int $next,
private readonly int $previous,
private readonly int $total,
private readonly int $perPage,
private readonly int $current,
private readonly SupportCollection|DatabaseCollection $data
) {
}
/**
* Set the base URL for pagination links.
*
* @param string $url
* @return static
*/
public function setBaseUrl(string $url): static
{
$this->baseUrl = $url;
return $this;
}
/**
* Get the base URL for pagination links.
*
* @return string|null
*/
public function getBaseUrl(): ?string
{
return $this->baseUrl;
}
/**
* Set the page query parameter name.
*
* @param string $name
* @return static
*/
public function setPageParam(string $name): static
{
$this->pageParam = $name;
return $this;
}
/**
* Get the page query parameter name.
*
* @return string
*/
public function getPageParam(): string
{
return $this->pageParam;
}
/**
* Add query parameters to the pagination URLs.
*
* @param array $params
* @return static
*/
public function withQueryParams(array $params): static
{
$this->queryParams = array_merge($this->queryParams, $params);
return $this;
}
/**
* Set query parameters for the pagination URLs (replaces existing).
*
* @param array $params
* @return static
*/
public function setQueryParams(array $params): static
{
$this->queryParams = $params;
return $this;
}
/**
* Get the query parameters.
*
* @return array
*/
public function getQueryParams(): array
{
return $this->queryParams;
}
/**
* Get the next page number.
*
* @return int
*/
public function next(): int
{
return $this->next;
}
/**
* Check if there is a next page.
*
* @return bool
*/
public function hasNext(): bool
{
return $this->next != 0;
}
/**
* Get the number of items per page.
*
* @return int
*/
public function perPage(): int
{
return $this->perPage;
}
/**
* Get the previous page number.
*
* @return int
*/
public function previous(): int
{
return $this->previous;
}
/**
* Check if there is a previous page.
*
* @return bool
*/
public function hasPrevious(): bool
{
return $this->previous != 0;
}
/**
* Get the current page number.
*
* @return int
*/
public function current(): int
{
return $this->current;
}
/**
* Get the collection of items for the current page.
*
* @return SupportCollection|DatabaseCollection
*/
public function items(): SupportCollection|DatabaseCollection
{
return $this->data;
}
/**
* Get the total number of items.
*
* @return int
*/
public function total(): int
{
return $this->total;
}
/**
* Get the total number of pages.
*
* @return int
*/
public function totalPages(): int
{
return (int) ceil($this->total / $this->perPage);
}
/**
* Check if there are multiple pages.
*
* @return bool
*/
public function hasPages(): bool
{
return $this->totalPages() > 1;
}
/**
* Check if currently on the first page.
*
* @return bool
*/
public function onFirstPage(): bool
{
return $this->current === 1;
}
/**
* Check if currently on the last page.
*
* @return bool
*/
public function onLastPage(): bool
{
return $this->current === $this->totalPages();
}
/**
* Check if the pagination has no items.
*
* @return bool
*/
public function isEmpty(): bool
{
return $this->data->isEmpty();
}
/**
* Check if the pagination has items.
*
* @return bool
*/
public function isNotEmpty(): bool
{
return !$this->isEmpty();
}
/**
* Get the number of items on the current page.
*
* @return int
*/
public function count(): int
{
return $this->data->count();
}
/**
* Get the "index" of the first item being paginated (1-indexed).
*
* @return int
*/
public function firstItem(): int
{
if ($this->total === 0) {
return 0;
}
return ($this->current - 1) * $this->perPage + 1;
}
/**
* Get the "index" of the last item being paginated (1-indexed).
*
* @return int
*/
public function lastItem(): int
{
if ($this->total === 0) {
return 0;
}
return $this->firstItem() + $this->count() - 1;
}
/**
* Get the pagination data as an array.
*
* @return array
*/
public function toArray(): array
{
return [
'current_page' => $this->current,
'data' => $this->data->toArray(),
'first_item' => $this->firstItem(),
'last_item' => $this->lastItem(),
'per_page' => $this->perPage,
'total' => $this->total,
'total_pages' => $this->totalPages(),
'next_page' => $this->hasNext() ? $this->next : null,
'previous_page' => $this->hasPrevious() ? $this->previous : null,
];
}
/**
* Convert the pagination to JSON.
*
* @param int $options
* @return string
*/
public function toJson(int $options = 0): string
{
return json_encode($this->toArray(), $options);
}
/**
* Build a URL for a specific page number.
*
* @param int $page
* @return string|null
*/
public function url(int $page): ?string
{
if ($this->baseUrl === null) {
return null;
}
if ($page < 1 || $page > $this->totalPages()) {
return null;
}
$params = array_merge($this->queryParams, [$this->pageParam => $page]);
$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
$separator = str_contains($this->baseUrl, '?') ? '&' : '?';
return $this->baseUrl . $separator . $query;
}
/**
* Get the URL for the next page.
*
* @return string|null
*/
public function nextPageUrl(): ?string
{
if (!$this->hasNext()) {
return null;
}
return $this->url($this->next);
}
/**
* Get the URL for the previous page.
*
* @return string|null
*/
public function previousPageUrl(): ?string
{
if (!$this->hasPrevious()) {
return null;
}
return $this->url($this->previous);
}
/**
* Get the URL for the first page.
*
* @return string|null
*/
public function firstPageUrl(): ?string
{
return $this->url(1);
}
/**
* Get the URL for the last page.
*
* @return string|null
*/
public function lastPageUrl(): ?string
{
return $this->url($this->totalPages());
}
/**
* Get an array of URLs for a range of pages.
*
* @param int $onEachSide Number of links on each side of current page
* @return array
*/
public function getUrlRange(int $onEachSide = 3): array
{
$totalPages = $this->totalPages();
$current = $this->current;
$start = max(1, $current - $onEachSide);
$end = min($totalPages, $current + $onEachSide);
$urls = [];
for ($page = $start; $page <= $end; $page++) {
$urls[$page] = $this->url($page);
}
return $urls;
}
/**
* Get pagination links data for rendering.
*
* @param int $onEachSide Number of links on each side of current page
* @return array
*/
public function links(int $onEachSide = 3): array
{
$totalPages = $this->totalPages();
$current = $this->current;
$links = [];
// Previous link
$links[] = [
'url' => $this->previousPageUrl(),
'label' => '« Previous',
'active' => false,
'disabled' => !$this->hasPrevious(),
];
// Page number links
$start = max(1, $current - $onEachSide);
$end = min($totalPages, $current + $onEachSide);
// Add first page and ellipsis if needed
if ($start > 1) {
$links[] = [
'url' => $this->url(1),
'label' => '1',
'active' => false,
'disabled' => false,
];
if ($start > 2) {
$links[] = [
'url' => null,
'label' => '...',
'active' => false,
'disabled' => true,
];
}
}
// Add page links
for ($page = $start; $page <= $end; $page++) {
$links[] = [
'url' => $this->url($page),
'label' => (string) $page,
'active' => $page === $current,
'disabled' => false,
];
}
// Add ellipsis and last page if needed
if ($end < $totalPages) {
if ($end < $totalPages - 1) {
$links[] = [
'url' => null,
'label' => '...',
'active' => false,
'disabled' => true,
];
}
$links[] = [
'url' => $this->url($totalPages),
'label' => (string) $totalPages,
'active' => false,
'disabled' => false,
];
}
// Next link
$links[] = [
'url' => $this->nextPageUrl(),
'label' => 'Next »',
'active' => false,
'disabled' => !$this->hasNext(),
];
return $links;
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return $this->data->offsetExists($offset);
}
/**
* Get an item at a given offset.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->data->offsetGet($offset);
}
/**
* Set the item at a given offset.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->data->offsetSet($offset, $value);
}
/**
* Unset the item at a given offset.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
$this->data->offsetUnset($offset);
}
/**
* Get an iterator for the items.
*
* @return Traversable
*/
public function getIterator(): Traversable
{
return $this->data->getIterator();
}
}