UrlWindow.php 5.6 KB

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