LengthAwarePaginator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Countable;
  13. use Hyperf\Collection\Collection;
  14. use Hyperf\Contract\Arrayable;
  15. use Hyperf\Contract\Jsonable;
  16. use Hyperf\Contract\LengthAwarePaginatorInterface;
  17. use IteratorAggregate;
  18. use JsonSerializable;
  19. use RuntimeException;
  20. class LengthAwarePaginator extends AbstractPaginator implements Arrayable, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorInterface
  21. {
  22. /**
  23. * The total number of items before slicing.
  24. */
  25. protected int $total;
  26. /**
  27. * The last available page.
  28. */
  29. protected int $lastPage;
  30. /**
  31. * Create a new paginator instance.
  32. *
  33. * @param array $options (path, query, fragment, pageName)
  34. */
  35. public function __construct(mixed $items, int $total, int $perPage, ?int $currentPage = 1, array $options = [])
  36. {
  37. $this->options = $options;
  38. foreach ($options as $key => $value) {
  39. $this->{$key} = $value;
  40. }
  41. $this->total = $total;
  42. $this->perPage = $perPage;
  43. $this->lastPage = max((int) ceil($total / $perPage), 1);
  44. $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
  45. $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
  46. $this->items = $items instanceof Collection ? $items : Collection::make($items);
  47. }
  48. public function __toString(): string
  49. {
  50. return $this->toJson();
  51. }
  52. /**
  53. * Render the paginator using the given view.
  54. */
  55. public function links(?string $view = null, array $data = [])
  56. {
  57. return $this->render($view, $data);
  58. }
  59. /**
  60. * Render the paginator using the given view.
  61. */
  62. public function render(?string $view = null, array $data = []): string
  63. {
  64. if ($view) {
  65. throw new RuntimeException('WIP.');
  66. }
  67. return json_encode(array_merge($data, $this->items()), 0);
  68. }
  69. /**
  70. * Get the total number of items being paginated.
  71. */
  72. public function total(): int
  73. {
  74. return $this->total;
  75. }
  76. /**
  77. * Determine if there are more items in the data source.
  78. */
  79. public function hasMorePages(): bool
  80. {
  81. return $this->currentPage() < $this->lastPage();
  82. }
  83. /**
  84. * Get the URL for the next page.
  85. */
  86. public function nextPageUrl(): ?string
  87. {
  88. if ($this->lastPage() > $this->currentPage()) {
  89. return $this->url($this->currentPage() + 1);
  90. }
  91. return null;
  92. }
  93. /**
  94. * Get the last page.
  95. */
  96. public function lastPage(): int
  97. {
  98. return $this->lastPage;
  99. }
  100. /**
  101. * Get the instance as an array.
  102. */
  103. public function toArray(): array
  104. {
  105. return [
  106. 'current_page' => $this->currentPage(),
  107. 'data' => $this->items->toArray(),
  108. 'first_page_url' => $this->url(1),
  109. 'from' => $this->firstItem(),
  110. 'last_page' => $this->lastPage(),
  111. 'last_page_url' => $this->url($this->lastPage()),
  112. 'next_page_url' => $this->nextPageUrl(),
  113. 'path' => $this->path,
  114. 'per_page' => $this->perPage(),
  115. 'prev_page_url' => $this->previousPageUrl(),
  116. 'to' => $this->lastItem(),
  117. 'total' => $this->total(),
  118. ];
  119. }
  120. /**
  121. * Convert the object into something JSON serializable.
  122. */
  123. public function jsonSerialize(): mixed
  124. {
  125. return $this->toArray();
  126. }
  127. /**
  128. * Convert the object to its JSON representation.
  129. */
  130. public function toJson(int $options = 0): string
  131. {
  132. return json_encode($this->jsonSerialize(), $options);
  133. }
  134. /**
  135. * Get the current page for the request.
  136. */
  137. protected function setCurrentPage(?int $currentPage, string $pageName): int
  138. {
  139. $currentPage = $currentPage ?: static::resolveCurrentPage($pageName);
  140. return $this->isValidPageNumber($currentPage) ? $currentPage : 1;
  141. }
  142. /**
  143. * Get the array of elements to pass to the view.
  144. */
  145. protected function elements(): array
  146. {
  147. $window = UrlWindow::make($this);
  148. return array_filter([
  149. $window['first'],
  150. is_array($window['slider']) ? '...' : null,
  151. $window['slider'],
  152. is_array($window['last']) ? '...' : null,
  153. $window['last'],
  154. ]);
  155. }
  156. }