Debian Codegraph @

lib/3rdParty/composer/illuminate/pagination/UrlWindow.php

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