LengthAwarePaginator.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use ArrayAccess;
  4. use Countable;
  5. use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
  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 LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, LengthAwarePaginatorContract
  12. {
  13. /**
  14. * The total number of items before slicing.
  15. *
  16. * @var int
  17. */
  18. protected $total;
  19. /**
  20. * The last available page.
  21. *
  22. * @var int
  23. */
  24. protected $lastPage;
  25. /**
  26. * Create a new paginator instance.
  27. *
  28. * @param mixed $items
  29. * @param int $total
  30. * @param int $perPage
  31. * @param int|null $currentPage
  32. * @param array $options (path, query, fragment, pageName)
  33. * @return void
  34. */
  35. public function __construct($items, $total, $perPage, $currentPage = null, 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. /**
  49. * Get the current page for the request.
  50. *
  51. * @param int $currentPage
  52. * @param string $pageName
  53. * @return int
  54. */
  55. protected function setCurrentPage($currentPage, $pageName)
  56. {
  57. $currentPage = $currentPage ?: static::resolveCurrentPage($pageName);
  58. return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
  59. }
  60. /**
  61. * Render the paginator using the given view.
  62. *
  63. * @param string|null $view
  64. * @param array $data
  65. * @return \Illuminate\Contracts\Support\Htmlable
  66. */
  67. public function links($view = null, $data = [])
  68. {
  69. return $this->render($view, $data);
  70. }
  71. /**
  72. * Render the paginator using the given view.
  73. *
  74. * @param string|null $view
  75. * @param array $data
  76. * @return \Illuminate\Contracts\Support\Htmlable
  77. */
  78. public function render($view = null, $data = [])
  79. {
  80. return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
  81. 'paginator' => $this,
  82. 'elements' => $this->elements(),
  83. ]));
  84. }
  85. /**
  86. * Get the paginator links as a collection (for JSON responses).
  87. *
  88. * @return \Illuminate\Support\Collection
  89. */
  90. public function linkCollection()
  91. {
  92. return collect($this->elements())->flatMap(function ($item) {
  93. if (! is_array($item)) {
  94. return [['url' => null, 'label' => '...', 'active' => false]];
  95. }
  96. return collect($item)->map(function ($url, $page) {
  97. return [
  98. 'url' => $url,
  99. 'label' => (string) $page,
  100. 'active' => $this->currentPage() === $page,
  101. ];
  102. });
  103. })->prepend([
  104. 'url' => $this->previousPageUrl(),
  105. 'label' => function_exists('__') ? __('pagination.previous') : 'Previous',
  106. 'active' => false,
  107. ])->push([
  108. 'url' => $this->nextPageUrl(),
  109. 'label' => function_exists('__') ? __('pagination.next') : 'Next',
  110. 'active' => false,
  111. ]);
  112. }
  113. /**
  114. * Get the array of elements to pass to the view.
  115. *
  116. * @return array
  117. */
  118. protected function elements()
  119. {
  120. $window = UrlWindow::make($this);
  121. return array_filter([
  122. $window['first'],
  123. is_array($window['slider']) ? '...' : null,
  124. $window['slider'],
  125. is_array($window['last']) ? '...' : null,
  126. $window['last'],
  127. ]);
  128. }
  129. /**
  130. * Get the total number of items being paginated.
  131. *
  132. * @return int
  133. */
  134. public function total()
  135. {
  136. return $this->total;
  137. }
  138. /**
  139. * Determine if there are more items in the data source.
  140. *
  141. * @return bool
  142. */
  143. public function hasMorePages()
  144. {
  145. return $this->currentPage() < $this->lastPage();
  146. }
  147. /**
  148. * Get the URL for the next page.
  149. *
  150. * @return string|null
  151. */
  152. public function nextPageUrl()
  153. {
  154. if ($this->hasMorePages()) {
  155. return $this->url($this->currentPage() + 1);
  156. }
  157. }
  158. /**
  159. * Get the last page.
  160. *
  161. * @return int
  162. */
  163. public function lastPage()
  164. {
  165. return $this->lastPage;
  166. }
  167. /**
  168. * Get the instance as an array.
  169. *
  170. * @return array
  171. */
  172. public function toArray()
  173. {
  174. return [
  175. 'current_page' => $this->currentPage(),
  176. 'data' => $this->items->toArray(),
  177. 'first_page_url' => $this->url(1),
  178. 'from' => $this->firstItem(),
  179. 'last_page' => $this->lastPage(),
  180. 'last_page_url' => $this->url($this->lastPage()),
  181. 'links' => $this->linkCollection()->toArray(),
  182. 'next_page_url' => $this->nextPageUrl(),
  183. 'path' => $this->path(),
  184. 'per_page' => $this->perPage(),
  185. 'prev_page_url' => $this->previousPageUrl(),
  186. 'to' => $this->lastItem(),
  187. 'total' => $this->total(),
  188. ];
  189. }
  190. /**
  191. * Convert the object into something JSON serializable.
  192. *
  193. * @return array
  194. */
  195. #[\ReturnTypeWillChange]
  196. public function jsonSerialize()
  197. {
  198. return $this->toArray();
  199. }
  200. /**
  201. * Convert the object to its JSON representation.
  202. *
  203. * @param int $options
  204. * @return string
  205. */
  206. public function toJson($options = 0)
  207. {
  208. return json_encode($this->jsonSerialize(), $options);
  209. }
  210. }