CursorPaginator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Paginator;
  12. use ArrayAccess;
  13. use Countable;
  14. use Hyperf\Collection\Collection;
  15. use Hyperf\Contract\Arrayable;
  16. use Hyperf\Contract\Jsonable;
  17. use Hyperf\Paginator\Contract\CursorPaginator as CursorPaginatorContract;
  18. use IteratorAggregate;
  19. use JsonSerializable;
  20. class CursorPaginator extends AbstractCursorPaginator implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable, CursorPaginatorContract
  21. {
  22. /**
  23. * Indicates whether there are more items in the data source.
  24. *
  25. * @return bool
  26. */
  27. protected bool $hasMore;
  28. /**
  29. * Create a new paginator instance.
  30. */
  31. public function __construct(mixed $items, int $perPage, ?Cursor $cursor = null, array $options = [])
  32. {
  33. $this->options = $options;
  34. foreach ($options as $key => $value) {
  35. $this->{$key} = $value;
  36. }
  37. $this->perPage = (int) $perPage;
  38. $this->cursor = $cursor;
  39. $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
  40. $this->setItems($items);
  41. }
  42. public function __toString(): string
  43. {
  44. return $this->toJson();
  45. }
  46. /**
  47. * Determine if there are more items in the data source.
  48. */
  49. public function hasMorePages(): bool
  50. {
  51. return (is_null($this->cursor) && $this->hasMore)
  52. || (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && $this->hasMore)
  53. || (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems());
  54. }
  55. /**
  56. * Determine if there are enough items to split into multiple pages.
  57. */
  58. public function hasPages(): bool
  59. {
  60. return ! $this->onFirstPage() || $this->hasMorePages();
  61. }
  62. /**
  63. * Determine if the paginator is on the first page.
  64. */
  65. public function onFirstPage(): bool
  66. {
  67. return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore);
  68. }
  69. /**
  70. * Determine if the paginator is on the last page.
  71. */
  72. public function onLastPage(): bool
  73. {
  74. return ! $this->hasMorePages();
  75. }
  76. /**
  77. * Get the instance as an array.
  78. */
  79. public function toArray(): array
  80. {
  81. return [
  82. 'data' => $this->items->toArray(),
  83. 'path' => $this->path(),
  84. 'per_page' => $this->perPage(),
  85. 'next_cursor' => $this->nextCursor()?->encode(),
  86. 'next_page_url' => $this->nextPageUrl(),
  87. 'prev_cursor' => $this->previousCursor()?->encode(),
  88. 'prev_page_url' => $this->previousPageUrl(),
  89. ];
  90. }
  91. /**
  92. * Convert the object into something JSON serializable.
  93. */
  94. public function jsonSerialize(): array
  95. {
  96. return $this->toArray();
  97. }
  98. /**
  99. * Convert the object to its JSON representation.
  100. */
  101. public function toJson(int $options = 0): string
  102. {
  103. return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
  104. }
  105. /**
  106. * Set the items for the paginator.
  107. * @param mixed $items
  108. */
  109. protected function setItems($items)
  110. {
  111. $this->items = $items instanceof Collection ? $items : Collection::make($items);
  112. $this->hasMore = $this->items->count() > $this->perPage;
  113. $this->items = $this->items->slice(0, $this->perPage);
  114. if (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()) {
  115. $this->items = $this->items->reverse()->values();
  116. }
  117. }
  118. }