Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Pagination;
4
5 use Closure;
6 use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
7 use Illuminate\Contracts\Support\Htmlable;
8 use Illuminate\Support\Arr;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Traits\ForwardsCalls;
11 use Illuminate\Support\Traits\Tappable;
12 use Illuminate\Support\Traits\TransformsToResourceCollection;
13 use Stringable;
14 use Traversable;
15
16 /**
17 * @template TKey of array-key
18 *
19 * @template-covariant TValue
20 *
21 * @mixin \Illuminate\Support\Collection<TKey, TValue>
22 */
23 abstract class AbstractPaginator implements CanBeEscapedWhenCastToString, Htmlable, Stringable
24 {
25 use ForwardsCalls, Tappable, TransformsToResourceCollection;
26
27 /**
28 * All of the items being paginated.
29 *
30 * @var \Illuminate\Support\Collection<TKey, TValue>
31 */
32 protected $items;
33
34 /**
35 * The number of items to be shown per page.
36 *
37 * @var int
38 */
39 protected $perPage;
40
41 /**
42 * The current page being "viewed".
43 *
44 * @var int
45 */
46 protected $currentPage;
47
48 /**
49 * The base path to assign to all URLs.
50 *
51 * @var string
52 */
53 protected $path = '/';
54
55 /**
56 * The query parameters to add to all URLs.
57 *
58 * @var array
59 */
60 protected $query = [];
61
62 /**
63 * The URL fragment to add to all URLs.
64 *
65 * @var string|null
66 */
67 protected $fragment;
68
69 /**
70 * The query string variable used to store the page.
71 *
72 * @var string
73 */
74 protected $pageName = 'page';
75
76 /**
77 * Indicates that the paginator's string representation should be escaped when __toString is invoked.
78 *
79 * @var bool
80 */
81 protected $escapeWhenCastingToString = false;
82
83 /**
84 * The number of links to display on each side of current page link.
85 *
86 * @var int
87 */
88 public $onEachSide = 3;
89
90 /**
91 * The paginator options.
92 *
93 * @var array
94 */
95 protected $options;
96
97 /**
98 * The current path resolver callback.
99 *
100 * @var \Closure
101 */
102 protected static $currentPathResolver;
103
104 /**
105 * The current page resolver callback.
106 *
107 * @var \Closure
108 */
109 protected static $currentPageResolver;
110
111 /**
112 * The query string resolver callback.
113 *
114 * @var \Closure
115 */
116 protected static $queryStringResolver;
117
118 /**
119 * The view factory resolver callback.
120 *
121 * @var \Closure
122 */
123 protected static $viewFactoryResolver;
124
125 /**
126 * The default pagination view.
127 *
128 * @var string
129 */
130 public static $defaultView = 'pagination::tailwind';
131
132 /**
133 * The default "simple" pagination view.
134 *
135 * @var string
136 */
137 public static $defaultSimpleView = 'pagination::simple-tailwind';
138
139 /**
140 * Determine if the given value is a valid page number.
141 *
142 * @param int $page
143 * @return bool
144 */
145 protected function isValidPageNumber($page)
146 {
147 return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
148 }
149
150 /**
151 * Get the URL for the previous page.
152 *
153 * @return string|null
154 */
155 public function previousPageUrl()
156 {
157 if ($this->currentPage() > 1) {
158 return $this->url($this->currentPage() - 1);
159 }
160 }
161
162 /**
163 * Create a range of pagination URLs.
164 *
165 * @param int $start
166 * @param int $end
167 * @return array
168 */
169 public function getUrlRange($start, $end)
170 {
171 return Collection::range($start, $end)
172 ->mapWithKeys(fn ($page) => [$page => $this->url($page)])
173 ->all();
174 }
175
176 /**
177 * Get the URL for a given page number.
178 *
179 * @param int $page
180 * @return string
181 */
182 public function url($page)
183 {
184 if ($page <= 0) {
185 $page = 1;
186 }
187
188 // If we have any extra query string key / value pairs that need to be added
189 // onto the URL, we will put them in query string form and then attach it
190 // to the URL. This allows for extra information like sortings storage.
191 $parameters = [$this->pageName => $page];
192
193 if (count($this->query) > 0) {
194 $parameters = array_merge($this->query, $parameters);
195 }
196
197 return $this->path()
198 .(str_contains($this->path(), '?') ? '&' : '?')
199 .Arr::query($parameters)
200 .$this->buildFragment();
201 }
202
203 /**
204 * Get / set the URL fragment to be appended to URLs.
205 *
206 * @param string|null $fragment
207 * @return $this|string|null
208 */
209 public function fragment($fragment = null)
210 {
211 if (is_null($fragment)) {
212 return $this->fragment;
213 }
214
215 $this->fragment = $fragment;
216
217 return $this;
218 }
219
220 /**
221 * Add a set of query string values to the paginator.
222 *
223 * @param array|string|null $key
224 * @param string|null $value
225 * @return $this
226 */
227 public function appends($key, $value = null)
228 {
229 if (is_null($key)) {
230 return $this;
231 }
232
233 if (is_array($key)) {
234 return $this->appendArray($key);
235 }
236
237 return $this->addQuery($key, $value);
238 }
239
240 /**
241 * Add an array of query string values.
242 *
243 * @param array $keys
244 * @return $this
245 */
246 protected function appendArray(array $keys)
247 {
248 foreach ($keys as $key => $value) {
249 $this->addQuery($key, $value);
250 }
251
252 return $this;
253 }
254
255 /**
256 * Add all current query string values to the paginator.
257 *
258 * @return $this
259 */
260 public function withQueryString()
261 {
262 if (isset(static::$queryStringResolver)) {
263 return $this->appends(call_user_func(static::$queryStringResolver));
264 }
265
266 return $this;
267 }
268
269 /**
270 * Add a query string value to the paginator.
271 *
272 * @param string $key
273 * @param string $value
274 * @return $this
275 */
276 protected function addQuery($key, $value)
277 {
278 if ($key !== $this->pageName) {
279 $this->query[$key] = $value;
280 }
281
282 return $this;
283 }
284
285 /**
286 * Build the full fragment portion of a URL.
287 *
288 * @return string
289 */
290 protected function buildFragment()
291 {
292 return $this->fragment ? '#'.$this->fragment : '';
293 }
294
295 /**
296 * Load a set of relationships onto the mixed relationship collection.
297 *
298 * @param string $relation
299 * @param array $relations
300 * @return $this
301 */
302 public function loadMorph($relation, $relations)
303 {
304 $this->getCollection()->loadMorph($relation, $relations);
305
306 return $this;
307 }
308
309 /**
310 * Load a set of relationship counts onto the mixed relationship collection.
311 *
312 * @param string $relation
313 * @param array $relations
314 * @return $this
315 */
316 public function loadMorphCount($relation, $relations)
317 {
318 $this->getCollection()->loadMorphCount($relation, $relations);
319
320 return $this;
321 }
322
323 /**
324 * Get the slice of items being paginated.
325 *
326 * @return array<TKey, TValue>
327 */
328 public function items()
329 {
330 return $this->items->all();
331 }
332
333 /**
334 * Get the number of the first item in the slice.
335 *
336 * @return int|null
337 */
338 public function firstItem()
339 {
340 return count($this->items) > 0 ? ($this->currentPage - 1) * $this->perPage + 1 : null;
341 }
342
343 /**
344 * Get the number of the last item in the slice.
345 *
346 * @return int|null
347 */
348 public function lastItem()
349 {
350 return count($this->items) > 0 ? $this->firstItem() + $this->count() - 1 : null;
351 }
352
353 /**
354 * Transform each item in the slice of items using a callback.
355 *
356 * @template TMapValue
357 *
358 * @param callable(TValue, TKey): TMapValue $callback
359 * @return $this
360 *
361 * @phpstan-this-out static<TKey, TMapValue>
362 */
363 public function through(callable $callback)
364 {
365 $this->items->transform($callback);
366
367 return $this;
368 }
369
370 /**
371 * Get the number of items shown per page.
372 *
373 * @return int
374 */
375 public function perPage()
376 {
377 return $this->perPage;
378 }
379
380 /**
381 * Determine if there are enough items to split into multiple pages.
382 *
383 * @return bool
384 */
385 public function hasPages()
386 {
387 return $this->currentPage() != 1 || $this->hasMorePages();
388 }
389
390 /**
391 * Determine if the paginator is on the first page.
392 *
393 * @return bool
394 */
395 public function onFirstPage()
396 {
397 return $this->currentPage() <= 1;
398 }
399
400 /**
401 * Determine if the paginator is on the last page.
402 *
403 * @return bool
404 */
405 public function onLastPage()
406 {
407 return ! $this->hasMorePages();
408 }
409
410 /**
411 * Get the current page.
412 *
413 * @return int
414 */
415 public function currentPage()
416 {
417 return $this->currentPage;
418 }
419
420 /**
421 * Get the query string variable used to store the page.
422 *
423 * @return string
424 */
425 public function getPageName()
426 {
427 return $this->pageName;
428 }
429
430 /**
431 * Set the query string variable used to store the page.
432 *
433 * @param string $name
434 * @return $this
435 */
436 public function setPageName($name)
437 {
438 $this->pageName = $name;
439
440 return $this;
441 }
442
443 /**
444 * Set the base path to assign to all URLs.
445 *
446 * @param string $path
447 * @return $this
448 */
449 public function withPath($path)
450 {
451 return $this->setPath($path);
452 }
453
454 /**
455 * Set the base path to assign to all URLs.
456 *
457 * @param string $path
458 * @return $this
459 */
460 public function setPath($path)
461 {
462 $this->path = $path;
463
464 return $this;
465 }
466
467 /**
468 * Set the number of links to display on each side of current page link.
469 *
470 * @param int $count
471 * @return $this
472 */
473 public function onEachSide($count)
474 {
475 $this->onEachSide = $count;
476
477 return $this;
478 }
479
480 /**
481 * Get the base path for paginator generated URLs.
482 *
483 * @return string|null
484 */
485 public function path()
486 {
487 return $this->path;
488 }
489
490 /**
491 * Resolve the current request path or return the default value.
492 *
493 * @param string $default
494 * @return string
495 */
496 public static function resolveCurrentPath($default = '/')
497 {
498 if (isset(static::$currentPathResolver)) {
499 return call_user_func(static::$currentPathResolver);
500 }
501
502 return $default;
503 }
504
505 /**
506 * Set the current request path resolver callback.
507 *
508 * @param \Closure $resolver
509 * @return void
510 */
511 public static function currentPathResolver(Closure $resolver)
512 {
513 static::$currentPathResolver = $resolver;
514 }
515
516 /**
517 * Resolve the current page or return the default value.
518 *
519 * @param string $pageName
520 * @param int $default
521 * @return int
522 */
523 public static function resolveCurrentPage($pageName = 'page', $default = 1)
524 {
525 if (isset(static::$currentPageResolver)) {
526 return (int) call_user_func(static::$currentPageResolver, $pageName);
527 }
528
529 return $default;
530 }
531
532 /**
533 * Set the current page resolver callback.
534 *
535 * @param \Closure $resolver
536 * @return void
537 */
538 public static function currentPageResolver(Closure $resolver)
539 {
540 static::$currentPageResolver = $resolver;
541 }
542
543 /**
544 * Resolve the query string or return the default value.
545 *
546 * @param string|array|null $default
547 * @return string
548 */
549 public static function resolveQueryString($default = null)
550 {
551 if (isset(static::$queryStringResolver)) {
552 return (static::$queryStringResolver)();
553 }
554
555 return $default;
556 }
557
558 /**
559 * Set with query string resolver callback.
560 *
561 * @param \Closure $resolver
562 * @return void
563 */
564 public static function queryStringResolver(Closure $resolver)
565 {
566 static::$queryStringResolver = $resolver;
567 }
568
569 /**
570 * Get an instance of the view factory from the resolver.
571 *
572 * @return \Illuminate\Contracts\View\Factory
573 */
574 public static function viewFactory()
575 {
576 return call_user_func(static::$viewFactoryResolver);
577 }
578
579 /**
580 * Set the view factory resolver callback.
581 *
582 * @param \Closure $resolver
583 * @return void
584 */
585 public static function viewFactoryResolver(Closure $resolver)
586 {
587 static::$viewFactoryResolver = $resolver;
588 }
589
590 /**
591 * Set the default pagination view.
592 *
593 * @param string $view
594 * @return void
595 */
596 public static function defaultView($view)
597 {
598 static::$defaultView = $view;
599 }
600
601 /**
602 * Set the default "simple" pagination view.
603 *
604 * @param string $view
605 * @return void
606 */
607 public static function defaultSimpleView($view)
608 {
609 static::$defaultSimpleView = $view;
610 }
611
612 /**
613 * Indicate that Tailwind styling should be used for generated links.
614 *
615 * @return void
616 */
617 public static function useTailwind()
618 {
619 static::defaultView('pagination::tailwind');
620 static::defaultSimpleView('pagination::simple-tailwind');
621 }
622
623 /**
624 * Indicate that Bootstrap 4 styling should be used for generated links.
625 *
626 * @return void
627 */
628 public static function useBootstrap()
629 {
630 static::useBootstrapFour();
631 }
632
633 /**
634 * Indicate that Bootstrap 3 styling should be used for generated links.
635 *
636 * @return void
637 */
638 public static function useBootstrapThree()
639 {
640 static::defaultView('pagination::default');
641 static::defaultSimpleView('pagination::simple-default');
642 }
643
644 /**
645 * Indicate that Bootstrap 4 styling should be used for generated links.
646 *
647 * @return void
648 */
649 public static function useBootstrapFour()
650 {
651 static::defaultView('pagination::bootstrap-4');
652 static::defaultSimpleView('pagination::simple-bootstrap-4');
653 }
654
655 /**
656 * Indicate that Bootstrap 5 styling should be used for generated links.
657 *
658 * @return void
659 */
660 public static function useBootstrapFive()
661 {
662 static::defaultView('pagination::bootstrap-5');
663 static::defaultSimpleView('pagination::simple-bootstrap-5');
664 }
665
666 /**
667 * Get an iterator for the items.
668 *
669 * @return \ArrayIterator<TKey, TValue>
670 */
671 public function getIterator(): Traversable
672 {
673 return $this->items->getIterator();
674 }
675
676 /**
677 * Determine if the list of items is empty.
678 *
679 * @return bool
680 */
681 public function isEmpty()
682 {
683 return $this->items->isEmpty();
684 }
685
686 /**
687 * Determine if the list of items is not empty.
688 *
689 * @return bool
690 */
691 public function isNotEmpty()
692 {
693 return $this->items->isNotEmpty();
694 }
695
696 /**
697 * Get the number of items for the current page.
698 *
699 * @return int
700 */
701 public function count(): int
702 {
703 return $this->items->count();
704 }
705
706 /**
707 * Get the paginator's underlying collection.
708 *
709 * @return \Illuminate\Support\Collection<TKey, TValue>
710 */
711 public function getCollection()
712 {
713 return $this->items;
714 }
715
716 /**
717 * Set the paginator's underlying collection.
718 *
719 * @param \Illuminate\Support\Collection<TKey, TValue> $collection
720 * @return $this
721 */
722 public function setCollection(Collection $collection)
723 {
724 $this->items = $collection;
725
726 return $this;
727 }
728
729 /**
730 * Get the paginator options.
731 *
732 * @return array
733 */
734 public function getOptions()
735 {
736 return $this->options;
737 }
738
739 /**
740 * Determine if the given item exists.
741 *
742 * @param TKey $key
743 * @return bool
744 */
745 public function offsetExists($key): bool
746 {
747 return $this->items->has($key);
748 }
749
750 /**
751 * Get the item at the given offset.
752 *
753 * @param TKey $key
754 * @return TValue|null
755 */
756 public function offsetGet($key): mixed
757 {
758 return $this->items->get($key);
759 }
760
761 /**
762 * Set the item at the given offset.
763 *
764 * @param TKey|null $key
765 * @param TValue $value
766 * @return void
767 */
768 public function offsetSet($key, $value): void
769 {
770 $this->items->put($key, $value);
771 }
772
773 /**
774 * Unset the item at the given key.
775 *
776 * @param TKey $key
777 * @return void
778 */
779 public function offsetUnset($key): void
780 {
781 $this->items->forget($key);
782 }
783
784 /**
785 * Render the contents of the paginator to HTML.
786 *
787 * @return string
788 */
789 public function toHtml()
790 {
791 return (string) $this->render();
792 }
793
794 /**
795 * Make dynamic calls into the collection.
796 *
797 * @param string $method
798 * @param array $parameters
799 * @return mixed
800 */
801 public function __call($method, $parameters)
802 {
803 return $this->forwardCallTo($this->getCollection(), $method, $parameters);
804 }
805
806 /**
807 * Render the contents of the paginator when casting to a string.
808 *
809 * @return string
810 */
811 public function __toString()
812 {
813 return $this->escapeWhenCastingToString
814 ? e((string) $this->render())
815 : (string) $this->render();
816 }
817
818 /**
819 * Indicate that the paginator's string representation should be escaped when __toString is invoked.
820 *
821 * @param bool $escape
822 * @return $this
823 */
824 public function escapeWhenCastingToString($escape = true)
825 {
826 $this->escapeWhenCastingToString = $escape;
827
828 return $this;
829 }
830 }