LengthAwarePaginator.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Countable;
  4. use ArrayAccess;
  5. use JsonSerializable;
  6. use IteratorAggregate;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\HtmlString;
  9. use Illuminate\Contracts\Support\Jsonable;
  10. use Illuminate\Contracts\Support\Arrayable;
  11. use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
  12. class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract
  13. {
  14. /**
  15. * The total number of items before slicing.
  16. *
  17. * @var int
  18. */
  19. protected $total;
  20. /**
  21. * The last available page.
  22. *
  23. * @var int
  24. */
  25. protected $lastPage;
  26. /**
  27. * Create a new paginator instance.
  28. *
  29. * @param mixed $items
  30. * @param int $total
  31. * @param int $perPage
  32. * @param int|null $currentPage
  33. * @param array $options (path, query, fragment, pageName)
  34. * @return void
  35. */
  36. public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
  37. {
  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\Support\HtmlString
  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\Support\HtmlString
  77. */
  78. public function render($view = null, $data = [])
  79. {
  80. return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
  81. 'paginator' => $this,
  82. 'elements' => $this->elements(),
  83. ]))->render());
  84. }
  85. /**
  86. * Get the array of elements to pass to the view.
  87. *
  88. * @return array
  89. */
  90. protected function elements()
  91. {
  92. $window = UrlWindow::make($this);
  93. return array_filter([
  94. $window['first'],
  95. is_array($window['slider']) ? '...' : null,
  96. $window['slider'],
  97. is_array($window['last']) ? '...' : null,
  98. $window['last'],
  99. ]);
  100. }
  101. /**
  102. * Get the total number of items being paginated.
  103. *
  104. * @return int
  105. */
  106. public function total()
  107. {
  108. return $this->total;
  109. }
  110. /**
  111. * Determine if there are more items in the data source.
  112. *
  113. * @return bool
  114. */
  115. public function hasMorePages()
  116. {
  117. return $this->currentPage() < $this->lastPage();
  118. }
  119. /**
  120. * Get the URL for the next page.
  121. *
  122. * @return string|null
  123. */
  124. public function nextPageUrl()
  125. {
  126. if ($this->lastPage() > $this->currentPage()) {
  127. return $this->url($this->currentPage() + 1);
  128. }
  129. }
  130. /**
  131. * Get the last page.
  132. *
  133. * @return int
  134. */
  135. public function lastPage()
  136. {
  137. return $this->lastPage;
  138. }
  139. /**
  140. * Get the instance as an array.
  141. *
  142. * @return array
  143. */
  144. public function toArray()
  145. {
  146. return [
  147. 'current_page' => $this->currentPage(),
  148. 'data' => $this->items->toArray(),
  149. 'first_page_url' => $this->url(1),
  150. 'from' => $this->firstItem(),
  151. 'last_page' => $this->lastPage(),
  152. 'last_page_url' => $this->url($this->lastPage()),
  153. 'next_page_url' => $this->nextPageUrl(),
  154. 'path' => $this->path,
  155. 'per_page' => $this->perPage(),
  156. 'prev_page_url' => $this->previousPageUrl(),
  157. 'to' => $this->lastItem(),
  158. 'total' => $this->total(),
  159. ];
  160. }
  161. /**
  162. * Convert the object into something JSON serializable.
  163. *
  164. * @return array
  165. */
  166. public function jsonSerialize()
  167. {
  168. return $this->toArray();
  169. }
  170. /**
  171. * Convert the object to its JSON representation.
  172. *
  173. * @param int $options
  174. * @return string
  175. */
  176. public function toJson($options = 0)
  177. {
  178. return json_encode($this->jsonSerialize(), $options);
  179. }
  180. }