Paginator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use ArrayAccess;
  4. use Countable;
  5. use Illuminate\Contracts\Pagination\Paginator 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 Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
  12. {
  13. /**
  14. * Determine if 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 int|null $currentPage
  25. * @param array $options (path, query, fragment, pageName)
  26. * @return void
  27. */
  28. public function __construct($items, $perPage, $currentPage = null, array $options = [])
  29. {
  30. $this->options = $options;
  31. foreach ($options as $key => $value) {
  32. $this->{$key} = $value;
  33. }
  34. $this->perPage = $perPage;
  35. $this->currentPage = $this->setCurrentPage($currentPage);
  36. $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
  37. $this->setItems($items);
  38. }
  39. /**
  40. * Get the current page for the request.
  41. *
  42. * @param int $currentPage
  43. * @return int
  44. */
  45. protected function setCurrentPage($currentPage)
  46. {
  47. $currentPage = $currentPage ?: static::resolveCurrentPage();
  48. return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
  49. }
  50. /**
  51. * Set the items for the paginator.
  52. *
  53. * @param mixed $items
  54. * @return void
  55. */
  56. protected function setItems($items)
  57. {
  58. $this->items = $items instanceof Collection ? $items : Collection::make($items);
  59. $this->hasMore = $this->items->count() > $this->perPage;
  60. $this->items = $this->items->slice(0, $this->perPage);
  61. }
  62. /**
  63. * Get the URL for the next page.
  64. *
  65. * @return string|null
  66. */
  67. public function nextPageUrl()
  68. {
  69. if ($this->hasMorePages()) {
  70. return $this->url($this->currentPage() + 1);
  71. }
  72. }
  73. /**
  74. * Render the paginator using the given view.
  75. *
  76. * @param string|null $view
  77. * @param array $data
  78. * @return string
  79. */
  80. public function links($view = null, $data = [])
  81. {
  82. return $this->render($view, $data);
  83. }
  84. /**
  85. * Render the paginator using the given view.
  86. *
  87. * @param string|null $view
  88. * @param array $data
  89. * @return \Illuminate\Contracts\Support\Htmlable
  90. */
  91. public function render($view = null, $data = [])
  92. {
  93. return static::viewFactory()->make($view ?: static::$defaultSimpleView, array_merge($data, [
  94. 'paginator' => $this,
  95. ]));
  96. }
  97. /**
  98. * Manually indicate that the paginator does have more pages.
  99. *
  100. * @param bool $hasMore
  101. * @return $this
  102. */
  103. public function hasMorePagesWhen($hasMore = true)
  104. {
  105. $this->hasMore = $hasMore;
  106. return $this;
  107. }
  108. /**
  109. * Determine if there are more items in the data source.
  110. *
  111. * @return bool
  112. */
  113. public function hasMorePages()
  114. {
  115. return $this->hasMore;
  116. }
  117. /**
  118. * Get the instance as an array.
  119. *
  120. * @return array
  121. */
  122. public function toArray()
  123. {
  124. return [
  125. 'current_page' => $this->currentPage(),
  126. 'data' => $this->items->toArray(),
  127. 'first_page_url' => $this->url(1),
  128. 'from' => $this->firstItem(),
  129. 'next_page_url' => $this->nextPageUrl(),
  130. 'path' => $this->path(),
  131. 'per_page' => $this->perPage(),
  132. 'prev_page_url' => $this->previousPageUrl(),
  133. 'to' => $this->lastItem(),
  134. ];
  135. }
  136. /**
  137. * Convert the object into something JSON serializable.
  138. *
  139. * @return array
  140. */
  141. #[\ReturnTypeWillChange]
  142. public function jsonSerialize()
  143. {
  144. return $this->toArray();
  145. }
  146. /**
  147. * Convert the object to its JSON representation.
  148. *
  149. * @param int $options
  150. * @return string
  151. */
  152. public function toJson($options = 0)
  153. {
  154. return json_encode($this->jsonSerialize(), $options);
  155. }
  156. }