| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
abstract class AbstractPaginator implements CanBeEscapedWhenCastToString, Htmlable, Stringable |
| 24 |
{ |
| 25 |
use ForwardsCalls, Tappable, TransformsToResourceCollection; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected $items; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected $perPage; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected $currentPage; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $path = '/'; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
protected $query = []; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected $fragment; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected $pageName = 'page'; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
protected $escapeWhenCastingToString = false; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public $onEachSide = 3; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
protected $options; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
protected static $currentPathResolver; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected static $currentPageResolver; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected static $queryStringResolver; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected static $viewFactoryResolver; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static $defaultView = 'pagination::tailwind'; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static $defaultSimpleView = 'pagination::simple-tailwind'; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected function isValidPageNumber($page) |
| 146 |
{ |
| 147 |
return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function previousPageUrl() |
| 156 |
{ |
| 157 |
if ($this->currentPage() > 1) { |
| 158 |
return $this->url($this->currentPage() - 1); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function url($page) |
| 183 |
{ |
| 184 |
if ($page <= 0) { |
| 185 |
$page = 1; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 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 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 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 |
|
| 257 |
|
| 258 |
|
| 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 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 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 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
protected function buildFragment() |
| 291 |
{ |
| 292 |
return $this->fragment ? '#'.$this->fragment : ''; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function loadMorph($relation, $relations) |
| 303 |
{ |
| 304 |
$this->getCollection()->loadMorph($relation, $relations); |
| 305 |
|
| 306 |
return $this; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function loadMorphCount($relation, $relations) |
| 317 |
{ |
| 318 |
$this->getCollection()->loadMorphCount($relation, $relations); |
| 319 |
|
| 320 |
return $this; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function items() |
| 329 |
{ |
| 330 |
return $this->items->all(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function firstItem() |
| 339 |
{ |
| 340 |
return count($this->items) > 0 ? ($this->currentPage - 1) * $this->perPage + 1 : null; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public function lastItem() |
| 349 |
{ |
| 350 |
return count($this->items) > 0 ? $this->firstItem() + $this->count() - 1 : null; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function through(callable $callback) |
| 364 |
{ |
| 365 |
$this->items->transform($callback); |
| 366 |
|
| 367 |
return $this; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function perPage() |
| 376 |
{ |
| 377 |
return $this->perPage; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function hasPages() |
| 386 |
{ |
| 387 |
return $this->currentPage() != 1 || $this->hasMorePages(); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function onFirstPage() |
| 396 |
{ |
| 397 |
return $this->currentPage() <= 1; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public function onLastPage() |
| 406 |
{ |
| 407 |
return ! $this->hasMorePages(); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function currentPage() |
| 416 |
{ |
| 417 |
return $this->currentPage; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function getPageName() |
| 426 |
{ |
| 427 |
return $this->pageName; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function setPageName($name) |
| 437 |
{ |
| 438 |
$this->pageName = $name; |
| 439 |
|
| 440 |
return $this; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public function withPath($path) |
| 450 |
{ |
| 451 |
return $this->setPath($path); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function setPath($path) |
| 461 |
{ |
| 462 |
$this->path = $path; |
| 463 |
|
| 464 |
return $this; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function onEachSide($count) |
| 474 |
{ |
| 475 |
$this->onEachSide = $count; |
| 476 |
|
| 477 |
return $this; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public function path() |
| 486 |
{ |
| 487 |
return $this->path; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 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 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public static function currentPathResolver(Closure $resolver) |
| 512 |
{ |
| 513 |
static::$currentPathResolver = $resolver; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 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 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public static function currentPageResolver(Closure $resolver) |
| 539 |
{ |
| 540 |
static::$currentPageResolver = $resolver; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 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 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public static function queryStringResolver(Closure $resolver) |
| 565 |
{ |
| 566 |
static::$queryStringResolver = $resolver; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public static function viewFactory() |
| 575 |
{ |
| 576 |
return call_user_func(static::$viewFactoryResolver); |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
public static function viewFactoryResolver(Closure $resolver) |
| 586 |
{ |
| 587 |
static::$viewFactoryResolver = $resolver; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public static function defaultView($view) |
| 597 |
{ |
| 598 |
static::$defaultView = $view; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public static function defaultSimpleView($view) |
| 608 |
{ |
| 609 |
static::$defaultSimpleView = $view; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public static function useTailwind() |
| 618 |
{ |
| 619 |
static::defaultView('pagination::tailwind'); |
| 620 |
static::defaultSimpleView('pagination::simple-tailwind'); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public static function useBootstrap() |
| 629 |
{ |
| 630 |
static::useBootstrapFour(); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public static function useBootstrapThree() |
| 639 |
{ |
| 640 |
static::defaultView('pagination::default'); |
| 641 |
static::defaultSimpleView('pagination::simple-default'); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public static function useBootstrapFour() |
| 650 |
{ |
| 651 |
static::defaultView('pagination::bootstrap-4'); |
| 652 |
static::defaultSimpleView('pagination::simple-bootstrap-4'); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
public static function useBootstrapFive() |
| 661 |
{ |
| 662 |
static::defaultView('pagination::bootstrap-5'); |
| 663 |
static::defaultSimpleView('pagination::simple-bootstrap-5'); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
public function getIterator(): Traversable |
| 672 |
{ |
| 673 |
return $this->items->getIterator(); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
public function isEmpty() |
| 682 |
{ |
| 683 |
return $this->items->isEmpty(); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public function isNotEmpty() |
| 692 |
{ |
| 693 |
return $this->items->isNotEmpty(); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
public function count(): int |
| 702 |
{ |
| 703 |
return $this->items->count(); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public function getCollection() |
| 712 |
{ |
| 713 |
return $this->items; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
public function setCollection(Collection $collection) |
| 723 |
{ |
| 724 |
$this->items = $collection; |
| 725 |
|
| 726 |
return $this; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
public function getOptions() |
| 735 |
{ |
| 736 |
return $this->options; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
public function offsetExists($key): bool |
| 746 |
{ |
| 747 |
return $this->items->has($key); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
public function offsetGet($key): mixed |
| 757 |
{ |
| 758 |
return $this->items->get($key); |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public function offsetSet($key, $value): void |
| 769 |
{ |
| 770 |
$this->items->put($key, $value); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public function offsetUnset($key): void |
| 780 |
{ |
| 781 |
$this->items->forget($key); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
public function toHtml() |
| 790 |
{ |
| 791 |
return (string) $this->render(); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public function __call($method, $parameters) |
| 802 |
{ |
| 803 |
return $this->forwardCallTo($this->getCollection(), $method, $parameters); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public function __toString() |
| 812 |
{ |
| 813 |
return $this->escapeWhenCastingToString |
| 814 |
? e((string) $this->render()) |
| 815 |
: (string) $this->render(); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public function escapeWhenCastingToString($escape = true) |
| 825 |
{ |
| 826 |
$this->escapeWhenCastingToString = $escape; |
| 827 |
|
| 828 |
return $this; |
| 829 |
} |
| 830 |
} |