Paginator.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Paginator as PaginatorContract;
  12. class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, PaginatorContract
  13. {
  14. /**
  15. * Determine if there are more items in the data source.
  16. *
  17. * @return bool
  18. */
  19. protected $hasMore;
  20. /**
  21. * Create a new paginator instance.
  22. *
  23. * @param mixed $items
  24. * @param int $perPage
  25. * @param int|null $currentPage
  26. * @param array $options (path, query, fragment, pageName)
  27. * @return void
  28. */
  29. public function __construct($items, $perPage, $currentPage = null, array $options = [])
  30. {
  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 string
  90. */
  91. public function render($view = null, $data = [])
  92. {
  93. return new HtmlString(
  94. static::viewFactory()->make($view ?: static::$defaultSimpleView, array_merge($data, [
  95. 'paginator' => $this,
  96. ]))->render()
  97. );
  98. }
  99. /**
  100. * Manually indicate that the paginator does have more pages.
  101. *
  102. * @param bool $hasMore
  103. * @return $this
  104. */
  105. public function hasMorePagesWhen($hasMore = true)
  106. {
  107. $this->hasMore = $hasMore;
  108. return $this;
  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->hasMore;
  118. }
  119. /**
  120. * Get the instance as an array.
  121. *
  122. * @return array
  123. */
  124. public function toArray()
  125. {
  126. return [
  127. 'current_page' => $this->currentPage(),
  128. 'data' => $this->items->toArray(),
  129. 'first_page_url' => $this->url(1),
  130. 'from' => $this->firstItem(),
  131. 'next_page_url' => $this->nextPageUrl(),
  132. 'path' => $this->path,
  133. 'per_page' => $this->perPage(),
  134. 'prev_page_url' => $this->previousPageUrl(),
  135. 'to' => $this->lastItem(),
  136. ];
  137. }
  138. /**
  139. * Convert the object into something JSON serializable.
  140. *
  141. * @return array
  142. */
  143. public function jsonSerialize()
  144. {
  145. return $this->toArray();
  146. }
  147. /**
  148. * Convert the object to its JSON representation.
  149. *
  150. * @param int $options
  151. * @return string
  152. */
  153. public function toJson($options = 0)
  154. {
  155. return json_encode($this->jsonSerialize(), $options);
  156. }
  157. }