fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Pagination
/
LengthAwarePaginator.php
LengthAwarePaginator.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.4.01, at vendor/wpfluent/framework/src/WPFluent/Pagination/LengthAwarePaginator.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Pagination; |
| 4 | |
| 5 | use Countable; |
| 6 | use ArrayAccess; |
| 7 | use JsonSerializable; |
| 8 | use IteratorAggregate; |
| 9 | use FluentCommunity\Framework\Support\Collection; |
| 10 | use FluentCommunity\Framework\Support\JsonableInterface; |
| 11 | use FluentCommunity\Framework\Support\ArrayableInterface; |
| 12 | use FluentCommunity\Framework\Pagination\AbstractPaginator; |
| 13 | use FluentCommunity\Framework\Pagination\LengthAwarePaginatorInterface; |
| 14 | |
| 15 | class LengthAwarePaginator extends AbstractPaginator implements ArrayableInterface, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, JsonableInterface, LengthAwarePaginatorInterface |
| 16 | { |
| 17 | /** |
| 18 | * The total number of items before slicing. |
| 19 | * |
| 20 | * @var int |
| 21 | */ |
| 22 | protected $total; |
| 23 | |
| 24 | /** |
| 25 | * The last available page. |
| 26 | * |
| 27 | * @var int |
| 28 | */ |
| 29 | protected $lastPage; |
| 30 | |
| 31 | /** |
| 32 | * Create a new paginator instance. |
| 33 | * |
| 34 | * @param mixed $items |
| 35 | * @param int $total |
| 36 | * @param int $perPage |
| 37 | * @param int|null $currentPage |
| 38 | * @param array $options (path, query, fragment, pageName) |
| 39 | * @return void |
| 40 | */ |
| 41 | public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) |
| 42 | { |
| 43 | $this->options = $options; |
| 44 | |
| 45 | foreach ($options as $key => $value) { |
| 46 | $this->{$key} = $value; |
| 47 | } |
| 48 | |
| 49 | $this->total = $total; |
| 50 | $this->perPage = $perPage; |
| 51 | $this->lastPage = max((int) ceil($total / $perPage), 1); |
| 52 | $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; |
| 53 | $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName); |
| 54 | $this->items = $items instanceof Collection ? $items : Collection::make($items); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the current page for the request. |
| 59 | * |
| 60 | * @param int $currentPage |
| 61 | * @param string $pageName |
| 62 | * @return int |
| 63 | */ |
| 64 | protected function setCurrentPage($currentPage, $pageName) |
| 65 | { |
| 66 | $currentPage = $currentPage ?: static::resolveCurrentPage($pageName); |
| 67 | |
| 68 | return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the paginator links as a collection (for JSON responses). |
| 73 | * |
| 74 | * @return \FluentCommunity\Framework\Support\Collection |
| 75 | */ |
| 76 | public function linkCollection() |
| 77 | { |
| 78 | return Collection::make($this->elements())->flatMap(function ($item) { |
| 79 | if (! is_array($item)) { |
| 80 | return [['url' => null, 'label' => '...', 'active' => false]]; |
| 81 | } |
| 82 | |
| 83 | return Collection::make($item)->map(function ($url, $page) { |
| 84 | return [ |
| 85 | 'url' => $url, |
| 86 | 'label' => (string) $page, |
| 87 | 'active' => $this->currentPage() === $page, |
| 88 | ]; |
| 89 | }); |
| 90 | })->prepend([ |
| 91 | 'url' => $this->previousPageUrl(), |
| 92 | 'label' => function_exists('__') ? __('pagination.previous') : 'Previous', |
| 93 | 'active' => false, |
| 94 | ])->push([ |
| 95 | 'url' => $this->nextPageUrl(), |
| 96 | 'label' => function_exists('__') ? __('pagination.next') : 'Next', |
| 97 | 'active' => false, |
| 98 | ]); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the array of elements to pass to the view. |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | protected function elements() |
| 107 | { |
| 108 | $window = UrlWindow::make($this); |
| 109 | |
| 110 | return array_filter([ |
| 111 | $window['first'], |
| 112 | is_array($window['slider']) ? '...' : null, |
| 113 | $window['slider'], |
| 114 | is_array($window['last']) ? '...' : null, |
| 115 | $window['last'], |
| 116 | ]); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the total number of items being paginated. |
| 121 | * |
| 122 | * @return int |
| 123 | */ |
| 124 | public function total() |
| 125 | { |
| 126 | return $this->total; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Determine if there are more items in the data source. |
| 131 | * |
| 132 | * @return bool |
| 133 | */ |
| 134 | public function hasMorePages() |
| 135 | { |
| 136 | return $this->currentPage() < $this->lastPage(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the URL for the next page. |
| 141 | * |
| 142 | * @return string|null |
| 143 | */ |
| 144 | public function nextPageUrl() |
| 145 | { |
| 146 | if ($this->hasMorePages()) { |
| 147 | return $this->url($this->currentPage() + 1); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the last page. |
| 153 | * |
| 154 | * @return int |
| 155 | */ |
| 156 | public function lastPage() |
| 157 | { |
| 158 | return $this->lastPage; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the instance as an array. |
| 163 | * |
| 164 | * @return array |
| 165 | */ |
| 166 | public function toArray() |
| 167 | { |
| 168 | return [ |
| 169 | 'current_page' => $this->currentPage(), |
| 170 | 'data' => $this->items->toArray(), |
| 171 | 'first_page_url' => $this->url(1), |
| 172 | 'from' => $this->firstItem(), |
| 173 | 'last_page' => $this->lastPage(), |
| 174 | 'last_page_url' => $this->url($this->lastPage()), |
| 175 | 'links' => $this->linkCollection()->toArray(), |
| 176 | 'next_page_url' => $this->nextPageUrl(), |
| 177 | 'path' => $this->path(), |
| 178 | 'per_page' => $this->perPage(), |
| 179 | 'prev_page_url' => $this->previousPageUrl(), |
| 180 | 'to' => $this->lastItem(), |
| 181 | 'total' => $this->total(), |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Convert the object into something JSON serializable. |
| 187 | * |
| 188 | * @return array |
| 189 | */ |
| 190 | #[\ReturnTypeWillChange] |
| 191 | public function jsonSerialize() |
| 192 | { |
| 193 | return $this->toArray(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Convert the object to its JSON representation. |
| 198 | * |
| 199 | * @param int $options |
| 200 | * @return string |
| 201 | */ |
| 202 | public function toJson($options = 0) |
| 203 | { |
| 204 | return json_encode($this->jsonSerialize(), $options); |
| 205 | } |
| 206 | } |
| 207 |