Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Pagination;
4
5 use ArrayAccess;
6 use Closure;
7 use Exception;
8 use Illuminate\Contracts\Support\Htmlable;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Database\Eloquent\Relations\Pivot;
11 use Illuminate\Http\Resources\Json\JsonResource;
12 use Illuminate\Support\Arr;
13 use Illuminate\Support\Collection;
14 use Illuminate\Support\Str;
15 use Illuminate\Support\Traits\ForwardsCalls;
16 use Illuminate\Support\Traits\Tappable;
17 use Illuminate\Support\Traits\TransformsToResourceCollection;
18 use Stringable;
19 use Traversable;
20
21 /**
22 * @template TKey of array-key
23 *
24 * @template-covariant TValue
25 *
26 * @mixin \Illuminate\Support\Collection<TKey, TValue>
27 */
28 abstract class AbstractCursorPaginator implements Htmlable, Stringable
29 {
30 use ForwardsCalls, Tappable, TransformsToResourceCollection;
31
32 /**
33 * All of the items being paginated.
34 *
35 * @var \Illuminate\Support\Collection<TKey, TValue>
36 */
37 protected $items;
38
39 /**
40 * The number of items to be shown per page.
41 *
42 * @var int
43 */
44 protected $perPage;
45
46 /**
47 * The base path to assign to all URLs.
48 *
49 * @var string
50 */
51 protected $path = '/';
52
53 /**
54 * The query parameters to add to all URLs.
55 *
56 * @var array
57 */
58 protected $query = [];
59
60 /**
61 * The URL fragment to add to all URLs.
62 *
63 * @var string|null
64 */
65 protected $fragment;
66
67 /**
68 * The cursor string variable used to store the page.
69 *
70 * @var string
71 */
72 protected $cursorName = 'cursor';
73
74 /**
75 * The current cursor.
76 *
77 * @var \Illuminate\Pagination\Cursor|null
78 */
79 protected $cursor;
80
81 /**
82 * The paginator parameters for the cursor.
83 *
84 * @var array
85 */
86 protected $parameters;
87
88 /**
89 * The paginator options.
90 *
91 * @var array
92 */
93 protected $options;
94
95 /**
96 * The current cursor resolver callback.
97 *
98 * @var \Closure
99 */
100 protected static $currentCursorResolver;
101
102 /**
103 * Get the URL for a given cursor.
104 *
105 * @param \Illuminate\Pagination\Cursor|null $cursor
106 * @return string
107 */
108 public function url($cursor)
109 {
110 // If we have any extra query string key / value pairs that need to be added
111 // onto the URL, we will put them in query string form and then attach it
112 // to the URL. This allows for extra information like sortings storage.
113 $parameters = is_null($cursor) ? [] : [$this->cursorName => $cursor->encode()];
114
115 if (count($this->query) > 0) {
116 $parameters = array_merge($this->query, $parameters);
117 }
118
119 return $this->path()
120 .(str_contains($this->path(), '?') ? '&' : '?')
121 .Arr::query($parameters)
122 .$this->buildFragment();
123 }
124
125 /**
126 * Get the URL for the previous page.
127 *
128 * @return string|null
129 */
130 public function previousPageUrl()
131 {
132 if (is_null($previousCursor = $this->previousCursor())) {
133 return null;
134 }
135
136 return $this->url($previousCursor);
137 }
138
139 /**
140 * The URL for the next page, or null.
141 *
142 * @return string|null
143 */
144 public function nextPageUrl()
145 {
146 if (is_null($nextCursor = $this->nextCursor())) {
147 return null;
148 }
149
150 return $this->url($nextCursor);
151 }
152
153 /**
154 * Get the "cursor" that points to the previous set of items.
155 *
156 * @return \Illuminate\Pagination\Cursor|null
157 */
158 public function previousCursor()
159 {
160 if (is_null($this->cursor) ||
161 ($this->cursor->pointsToPreviousItems() && ! $this->hasMore)) {
162 return null;
163 }
164
165 if ($this->items->isEmpty()) {
166 return null;
167 }
168
169 return $this->getCursorForItem($this->items->first(), false);
170 }
171
172 /**
173 * Get the "cursor" that points to the next set of items.
174 *
175 * @return \Illuminate\Pagination\Cursor|null
176 */
177 public function nextCursor()
178 {
179 if ((is_null($this->cursor) && ! $this->hasMore) ||
180 (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && ! $this->hasMore)) {
181 return null;
182 }
183
184 if ($this->items->isEmpty()) {
185 return null;
186 }
187
188 return $this->getCursorForItem($this->items->last(), true);
189 }
190
191 /**
192 * Get a cursor instance for the given item.
193 *
194 * @param \ArrayAccess|\stdClass $item
195 * @param bool $isNext
196 * @return \Illuminate\Pagination\Cursor
197 */
198 public function getCursorForItem($item, $isNext = true)
199 {
200 return new Cursor($this->getParametersForItem($item), $isNext);
201 }
202
203 /**
204 * Get the cursor parameters for a given object.
205 *
206 * @param \ArrayAccess|\stdClass $item
207 * @return array
208 *
209 * @throws \Exception
210 */
211 public function getParametersForItem($item)
212 {
213 return (new Collection($this->parameters))
214 ->filter()
215 ->flip()
216 ->map(function ($_, $parameterName) use ($item) {
217 if ($item instanceof JsonResource) {
218 $item = $item->resource;
219 }
220
221 if ($item instanceof Model &&
222 ! is_null($parameter = $this->getPivotParameterForItem($item, $parameterName))) {
223 return $parameter;
224 } elseif ($item instanceof ArrayAccess || is_array($item)) {
225 return $this->ensureParameterIsPrimitive(
226 $item[$parameterName] ?? $item[Str::afterLast($parameterName, '.')]
227 );
228 } elseif (is_object($item)) {
229 return $this->ensureParameterIsPrimitive(
230 $item->{$parameterName} ?? $item->{Str::afterLast($parameterName, '.')}
231 );
232 }
233
234 throw new Exception('Only arrays and objects are supported when cursor paginating items.');
235 })->toArray();
236 }
237
238 /**
239 * Get the cursor parameter value from a pivot model if applicable.
240 *
241 * @param \ArrayAccess|\stdClass $item
242 * @param string $parameterName
243 * @return string|null
244 */
245 protected function getPivotParameterForItem($item, $parameterName)
246 {
247 $table = Str::beforeLast($parameterName, '.');
248
249 foreach ($item->getRelations() as $relation) {
250 if ($relation instanceof Pivot && $relation->getTable() === $table) {
251 return $this->ensureParameterIsPrimitive(
252 $relation->getAttribute(Str::afterLast($parameterName, '.'))
253 );
254 }
255 }
256 }
257
258 /**
259 * Ensure the parameter is a primitive type.
260 *
261 * This can resolve issues that arise the developer uses a value object for an attribute.
262 *
263 * @param mixed $parameter
264 * @return mixed
265 */
266 protected function ensureParameterIsPrimitive($parameter)
267 {
268 return is_object($parameter) && method_exists($parameter, '__toString')
269 ? (string) $parameter
270 : $parameter;
271 }
272
273 /**
274 * Get / set the URL fragment to be appended to URLs.
275 *
276 * @param string|null $fragment
277 * @return $this|string|null
278 */
279 public function fragment($fragment = null)
280 {
281 if (is_null($fragment)) {
282 return $this->fragment;
283 }
284
285 $this->fragment = $fragment;
286
287 return $this;
288 }
289
290 /**
291 * Add a set of query string values to the paginator.
292 *
293 * @param array|string|null $key
294 * @param string|null $value
295 * @return $this
296 */
297 public function appends($key, $value = null)
298 {
299 if (is_null($key)) {
300 return $this;
301 }
302
303 if (is_array($key)) {
304 return $this->appendArray($key);
305 }
306
307 return $this->addQuery($key, $value);
308 }
309
310 /**
311 * Add an array of query string values.
312 *
313 * @param array $keys
314 * @return $this
315 */
316 protected function appendArray(array $keys)
317 {
318 foreach ($keys as $key => $value) {
319 $this->addQuery($key, $value);
320 }
321
322 return $this;
323 }
324
325 /**
326 * Add all current query string values to the paginator.
327 *
328 * @return $this
329 */
330 public function withQueryString()
331 {
332 if (! is_null($query = Paginator::resolveQueryString())) {
333 return $this->appends($query);
334 }
335
336 return $this;
337 }
338
339 /**
340 * Add a query string value to the paginator.
341 *
342 * @param string $key
343 * @param string $value
344 * @return $this
345 */
346 protected function addQuery($key, $value)
347 {
348 if ($key !== $this->cursorName) {
349 $this->query[$key] = $value;
350 }
351
352 return $this;
353 }
354
355 /**
356 * Build the full fragment portion of a URL.
357 *
358 * @return string
359 */
360 protected function buildFragment()
361 {
362 return $this->fragment ? '#'.$this->fragment : '';
363 }
364
365 /**
366 * Load a set of relationships onto the mixed relationship collection.
367 *
368 * @param string $relation
369 * @param array $relations
370 * @return $this
371 */
372 public function loadMorph($relation, $relations)
373 {
374 $this->getCollection()->loadMorph($relation, $relations);
375
376 return $this;
377 }
378
379 /**
380 * Load a set of relationship counts onto the mixed relationship collection.
381 *
382 * @param string $relation
383 * @param array $relations
384 * @return $this
385 */
386 public function loadMorphCount($relation, $relations)
387 {
388 $this->getCollection()->loadMorphCount($relation, $relations);
389
390 return $this;
391 }
392
393 /**
394 * Get the slice of items being paginated.
395 *
396 * @return array<TKey, TValue>
397 */
398 public function items()
399 {
400 return $this->items->all();
401 }
402
403 /**
404 * Transform each item in the slice of items using a callback.
405 *
406 * @template TThroughValue
407 *
408 * @param callable(TValue, TKey): TThroughValue $callback
409 * @return $this
410 *
411 * @phpstan-this-out static<TKey, TThroughValue>
412 */
413 public function through(callable $callback)
414 {
415 $this->items->transform($callback);
416
417 return $this;
418 }
419
420 /**
421 * Get the number of items shown per page.
422 *
423 * @return int
424 */
425 public function perPage()
426 {
427 return $this->perPage;
428 }
429
430 /**
431 * Get the current cursor being paginated.
432 *
433 * @return \Illuminate\Pagination\Cursor|null
434 */
435 public function cursor()
436 {
437 return $this->cursor;
438 }
439
440 /**
441 * Get the query string variable used to store the cursor.
442 *
443 * @return string
444 */
445 public function getCursorName()
446 {
447 return $this->cursorName;
448 }
449
450 /**
451 * Set the query string variable used to store the cursor.
452 *
453 * @param string $name
454 * @return $this
455 */
456 public function setCursorName($name)
457 {
458 $this->cursorName = $name;
459
460 return $this;
461 }
462
463 /**
464 * Set the base path to assign to all URLs.
465 *
466 * @param string $path
467 * @return $this
468 */
469 public function withPath($path)
470 {
471 return $this->setPath($path);
472 }
473
474 /**
475 * Set the base path to assign to all URLs.
476 *
477 * @param string $path
478 * @return $this
479 */
480 public function setPath($path)
481 {
482 $this->path = $path;
483
484 return $this;
485 }
486
487 /**
488 * Get the base path for paginator generated URLs.
489 *
490 * @return string|null
491 */
492 public function path()
493 {
494 return $this->path;
495 }
496
497 /**
498 * Resolve the current cursor or return the default value.
499 *
500 * @param string $cursorName
501 * @param \Illuminate\Pagination\Cursor|null $default
502 * @return \Illuminate\Pagination\Cursor|null
503 */
504 public static function resolveCurrentCursor($cursorName = 'cursor', $default = null)
505 {
506 if (isset(static::$currentCursorResolver)) {
507 return call_user_func(static::$currentCursorResolver, $cursorName);
508 }
509
510 return $default;
511 }
512
513 /**
514 * Set the current cursor resolver callback.
515 *
516 * @param \Closure $resolver
517 * @return void
518 */
519 public static function currentCursorResolver(Closure $resolver)
520 {
521 static::$currentCursorResolver = $resolver;
522 }
523
524 /**
525 * Get an instance of the view factory from the resolver.
526 *
527 * @return \Illuminate\Contracts\View\Factory
528 */
529 public static function viewFactory()
530 {
531 return Paginator::viewFactory();
532 }
533
534 /**
535 * Get an iterator for the items.
536 *
537 * @return \ArrayIterator<TKey, TValue>
538 */
539 public function getIterator(): Traversable
540 {
541 return $this->items->getIterator();
542 }
543
544 /**
545 * Determine if the list of items is empty.
546 *
547 * @return bool
548 */
549 public function isEmpty()
550 {
551 return $this->items->isEmpty();
552 }
553
554 /**
555 * Determine if the list of items is not empty.
556 *
557 * @return bool
558 */
559 public function isNotEmpty()
560 {
561 return $this->items->isNotEmpty();
562 }
563
564 /**
565 * Get the number of items for the current page.
566 *
567 * @return int
568 */
569 public function count(): int
570 {
571 return $this->items->count();
572 }
573
574 /**
575 * Get the paginator's underlying collection.
576 *
577 * @return \Illuminate\Support\Collection<TKey, TValue>
578 */
579 public function getCollection()
580 {
581 return $this->items;
582 }
583
584 /**
585 * Set the paginator's underlying collection.
586 *
587 * @template TSetKey of array-key
588 * @template TSetValue
589 *
590 * @param \Illuminate\Support\Collection<TSetKey, TSetValue> $collection
591 * @return $this
592 *
593 * @phpstan-this-out static<TSetKey, TSetValue>
594 */
595 public function setCollection(Collection $collection)
596 {
597 $this->items = $collection;
598
599 return $this;
600 }
601
602 /**
603 * Get the paginator options.
604 *
605 * @return array
606 */
607 public function getOptions()
608 {
609 return $this->options;
610 }
611
612 /**
613 * Determine if the given item exists.
614 *
615 * @param TKey $key
616 * @return bool
617 */
618 public function offsetExists($key): bool
619 {
620 return $this->items->has($key);
621 }
622
623 /**
624 * Get the item at the given offset.
625 *
626 * @param TKey $key
627 * @return TValue|null
628 */
629 public function offsetGet($key): mixed
630 {
631 return $this->items->get($key);
632 }
633
634 /**
635 * Set the item at the given offset.
636 *
637 * @param TKey|null $key
638 * @param TValue $value
639 * @return void
640 */
641 public function offsetSet($key, $value): void
642 {
643 $this->items->put($key, $value);
644 }
645
646 /**
647 * Unset the item at the given key.
648 *
649 * @param TKey $key
650 * @return void
651 */
652 public function offsetUnset($key): void
653 {
654 $this->items->forget($key);
655 }
656
657 /**
658 * Render the contents of the paginator to HTML.
659 *
660 * @return string
661 */
662 public function toHtml()
663 {
664 return (string) $this->render();
665 }
666
667 /**
668 * Make dynamic calls into the collection.
669 *
670 * @param string $method
671 * @param array $parameters
672 * @return mixed
673 */
674 public function __call($method, $parameters)
675 {
676 return $this->forwardCallTo($this->getCollection(), $method, $parameters);
677 }
678
679 /**
680 * Render the contents of the paginator when casting to a string.
681 *
682 * @return string
683 */
684 public function __toString()
685 {
686 return (string) $this->render();
687 }
688 }