|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DirectoryTree\ImapEngine\Pagination; |
| 4 | + |
| 5 | +use DirectoryTree\ImapEngine\Support\ForwardsCalls; |
| 6 | +use Illuminate\Contracts\Support\Arrayable; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use JsonSerializable; |
| 9 | + |
| 10 | +class LengthAwarePaginator implements Arrayable, JsonSerializable |
| 11 | +{ |
| 12 | + use ForwardsCalls; |
| 13 | + |
| 14 | + /** |
| 15 | + * Constructor. |
| 16 | + */ |
| 17 | + public function __construct( |
| 18 | + protected Collection $items, |
| 19 | + protected int $total, |
| 20 | + protected int $perPage, |
| 21 | + protected int $currentPage = 1, |
| 22 | + protected string $path = '', |
| 23 | + protected array $query = [], |
| 24 | + protected string $pageName = 'page', |
| 25 | + ) { |
| 26 | + $this->currentPage = max($currentPage, 1); |
| 27 | + |
| 28 | + $this->path = rtrim($path, '/'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Handle dynamic method calls on the paginator. |
| 33 | + */ |
| 34 | + public function __call(string $method, array $parameters): mixed |
| 35 | + { |
| 36 | + return $this->forwardCallTo($this->items, $method, $parameters); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the items being paginated. |
| 41 | + */ |
| 42 | + public function items(): Collection |
| 43 | + { |
| 44 | + return $this->items; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the total number of items. |
| 49 | + */ |
| 50 | + public function total(): int |
| 51 | + { |
| 52 | + return $this->total; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the number of items per page. |
| 57 | + */ |
| 58 | + public function perPage(): int |
| 59 | + { |
| 60 | + return $this->perPage; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the current page number. |
| 65 | + */ |
| 66 | + public function currentPage(): int |
| 67 | + { |
| 68 | + return $this->currentPage; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the last page (total pages). |
| 73 | + */ |
| 74 | + public function lastPage(): int |
| 75 | + { |
| 76 | + return (int) ceil($this->total / $this->perPage); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Determine if there are enough items to split into multiple pages. |
| 81 | + */ |
| 82 | + public function hasPages(): bool |
| 83 | + { |
| 84 | + return $this->total() > $this->perPage(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determine if there is a next page. |
| 89 | + */ |
| 90 | + public function hasMorePages(): bool |
| 91 | + { |
| 92 | + return $this->currentPage() < $this->lastPage(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Generate the URL for a given page. |
| 97 | + */ |
| 98 | + public function url(int $page): string |
| 99 | + { |
| 100 | + $params = array_merge($this->query, [$this->pageName => $page]); |
| 101 | + |
| 102 | + $queryString = http_build_query($params); |
| 103 | + |
| 104 | + return $this->path.($queryString ? '?'.$queryString : ''); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the URL for the next page, or null if none. |
| 109 | + */ |
| 110 | + public function nextPageUrl(): ?string |
| 111 | + { |
| 112 | + if ($this->hasMorePages()) { |
| 113 | + return $this->url($this->currentPage() + 1); |
| 114 | + } |
| 115 | + |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the URL for the previous page, or null if none. |
| 121 | + */ |
| 122 | + public function previousPageUrl(): ?string |
| 123 | + { |
| 124 | + if ($this->currentPage() > 1) { |
| 125 | + return $this->url($this->currentPage() - 1); |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Convert the pagination data to an array. |
| 133 | + */ |
| 134 | + public function toArray(): array |
| 135 | + { |
| 136 | + return [ |
| 137 | + 'path' => $this->path, |
| 138 | + 'total' => $this->total(), |
| 139 | + 'to' => $this->calculateTo(), |
| 140 | + 'per_page' => $this->perPage(), |
| 141 | + 'last_page' => $this->lastPage(), |
| 142 | + 'first_page_url' => $this->url(1), |
| 143 | + 'data' => $this->items()->toArray(), |
| 144 | + 'current_page' => $this->currentPage(), |
| 145 | + 'next_page_url' => $this->nextPageUrl(), |
| 146 | + 'prev_page_url' => $this->previousPageUrl(), |
| 147 | + 'last_page_url' => $this->url($this->lastPage()), |
| 148 | + 'from' => $this->total() ? ($this->currentPage() - 1) * $this->perPage() + 1 : null, |
| 149 | + ]; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Calculate the "to" index for the current page. |
| 154 | + */ |
| 155 | + protected function calculateTo(): ?int |
| 156 | + { |
| 157 | + if (! $this->total()) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + $to = $this->currentPage() * $this->perPage(); |
| 162 | + |
| 163 | + return min($to, $this->total()); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Convert the instance to JSON. |
| 168 | + */ |
| 169 | + public function jsonSerialize(): array |
| 170 | + { |
| 171 | + return $this->toArray(); |
| 172 | + } |
| 173 | +} |
0 commit comments