Paginator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 IteratorAggregate;
  17. use JsonSerializable;
  18. use RuntimeException;
  19. class Paginator extends AbstractPaginator implements Arrayable, Countable, IteratorAggregate, JsonSerializable, Jsonable
  20. {
  21. /**
  22. * Determine if there are more items in the data source.
  23. */
  24. protected bool $hasMore;
  25. /**
  26. * Create a new paginator instance.
  27. *
  28. * @param array $options (path, query, fragment, pageName)
  29. * @param mixed $items
  30. */
  31. public function __construct($items, int $perPage, ?int $currentPage = null, array $options = [])
  32. {
  33. $this->options = $options;
  34. foreach ($options as $key => $value) {
  35. $this->{$key} = $value;
  36. }
  37. $this->perPage = $perPage;
  38. $this->currentPage = $this->setCurrentPage($currentPage);
  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. * Get the URL for the next page.
  48. */
  49. public function nextPageUrl(): ?string
  50. {
  51. if ($this->hasMorePages()) {
  52. return $this->url($this->currentPage() + 1);
  53. }
  54. return null;
  55. }
  56. /**
  57. * Render the paginator using the given view.
  58. */
  59. public function render(?string $view = null, array $data = []): string
  60. {
  61. if ($view) {
  62. throw new RuntimeException('WIP.');
  63. }
  64. return json_encode($data, 0);
  65. }
  66. /**
  67. * Manually indicate that the paginator does have more pages.
  68. */
  69. public function hasMorePagesWhen(bool $hasMore = true): self
  70. {
  71. $this->hasMore = $hasMore;
  72. return $this;
  73. }
  74. /**
  75. * Determine if there are more items in the data source.
  76. */
  77. public function hasMorePages(): bool
  78. {
  79. return $this->hasMore;
  80. }
  81. /**
  82. * Get the instance as an array.
  83. */
  84. public function toArray(): array
  85. {
  86. return [
  87. 'current_page' => $this->currentPage(),
  88. 'data' => $this->items->toArray(),
  89. 'first_page_url' => $this->url(1),
  90. 'from' => $this->firstItem(),
  91. 'next_page_url' => $this->nextPageUrl(),
  92. 'path' => $this->path,
  93. 'per_page' => $this->perPage(),
  94. 'prev_page_url' => $this->previousPageUrl(),
  95. 'to' => $this->lastItem(),
  96. ];
  97. }
  98. /**
  99. * Convert the object into something JSON serializable.
  100. */
  101. public function jsonSerialize(): mixed
  102. {
  103. return $this->toArray();
  104. }
  105. /**
  106. * Convert the object to its JSON representation.
  107. */
  108. public function toJson(int $options = 0): string
  109. {
  110. return json_encode($this->jsonSerialize(), $options);
  111. }
  112. /**
  113. * Get the current page for the request.
  114. */
  115. protected function setCurrentPage(?int $currentPage): int
  116. {
  117. $currentPage = $currentPage ?: static::resolveCurrentPage();
  118. return $this->isValidPageNumber($currentPage) ? $currentPage : 1;
  119. }
  120. /**
  121. * Set the items for the paginator.
  122. * @param mixed $items
  123. */
  124. protected function setItems($items): void
  125. {
  126. $this->items = $items instanceof Collection ? $items : Collection::make($items);
  127. $this->hasMore = $this->items->count() > $this->perPage;
  128. $this->items = $this->items->slice(0, $this->perPage);
  129. }
  130. }