UrlWindow.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract;
  4. class UrlWindow
  5. {
  6. /**
  7. * The paginator implementation.
  8. *
  9. * @var \Illuminate\Contracts\Pagination\LengthAwarePaginator
  10. */
  11. protected $paginator;
  12. /**
  13. * Create a new URL window instance.
  14. *
  15. * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
  16. * @return void
  17. */
  18. public function __construct(PaginatorContract $paginator)
  19. {
  20. $this->paginator = $paginator;
  21. }
  22. /**
  23. * Create a new URL window instance.
  24. *
  25. * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
  26. * @param int $onEachSide
  27. * @return array
  28. */
  29. public static function make(PaginatorContract $paginator, $onEachSide = 3)
  30. {
  31. return (new static($paginator))->get($onEachSide);
  32. }
  33. /**
  34. * Get the window of URLs to be shown.
  35. *
  36. * @param int $onEachSide
  37. * @return array
  38. */
  39. public function get($onEachSide = 3)
  40. {
  41. if ($this->paginator->lastPage() < ($onEachSide * 2) + 6) {
  42. return $this->getSmallSlider();
  43. }
  44. return $this->getUrlSlider($onEachSide);
  45. }
  46. /**
  47. * Get the slider of URLs there are not enough pages to slide.
  48. *
  49. * @return array
  50. */
  51. protected function getSmallSlider()
  52. {
  53. return [
  54. 'first' => $this->paginator->getUrlRange(1, $this->lastPage()),
  55. 'slider' => null,
  56. 'last' => null,
  57. ];
  58. }
  59. /**
  60. * Create a URL slider links.
  61. *
  62. * @param int $onEachSide
  63. * @return array
  64. */
  65. protected function getUrlSlider($onEachSide)
  66. {
  67. $window = $onEachSide * 2;
  68. if (! $this->hasPages()) {
  69. return ['first' => null, 'slider' => null, 'last' => null];
  70. }
  71. // If the current page is very close to the beginning of the page range, we will
  72. // just render the beginning of the page range, followed by the last 2 of the
  73. // links in this list, since we will not have room to create a full slider.
  74. if ($this->currentPage() <= $window) {
  75. return $this->getSliderTooCloseToBeginning($window);
  76. }
  77. // If the current page is close to the ending of the page range we will just get
  78. // this first couple pages, followed by a larger window of these ending pages
  79. // since we're too close to the end of the list to create a full on slider.
  80. elseif ($this->currentPage() > ($this->lastPage() - $window)) {
  81. return $this->getSliderTooCloseToEnding($window);
  82. }
  83. // If we have enough room on both sides of the current page to build a slider we
  84. // will surround it with both the beginning and ending caps, with this window
  85. // of pages in the middle providing a Google style sliding paginator setup.
  86. return $this->getFullSlider($onEachSide);
  87. }
  88. /**
  89. * Get the slider of URLs when too close to beginning of window.
  90. *
  91. * @param int $window
  92. * @return array
  93. */
  94. protected function getSliderTooCloseToBeginning($window)
  95. {
  96. return [
  97. 'first' => $this->paginator->getUrlRange(1, $window + 2),
  98. 'slider' => null,
  99. 'last' => $this->getFinish(),
  100. ];
  101. }
  102. /**
  103. * Get the slider of URLs when too close to ending of window.
  104. *
  105. * @param int $window
  106. * @return array
  107. */
  108. protected function getSliderTooCloseToEnding($window)
  109. {
  110. $last = $this->paginator->getUrlRange(
  111. $this->lastPage() - ($window + 2),
  112. $this->lastPage()
  113. );
  114. return [
  115. 'first' => $this->getStart(),
  116. 'slider' => null,
  117. 'last' => $last,
  118. ];
  119. }
  120. /**
  121. * Get the slider of URLs when a full slider can be made.
  122. *
  123. * @param int $onEachSide
  124. * @return array
  125. */
  126. protected function getFullSlider($onEachSide)
  127. {
  128. return [
  129. 'first' => $this->getStart(),
  130. 'slider' => $this->getAdjacentUrlRange($onEachSide),
  131. 'last' => $this->getFinish(),
  132. ];
  133. }
  134. /**
  135. * Get the page range for the current page window.
  136. *
  137. * @param int $onEachSide
  138. * @return array
  139. */
  140. public function getAdjacentUrlRange($onEachSide)
  141. {
  142. return $this->paginator->getUrlRange(
  143. $this->currentPage() - $onEachSide,
  144. $this->currentPage() + $onEachSide
  145. );
  146. }
  147. /**
  148. * Get the starting URLs of a pagination slider.
  149. *
  150. * @return array
  151. */
  152. public function getStart()
  153. {
  154. return $this->paginator->getUrlRange(1, 2);
  155. }
  156. /**
  157. * Get the ending URLs of a pagination slider.
  158. *
  159. * @return array
  160. */
  161. public function getFinish()
  162. {
  163. return $this->paginator->getUrlRange(
  164. $this->lastPage() - 1,
  165. $this->lastPage()
  166. );
  167. }
  168. /**
  169. * Determine if the underlying paginator being presented has pages to show.
  170. *
  171. * @return bool
  172. */
  173. public function hasPages()
  174. {
  175. return $this->paginator->lastPage() > 1;
  176. }
  177. /**
  178. * Get the current page from the paginator.
  179. *
  180. * @return int
  181. */
  182. protected function currentPage()
  183. {
  184. return $this->paginator->currentPage();
  185. }
  186. /**
  187. * Get the last page from the paginator.
  188. *
  189. * @return int
  190. */
  191. protected function lastPage()
  192. {
  193. return $this->paginator->lastPage();
  194. }
  195. }