| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
abstract class AbstractCursorPaginator implements Htmlable, Stringable |
| 29 |
{ |
| 30 |
use ForwardsCalls, Tappable, TransformsToResourceCollection; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $items; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected $perPage; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected $path = '/'; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $query = []; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected $fragment; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected $cursorName = 'cursor'; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected $cursor; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
protected $parameters; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected $options; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
protected static $currentCursorResolver; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function url($cursor) |
| 109 |
{ |
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function getCursorForItem($item, $isNext = true) |
| 199 |
{ |
| 200 |
return new Cursor($this->getParametersForItem($item), $isNext); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 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 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 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 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 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 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 327 |
|
| 328 |
|
| 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 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 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 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
protected function buildFragment() |
| 361 |
{ |
| 362 |
return $this->fragment ? '#'.$this->fragment : ''; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function loadMorph($relation, $relations) |
| 373 |
{ |
| 374 |
$this->getCollection()->loadMorph($relation, $relations); |
| 375 |
|
| 376 |
return $this; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public function loadMorphCount($relation, $relations) |
| 387 |
{ |
| 388 |
$this->getCollection()->loadMorphCount($relation, $relations); |
| 389 |
|
| 390 |
return $this; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function items() |
| 399 |
{ |
| 400 |
return $this->items->all(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public function through(callable $callback) |
| 414 |
{ |
| 415 |
$this->items->transform($callback); |
| 416 |
|
| 417 |
return $this; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function perPage() |
| 426 |
{ |
| 427 |
return $this->perPage; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public function cursor() |
| 436 |
{ |
| 437 |
return $this->cursor; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public function getCursorName() |
| 446 |
{ |
| 447 |
return $this->cursorName; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public function setCursorName($name) |
| 457 |
{ |
| 458 |
$this->cursorName = $name; |
| 459 |
|
| 460 |
return $this; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public function withPath($path) |
| 470 |
{ |
| 471 |
return $this->setPath($path); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public function setPath($path) |
| 481 |
{ |
| 482 |
$this->path = $path; |
| 483 |
|
| 484 |
return $this; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function path() |
| 493 |
{ |
| 494 |
return $this->path; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 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 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public static function currentCursorResolver(Closure $resolver) |
| 520 |
{ |
| 521 |
static::$currentCursorResolver = $resolver; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public static function viewFactory() |
| 530 |
{ |
| 531 |
return Paginator::viewFactory(); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public function getIterator(): Traversable |
| 540 |
{ |
| 541 |
return $this->items->getIterator(); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public function isEmpty() |
| 550 |
{ |
| 551 |
return $this->items->isEmpty(); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public function isNotEmpty() |
| 560 |
{ |
| 561 |
return $this->items->isNotEmpty(); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public function count(): int |
| 570 |
{ |
| 571 |
return $this->items->count(); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public function getCollection() |
| 580 |
{ |
| 581 |
return $this->items; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public function setCollection(Collection $collection) |
| 596 |
{ |
| 597 |
$this->items = $collection; |
| 598 |
|
| 599 |
return $this; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public function getOptions() |
| 608 |
{ |
| 609 |
return $this->options; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
public function offsetExists($key): bool |
| 619 |
{ |
| 620 |
return $this->items->has($key); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public function offsetGet($key): mixed |
| 630 |
{ |
| 631 |
return $this->items->get($key); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public function offsetSet($key, $value): void |
| 642 |
{ |
| 643 |
$this->items->put($key, $value); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public function offsetUnset($key): void |
| 653 |
{ |
| 654 |
$this->items->forget($key); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function toHtml() |
| 663 |
{ |
| 664 |
return (string) $this->render(); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public function __call($method, $parameters) |
| 675 |
{ |
| 676 |
return $this->forwardCallTo($this->getCollection(), $method, $parameters); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public function __toString() |
| 685 |
{ |
| 686 |
return (string) $this->render(); |
| 687 |
} |
| 688 |
} |