|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace IgniteKit\Backports\Pagination; |
| 4 | + |
| 5 | +use Countable; |
| 6 | +use ArrayAccess; |
| 7 | +use JsonSerializable; |
| 8 | +use IteratorAggregate; |
| 9 | +use IgniteKit\Backports\Support\Collection; |
| 10 | +use IgniteKit\Backports\Support\HtmlString; |
| 11 | +use IgniteKit\Backports\Contracts\Support\Jsonable; |
| 12 | +use IgniteKit\Backports\Contracts\Support\Arrayable; |
| 13 | +use IgniteKit\Backports\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; |
| 14 | + |
| 15 | +class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract |
| 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 | + * Render the paginator using the given view. |
| 73 | + * |
| 74 | + * @param string|null $view |
| 75 | + * @param array $data |
| 76 | + * @return \IgniteKit\Backports\Support\HtmlString |
| 77 | + */ |
| 78 | + public function links($view = null, $data = []) |
| 79 | + { |
| 80 | + return $this->render($view, $data); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Render the paginator using the given view. |
| 85 | + * |
| 86 | + * @param string|null $view |
| 87 | + * @param array $data |
| 88 | + * @return \IgniteKit\Backports\Support\HtmlString |
| 89 | + */ |
| 90 | + public function render($view = null, $data = []) |
| 91 | + { |
| 92 | + return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [ |
| 93 | + 'paginator' => $this, |
| 94 | + 'elements' => $this->elements(), |
| 95 | + ]))->render()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the array of elements to pass to the view. |
| 100 | + * |
| 101 | + * @return array |
| 102 | + */ |
| 103 | + protected function elements() |
| 104 | + { |
| 105 | + $window = UrlWindow::make($this); |
| 106 | + |
| 107 | + return array_filter([ |
| 108 | + $window['first'], |
| 109 | + is_array($window['slider']) ? '...' : null, |
| 110 | + $window['slider'], |
| 111 | + is_array($window['last']) ? '...' : null, |
| 112 | + $window['last'], |
| 113 | + ]); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the total number of items being paginated. |
| 118 | + * |
| 119 | + * @return int |
| 120 | + */ |
| 121 | + public function total() |
| 122 | + { |
| 123 | + return $this->total; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Determine if there are more items in the data source. |
| 128 | + * |
| 129 | + * @return bool |
| 130 | + */ |
| 131 | + public function hasMorePages() |
| 132 | + { |
| 133 | + return $this->currentPage() < $this->lastPage(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get the URL for the next page. |
| 138 | + * |
| 139 | + * @return string|null |
| 140 | + */ |
| 141 | + public function nextPageUrl() |
| 142 | + { |
| 143 | + if ($this->lastPage() > $this->currentPage()) { |
| 144 | + return $this->url($this->currentPage() + 1); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get the last page. |
| 150 | + * |
| 151 | + * @return int |
| 152 | + */ |
| 153 | + public function lastPage() |
| 154 | + { |
| 155 | + return $this->lastPage; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get the instance as an array. |
| 160 | + * |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + public function toArray() |
| 164 | + { |
| 165 | + return [ |
| 166 | + 'current_page' => $this->currentPage(), |
| 167 | + 'data' => $this->items->toArray(), |
| 168 | + 'first_page_url' => $this->url(1), |
| 169 | + 'from' => $this->firstItem(), |
| 170 | + 'last_page' => $this->lastPage(), |
| 171 | + 'last_page_url' => $this->url($this->lastPage()), |
| 172 | + 'next_page_url' => $this->nextPageUrl(), |
| 173 | + 'path' => $this->path, |
| 174 | + 'per_page' => $this->perPage(), |
| 175 | + 'prev_page_url' => $this->previousPageUrl(), |
| 176 | + 'to' => $this->lastItem(), |
| 177 | + 'total' => $this->total(), |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Convert the object into something JSON serializable. |
| 183 | + * |
| 184 | + * @return array |
| 185 | + */ |
| 186 | + public function jsonSerialize() |
| 187 | + { |
| 188 | + return $this->toArray(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Convert the object to its JSON representation. |
| 193 | + * |
| 194 | + * @param int $options |
| 195 | + * @return string |
| 196 | + */ |
| 197 | + public function toJson($options = 0) |
| 198 | + { |
| 199 | + return json_encode($this->jsonSerialize(), $options); |
| 200 | + } |
| 201 | +} |
0 commit comments