|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Support\Paginator; |
| 4 | + |
| 5 | +use Tempest\Support\Paginator\Exceptions\ArgumentWasInvalid; |
| 6 | + |
| 7 | +final class Paginator |
| 8 | +{ |
| 9 | + public function __construct( |
| 10 | + private(set) int $totalItems, |
| 11 | + private(set) int $itemsPerPage = 20, |
| 12 | + private(set) int $currentPage = 1, |
| 13 | + private(set) int $maxLinks = 10, |
| 14 | + ) { |
| 15 | + if ($this->totalItems < 0) { |
| 16 | + throw new ArgumentWasInvalid('Total items cannot be negative'); |
| 17 | + } |
| 18 | + |
| 19 | + if ($this->itemsPerPage <= 0) { |
| 20 | + throw new ArgumentWasInvalid('Items per page must be positive'); |
| 21 | + } |
| 22 | + |
| 23 | + if ($this->currentPage <= 0) { |
| 24 | + throw new ArgumentWasInvalid('Current page must be positive'); |
| 25 | + } |
| 26 | + |
| 27 | + if ($this->maxLinks <= 0) { |
| 28 | + throw new ArgumentWasInvalid('Max links must be positive'); |
| 29 | + } |
| 30 | + |
| 31 | + $this->currentPage = min(max(1, $this->currentPage), $this->totalPages); |
| 32 | + } |
| 33 | + |
| 34 | + public int $totalPages { |
| 35 | + get => max(1, (int) ceil($this->totalItems / $this->itemsPerPage)); |
| 36 | + } |
| 37 | + |
| 38 | + public int $offset { |
| 39 | + get => ($this->currentPage - 1) * $this->itemsPerPage; |
| 40 | + } |
| 41 | + |
| 42 | + public int $limit { |
| 43 | + get => $this->itemsPerPage; |
| 44 | + } |
| 45 | + |
| 46 | + public bool $hasNext { |
| 47 | + get => $this->currentPage < $this->totalPages; |
| 48 | + } |
| 49 | + |
| 50 | + public bool $hasPrevious { |
| 51 | + get => $this->currentPage > 1; |
| 52 | + } |
| 53 | + |
| 54 | + public ?int $nextPage { |
| 55 | + get => $this->hasNext ? ($this->currentPage + 1) : null; |
| 56 | + } |
| 57 | + |
| 58 | + public ?int $previousPage { |
| 59 | + get => $this->hasPrevious ? ($this->currentPage - 1) : null; |
| 60 | + } |
| 61 | + |
| 62 | + public ?int $firstPage { |
| 63 | + get => $this->totalPages > 0 ? 1 : null; |
| 64 | + } |
| 65 | + |
| 66 | + public ?int $lastPage { |
| 67 | + get => $this->totalPages > 0 ? $this->totalPages : null; |
| 68 | + } |
| 69 | + |
| 70 | + public array $pageRange { |
| 71 | + get => $this->calculatePageRange(); |
| 72 | + } |
| 73 | + |
| 74 | + public function withPage(int $page): self |
| 75 | + { |
| 76 | + return new self( |
| 77 | + totalItems: $this->totalItems, |
| 78 | + itemsPerPage: $this->itemsPerPage, |
| 79 | + currentPage: $page, |
| 80 | + maxLinks: $this->maxLinks, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function withItemsPerPage(int $itemsPerPage): self |
| 85 | + { |
| 86 | + return new self( |
| 87 | + totalItems: $this->totalItems, |
| 88 | + itemsPerPage: $itemsPerPage, |
| 89 | + currentPage: $this->currentPage, |
| 90 | + maxLinks: $this->maxLinks, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Creates paginated data with the provided items. |
| 96 | + * |
| 97 | + * @template T |
| 98 | + * @param array<T> $data |
| 99 | + * @return PaginatedData<T> |
| 100 | + */ |
| 101 | + public function paginate(array $data): PaginatedData |
| 102 | + { |
| 103 | + return new PaginatedData( |
| 104 | + data: $data, |
| 105 | + currentPage: $this->currentPage, |
| 106 | + totalPages: $this->totalPages, |
| 107 | + totalItems: $this->totalItems, |
| 108 | + itemsPerPage: $this->itemsPerPage, |
| 109 | + offset: $this->offset, |
| 110 | + limit: $this->limit, |
| 111 | + hasNext: $this->hasNext, |
| 112 | + hasPrevious: $this->hasPrevious, |
| 113 | + nextPage: $this->nextPage, |
| 114 | + previousPage: $this->previousPage, |
| 115 | + pageRange: $this->pageRange, |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Creates paginated data from a callable that fetches data. |
| 121 | + * |
| 122 | + * @template T |
| 123 | + * @param callable(int $limit, int $offset): array<T> $callback |
| 124 | + * @return PaginatedData<T> |
| 125 | + */ |
| 126 | + public function paginateWith(callable $callback): PaginatedData |
| 127 | + { |
| 128 | + return $this->paginate($callback($this->limit, $this->offset)); |
| 129 | + } |
| 130 | + |
| 131 | + private function calculatePageRange(): array |
| 132 | + { |
| 133 | + if ($this->totalPages <= $this->maxLinks) { |
| 134 | + return range(1, $this->totalPages); |
| 135 | + } |
| 136 | + |
| 137 | + $half = (int) floor($this->maxLinks / 2); |
| 138 | + $start = max(1, $this->currentPage - $half); |
| 139 | + $end = min($this->totalPages, ($start + $this->maxLinks) - 1); |
| 140 | + |
| 141 | + if ((($end - $start) + 1) < $this->maxLinks) { |
| 142 | + $start = max(1, ($end - $this->maxLinks) + 1); |
| 143 | + } |
| 144 | + |
| 145 | + return range($start, $end); |
| 146 | + } |
| 147 | +} |
0 commit comments