Debian Codegraph @

lib/3rdParty/composer/illuminate/pagination/LengthAwarePaginator.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Pagination;
4
5 use ArrayAccess;
6 use Countable;
7 use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
8 use Illuminate\Contracts\Support\Arrayable;
9 use Illuminate\Contracts\Support\Jsonable;
10 use Illuminate\Support\Collection;
11 use IteratorAggregate;
12 use JsonSerializable;
13
14 /**
15 * @template TKey of array-key
16 *
17 * @template-covariant TValue
18 *
19 * @extends AbstractPaginator<TKey, TValue>
20 *
21 * @implements Arrayable<TKey, TValue>
22 * @implements ArrayAccess<TKey, TValue>
23 * @implements IteratorAggregate<TKey, TValue>
24 * @implements LengthAwarePaginatorContract<TKey, TValue>
25 */
26 class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, LengthAwarePaginatorContract
27 {
28 /**
29 * The total number of items before slicing.
30 *
31 * @var int
32 */
33 protected $total;
34
35 /**
36 * The last available page.
37 *
38 * @var int
39 */
40 protected $lastPage;
41
42 /**
43 * Create a new paginator instance.
44 *
45 * @param Collection<TKey, TValue>|Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items
46 * @param int $total
47 * @param int $perPage
48 * @param int|null $currentPage
49 * @param array $options (path, query, fragment, pageName)
50 */
51 public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
52 {
53 $this->options = $options;
54
55 foreach ($options as $key => $value) {
56 $this->{$key} = $value;
57 }
58
59 $this->total = $total;
60 $this->perPage = (int) $perPage;
61 $this->lastPage = max((int) ceil($total / $perPage), 1);
62 $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
63 $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
64 $this->items = $items instanceof Collection ? $items : new Collection($items);
65 }
66
67 /**
68 * Get the current page for the request.
69 *
70 * @param int $currentPage
71 * @param string $pageName
72 * @return int
73 */
74 protected function setCurrentPage($currentPage, $pageName)
75 {
76 $currentPage = $currentPage ?: static::resolveCurrentPage($pageName);
77
78 return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
79 }
80
81 /**
82 * Render the paginator using the given view.
83 *
84 * @param string|null $view
85 * @param array $data
86 * @return \Illuminate\Contracts\Support\Htmlable
87 */
88 public function links($view = null, $data = [])
89 {
90 return $this->render($view, $data);
91 }
92
93 /**
94 * Render the paginator using the given view.
95 *
96 * @param string|null $view
97 * @param array $data
98 * @return \Illuminate\Contracts\Support\Htmlable
99 */
100 public function render($view = null, $data = [])
101 {
102 return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
103 'paginator' => $this,
104 'elements' => $this->elements(),
105 ]));
106 }
107
108 /**
109 * Get the paginator links as a collection (for JSON responses).
110 *
111 * @return \Illuminate\Support\Collection
112 */
113 public function linkCollection()
114 {
115 return (new Collection($this->elements()))->flatMap(function ($item) {
116 if (! is_array($item)) {
117 return [['url' => null, 'label' => '...', 'active' => false]];
118 }
119
120 return (new Collection($item))->map(function ($url, $page) {
121 return [
122 'url' => $url,
123 'label' => (string) $page,
124 'page' => $page,
125 'active' => $this->currentPage() === $page,
126 ];
127 });
128 })->prepend([
129 'url' => $this->previousPageUrl(),
130 'label' => function_exists('__') ? __('pagination.previous') : 'Previous',
131 'page' => $this->currentPage() > 1 ? $this->currentPage() - 1 : null,
132 'active' => false,
133 ])->push([
134 'url' => $this->nextPageUrl(),
135 'label' => function_exists('__') ? __('pagination.next') : 'Next',
136 'page' => $this->hasMorePages() ? $this->currentPage() + 1 : null,
137 'active' => false,
138 ]);
139 }
140
141 /**
142 * Get the array of elements to pass to the view.
143 *
144 * @return array
145 */
146 protected function elements()
147 {
148 $window = UrlWindow::make($this);
149
150 return array_filter([
151 $window['first'],
152 is_array($window['slider']) ? '...' : null,
153 $window['slider'],
154 is_array($window['last']) ? '...' : null,
155 $window['last'],
156 ]);
157 }
158
159 /**
160 * Get the total number of items being paginated.
161 *
162 * @return int
163 */
164 public function total()
165 {
166 return $this->total;
167 }
168
169 /**
170 * Determine if there are more items in the data source.
171 *
172 * @return bool
173 */
174 public function hasMorePages()
175 {
176 return $this->currentPage() < $this->lastPage();
177 }
178
179 /**
180 * Get the URL for the next page.
181 *
182 * @return string|null
183 */
184 public function nextPageUrl()
185 {
186 if ($this->hasMorePages()) {
187 return $this->url($this->currentPage() + 1);
188 }
189 }
190
191 /**
192 * Get the last page.
193 *
194 * @return int
195 */
196 public function lastPage()
197 {
198 return $this->lastPage;
199 }
200
201 /**
202 * Get the instance as an array.
203 *
204 * @return array
205 */
206 public function toArray()
207 {
208 return [
209 'current_page' => $this->currentPage(),
210 'data' => $this->items->toArray(),
211 'first_page_url' => $this->url(1),
212 'from' => $this->firstItem(),
213 'last_page' => $this->lastPage(),
214 'last_page_url' => $this->url($this->lastPage()),
215 'links' => $this->linkCollection()->toArray(),
216 'next_page_url' => $this->nextPageUrl(),
217 'path' => $this->path(),
218 'per_page' => $this->perPage(),
219 'prev_page_url' => $this->previousPageUrl(),
220 'to' => $this->lastItem(),
221 'total' => $this->total(),
222 ];
223 }
224
225 /**
226 * Convert the object into something JSON serializable.
227 *
228 * @return array
229 */
230 public function jsonSerialize(): array
231 {
232 return $this->toArray();
233 }
234
235 /**
236 * Convert the object to its JSON representation.
237 *
238 * @param int $options
239 * @return string
240 */
241 public function toJson($options = 0)
242 {
243 return json_encode($this->jsonSerialize(), $options);
244 }
245
246 /**
247 * Convert the object to pretty print formatted JSON.
248 *
249 * @params int $options
250 *
251 * @return string
252 */
253 public function toPrettyJson(int $options = 0)
254 {
255 return $this->toJson(JSON_PRETTY_PRINT | $options);
256 }
257 }