CursorPaginator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use ArrayAccess;
  4. use Countable;
  5. use Illuminate\Contracts\Pagination\CursorPaginator as PaginatorContract;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Contracts\Support\Jsonable;
  8. use Illuminate\Support\Collection;
  9. use IteratorAggregate;
  10. use JsonSerializable;
  11. class CursorPaginator extends AbstractCursorPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
  12. {
  13. /**
  14. * Indicates whether there are more items in the data source.
  15. *
  16. * @return bool
  17. */
  18. protected $hasMore;
  19. /**
  20. * Create a new paginator instance.
  21. *
  22. * @param mixed $items
  23. * @param int $perPage
  24. * @param \Illuminate\Pagination\Cursor|null $cursor
  25. * @param array $options (path, query, fragment, pageName)
  26. * @return void
  27. */
  28. public function __construct($items, $perPage, $cursor = null, array $options = [])
  29. {
  30. $this->options = $options;
  31. foreach ($options as $key => $value) {
  32. $this->{$key} = $value;
  33. }
  34. $this->perPage = (int) $perPage;
  35. $this->cursor = $cursor;
  36. $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
  37. $this->setItems($items);
  38. }
  39. /**
  40. * Set the items for the paginator.
  41. *
  42. * @param mixed $items
  43. * @return void
  44. */
  45. protected function setItems($items)
  46. {
  47. $this->items = $items instanceof Collection ? $items : Collection::make($items);
  48. $this->hasMore = $this->items->count() > $this->perPage;
  49. $this->items = $this->items->slice(0, $this->perPage);
  50. if (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()) {
  51. $this->items = $this->items->reverse()->values();
  52. }
  53. }
  54. /**
  55. * Render the paginator using the given view.
  56. *
  57. * @param string|null $view
  58. * @param array $data
  59. * @return \Illuminate\Contracts\Support\Htmlable
  60. */
  61. public function links($view = null, $data = [])
  62. {
  63. return $this->render($view, $data);
  64. }
  65. /**
  66. * Render the paginator using the given view.
  67. *
  68. * @param string|null $view
  69. * @param array $data
  70. * @return \Illuminate\Contracts\Support\Htmlable
  71. */
  72. public function render($view = null, $data = [])
  73. {
  74. return static::viewFactory()->make($view ?: Paginator::$defaultSimpleView, array_merge($data, [
  75. 'paginator' => $this,
  76. ]));
  77. }
  78. /**
  79. * Determine if there are more items in the data source.
  80. *
  81. * @return bool
  82. */
  83. public function hasMorePages()
  84. {
  85. return (is_null($this->cursor) && $this->hasMore) ||
  86. (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && $this->hasMore) ||
  87. (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems());
  88. }
  89. /**
  90. * Determine if there are enough items to split into multiple pages.
  91. *
  92. * @return bool
  93. */
  94. public function hasPages()
  95. {
  96. return ! $this->onFirstPage() || $this->hasMorePages();
  97. }
  98. /**
  99. * Determine if the paginator is on the first page.
  100. *
  101. * @return bool
  102. */
  103. public function onFirstPage()
  104. {
  105. return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore);
  106. }
  107. /**
  108. * Determine if the paginator is on the last page.
  109. *
  110. * @return bool
  111. */
  112. public function onLastPage()
  113. {
  114. return ! $this->hasMorePages();
  115. }
  116. /**
  117. * Get the instance as an array.
  118. *
  119. * @return array
  120. */
  121. public function toArray()
  122. {
  123. return [
  124. 'data' => $this->items->toArray(),
  125. 'path' => $this->path(),
  126. 'per_page' => $this->perPage(),
  127. 'next_cursor' => $this->nextCursor()?->encode(),
  128. 'next_page_url' => $this->nextPageUrl(),
  129. 'prev_cursor' => $this->previousCursor()?->encode(),
  130. 'prev_page_url' => $this->previousPageUrl(),
  131. ];
  132. }
  133. /**
  134. * Convert the object into something JSON serializable.
  135. *
  136. * @return array
  137. */
  138. public function jsonSerialize(): array
  139. {
  140. return $this->toArray();
  141. }
  142. /**
  143. * Convert the object to its JSON representation.
  144. *
  145. * @param int $options
  146. * @return string
  147. */
  148. public function toJson($options = 0)
  149. {
  150. return json_encode($this->jsonSerialize(), $options);
  151. }
  152. }