1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
3 | /* |
||
0 ignored issues
–
show
|
|||
4 | The MIT License (MIT) |
||
0 ignored issues
–
show
|
|||
5 | |||
6 | Copyright (c) 2014 Jason Grimes |
||
0 ignored issues
–
show
|
|||
7 | |||
8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
||
0 ignored issues
–
show
|
|||
9 | of this software and associated documentation files (the "Software"), to deal |
||
0 ignored issues
–
show
|
|||
10 | in the Software without restriction, including without limitation the rights |
||
0 ignored issues
–
show
|
|||
11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
0 ignored issues
–
show
|
|||
12 | copies of the Software, and to permit persons to whom the Software is |
||
0 ignored issues
–
show
|
|||
13 | furnished to do so, subject to the following conditions: |
||
0 ignored issues
–
show
|
|||
14 | |||
15 | The above copyright notice and this permission notice shall be included in all |
||
0 ignored issues
–
show
|
|||
16 | copies or substantial portions of the Software. |
||
0 ignored issues
–
show
|
|||
17 | |||
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
0 ignored issues
–
show
|
|||
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
0 ignored issues
–
show
|
|||
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
0 ignored issues
–
show
|
|||
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
0 ignored issues
–
show
|
|||
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
0 ignored issues
–
show
|
|||
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
0 ignored issues
–
show
|
|||
24 | SOFTWARE. |
||
0 ignored issues
–
show
|
|||
25 | */ |
||
26 | |||
27 | /** |
||
28 | * Paginator.php - Jaxon Paginator |
||
29 | * |
||
30 | * Create pagination links from a Jaxon request and a data array. |
||
31 | * |
||
32 | * @package jaxon-core |
||
33 | * @author Jason Grimes |
||
34 | * @author Thierry Feuzeu |
||
35 | * @copyright 2014 Jason Grimes |
||
36 | * @copyright 2016 Thierry Feuzeu |
||
37 | * @license https://opensource.org/licenses/MIT MIT License |
||
38 | * @link https://github.com/jaxon-php/jaxon-core |
||
39 | */ |
||
40 | |||
41 | namespace Jaxon\Request\Call; |
||
42 | |||
43 | use Jaxon\App\View\PaginationRenderer; |
||
44 | |||
45 | use function array_map; |
||
46 | use function array_walk; |
||
47 | use function ceil; |
||
48 | use function floor; |
||
49 | use function max; |
||
50 | |||
51 | class Paginator |
||
0 ignored issues
–
show
|
|||
52 | { |
||
53 | /** |
||
54 | * The Jaxon request to be paginated |
||
55 | * |
||
56 | * @var Call |
||
57 | */ |
||
58 | protected $xCall = null; |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | /** |
||
61 | * @var PaginationRenderer |
||
62 | */ |
||
63 | protected $xRenderer; |
||
64 | |||
65 | /** |
||
66 | * @var integer |
||
67 | */ |
||
68 | protected $nTotalItems = 0; |
||
69 | |||
70 | /** |
||
71 | * @var integer |
||
72 | */ |
||
73 | protected $nTotalPages = 0; |
||
74 | |||
75 | /** |
||
76 | * @var integer |
||
77 | */ |
||
78 | protected $nItemsPerPage = 0; |
||
79 | |||
80 | /** |
||
81 | * @var integer |
||
82 | */ |
||
83 | protected $nCurrentPage = 0; |
||
84 | |||
85 | /** |
||
86 | * @var integer |
||
87 | */ |
||
88 | protected $nMaxPages = 10; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $sPreviousText = '«'; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $sNextText = '»'; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $sEllipsysText = '...'; |
||
104 | |||
105 | /** |
||
106 | * The constructor. |
||
107 | * |
||
108 | * @param PaginationRenderer $xRenderer |
||
0 ignored issues
–
show
|
|||
109 | */ |
||
110 | public function __construct(PaginationRenderer $xRenderer) |
||
0 ignored issues
–
show
|
|||
111 | { |
||
112 | $this->xRenderer = $xRenderer; |
||
113 | } |
||
0 ignored issues
–
show
|
|||
114 | |||
115 | /** |
||
116 | * Set the text for the previous page link |
||
117 | * |
||
118 | * @param string $sText The text for the previous page link |
||
0 ignored issues
–
show
|
|||
119 | * |
||
120 | * @return Paginator |
||
121 | */ |
||
122 | public function setPreviousText(string $sText): Paginator |
||
123 | { |
||
124 | $this->sPreviousText = $sText; |
||
125 | return $this; |
||
126 | } |
||
0 ignored issues
–
show
|
|||
127 | |||
128 | /** |
||
129 | * Set the text for the next page link |
||
130 | * |
||
131 | * @param string $sText The text for the previous page link |
||
0 ignored issues
–
show
|
|||
132 | * |
||
133 | * @return Paginator |
||
134 | */ |
||
135 | public function setNextText(string $sText): Paginator |
||
136 | { |
||
137 | $this->sNextText = $sText; |
||
138 | return $this; |
||
139 | } |
||
0 ignored issues
–
show
|
|||
140 | |||
141 | /** |
||
142 | * Update the number of pages |
||
143 | * |
||
144 | * @return Paginator |
||
145 | */ |
||
146 | protected function updateTotalPages(): Paginator |
||
147 | { |
||
148 | $this->nTotalPages = ($this->nItemsPerPage === 0 ? 0 : (int)ceil($this->nTotalItems / $this->nItemsPerPage)); |
||
149 | return $this; |
||
150 | } |
||
0 ignored issues
–
show
|
|||
151 | |||
152 | /** |
||
153 | * Set the max number of pages to show |
||
154 | * |
||
155 | * @param int $nMaxPages The max number of pages to show |
||
0 ignored issues
–
show
|
|||
156 | * |
||
157 | * @return Paginator |
||
158 | */ |
||
159 | public function setMaxPages(int $nMaxPages): Paginator |
||
160 | { |
||
161 | $this->nMaxPages = max($nMaxPages, 4); |
||
162 | return $this; |
||
163 | } |
||
0 ignored issues
–
show
|
|||
164 | |||
165 | /** |
||
166 | * Set the current page number |
||
167 | * |
||
168 | * @param int $nCurrentPage The current page number |
||
0 ignored issues
–
show
|
|||
169 | * |
||
170 | * @return Paginator |
||
171 | */ |
||
172 | protected function setCurrentPage(int $nCurrentPage): Paginator |
||
173 | { |
||
174 | $this->nCurrentPage = $nCurrentPage; |
||
175 | return $this; |
||
176 | } |
||
0 ignored issues
–
show
|
|||
177 | |||
178 | /** |
||
179 | * Set the number of items per page |
||
180 | * |
||
181 | * @param int $nItemsPerPage The number of items per page |
||
0 ignored issues
–
show
|
|||
182 | * |
||
183 | * @return Paginator |
||
184 | */ |
||
185 | protected function setItemsPerPage(int $nItemsPerPage): Paginator |
||
186 | { |
||
187 | $this->nItemsPerPage = $nItemsPerPage; |
||
188 | return $this->updateTotalPages(); |
||
189 | } |
||
0 ignored issues
–
show
|
|||
190 | |||
191 | /** |
||
192 | * Set the total number of items |
||
193 | * |
||
194 | * @param int $nTotalItems The total number of items |
||
0 ignored issues
–
show
|
|||
195 | * |
||
196 | * @return Paginator |
||
197 | */ |
||
198 | protected function setTotalItems(int $nTotalItems): Paginator |
||
199 | { |
||
200 | $this->nTotalItems = $nTotalItems; |
||
201 | return $this->updateTotalPages(); |
||
202 | } |
||
0 ignored issues
–
show
|
|||
203 | |||
204 | /** |
||
205 | * Get the js call to a given page |
||
206 | * |
||
207 | * @param int $pageNum The page number |
||
0 ignored issues
–
show
|
|||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function getPageCall(int $pageNum): string |
||
212 | { |
||
213 | return $this->xCall->setPageNumber($pageNum)->getScript(); |
||
214 | } |
||
0 ignored issues
–
show
|
|||
215 | |||
216 | /** |
||
217 | * Get the previous page data. |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function getPrevLink(): array |
||
222 | { |
||
223 | if($this->nCurrentPage <= 1) |
||
224 | { |
||
225 | return ['disabled', $this->sPreviousText, '']; |
||
226 | } |
||
227 | return ['enabled', $this->sPreviousText, $this->getPageCall($this->nCurrentPage - 1)]; |
||
228 | } |
||
0 ignored issues
–
show
|
|||
229 | |||
230 | /** |
||
231 | * Get the next page data. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | protected function getNextLink(): array |
||
236 | { |
||
237 | if($this->nCurrentPage >= $this->nTotalPages) |
||
238 | { |
||
239 | return ['disabled', $this->sNextText, '']; |
||
240 | } |
||
241 | return ['enabled', $this->sNextText, $this->getPageCall($this->nCurrentPage + 1)]; |
||
242 | } |
||
0 ignored issues
–
show
|
|||
243 | |||
244 | /** |
||
245 | * Get a page data. |
||
246 | * |
||
247 | * @param integer $nNumber The page number |
||
0 ignored issues
–
show
|
|||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | protected function getPageLink(int $nNumber): array |
||
252 | { |
||
253 | if($nNumber < 1) |
||
254 | { |
||
255 | return ['disabled', $this->sEllipsysText, '']; |
||
256 | } |
||
257 | $sTemplate = ($nNumber === $this->nCurrentPage ? 'current' : 'enabled'); |
||
258 | return [$sTemplate, $nNumber, $this->getPageCall($nNumber)]; |
||
259 | } |
||
0 ignored issues
–
show
|
|||
260 | |||
261 | /** |
||
262 | * Get the array of page numbers to be printed. |
||
263 | * |
||
264 | * Example: [1, 0, 4, 5, 6, 0, 10] |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | protected function getPageNumbers(): array |
||
269 | { |
||
270 | $aPageNumbers = []; |
||
271 | |||
272 | if($this->nTotalPages <= $this->nMaxPages) |
||
273 | { |
||
274 | for($i = 0; $i < $this->nTotalPages; $i++) |
||
275 | { |
||
276 | $aPageNumbers[] = $i + 1; |
||
277 | } |
||
278 | |||
279 | return $aPageNumbers; |
||
280 | } |
||
281 | |||
282 | // Determine the sliding range, centered around the current page. |
||
283 | $nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2); |
||
284 | |||
285 | $nSlidingStart = 1; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
286 | $nSlidingEndOffset = $nNumAdjacents + 3 - $this->nCurrentPage; |
||
287 | if($nSlidingEndOffset < 0) |
||
288 | { |
||
289 | $nSlidingStart = $this->nCurrentPage - $nNumAdjacents; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
290 | $nSlidingEndOffset = 0; |
||
291 | } |
||
292 | |||
293 | $nSlidingEnd = $this->nTotalPages; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
294 | $nSlidingStartOffset = $this->nCurrentPage + $nNumAdjacents + 2 - $this->nTotalPages; |
||
295 | if($nSlidingStartOffset < 0) |
||
296 | { |
||
297 | $nSlidingEnd = $this->nCurrentPage + $nNumAdjacents; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
298 | $nSlidingStartOffset = 0; |
||
299 | } |
||
300 | |||
301 | // Build the list of page numbers. |
||
302 | if($nSlidingStart > 1) |
||
303 | { |
||
304 | $aPageNumbers[] = 1; |
||
305 | $aPageNumbers[] = 0; // Ellipsys; |
||
306 | } |
||
307 | for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
||
308 | { |
||
309 | $aPageNumbers[] = $i; |
||
310 | } |
||
311 | if($nSlidingEnd < $this->nTotalPages) |
||
312 | { |
||
313 | $aPageNumbers[] = 0; // Ellipsys; |
||
314 | $aPageNumbers[] = $this->nTotalPages; |
||
315 | } |
||
316 | |||
317 | return $aPageNumbers; |
||
318 | } |
||
0 ignored issues
–
show
|
|||
319 | |||
320 | /** |
||
321 | * Get the links (pages raw data). |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | public function links(): array |
||
326 | { |
||
327 | $aPageNumbers = $this->getPageNumbers(); |
||
328 | $aPages = [$this->getPrevLink()]; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
329 | array_walk($aPageNumbers, function($nNumber) use(&$aPages) { |
||
330 | $aPages[] = $this->getPageLink($nNumber); |
||
331 | }); |
||
332 | $aPages[] = $this->getNextLink(); |
||
333 | |||
334 | return $aPages; |
||
335 | } |
||
0 ignored issues
–
show
|
|||
336 | |||
337 | /** |
||
338 | * Get the pages. |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | public function pages(): array |
||
343 | { |
||
344 | if($this->nTotalPages < 2) |
||
345 | { |
||
346 | return []; |
||
347 | } |
||
348 | |||
349 | return array_map(function($aPage) { |
||
350 | return (object)['type' => $aPage[0], 'text' => $aPage[1], 'call' => $aPage[2]]; |
||
351 | }, $this->links()); |
||
352 | } |
||
0 ignored issues
–
show
|
|||
353 | |||
354 | /** |
||
355 | * Setup the paginator |
||
356 | * |
||
357 | * @param Call $xCall The call to be paginated |
||
0 ignored issues
–
show
|
|||
358 | * @param int $nCurrentPage The current page number |
||
0 ignored issues
–
show
|
|||
359 | * @param int $nItemsPerPage The number of items per page |
||
0 ignored issues
–
show
|
|||
360 | * @param int $nTotalItems The total number of items |
||
0 ignored issues
–
show
|
|||
361 | * |
||
362 | * @return Paginator |
||
363 | */ |
||
364 | public function setup(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator |
||
365 | { |
||
366 | $this->setCurrentPage($nCurrentPage)->setItemsPerPage($nItemsPerPage)->setTotalItems($nTotalItems); |
||
367 | $this->xCall = $xCall; |
||
368 | return $this; |
||
369 | } |
||
0 ignored issues
–
show
|
|||
370 | |||
371 | /** |
||
372 | * Render an HTML pagination control. |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | public function __toString() |
||
377 | { |
||
378 | if($this->nTotalPages < 2) |
||
379 | { |
||
380 | return ''; |
||
381 | } |
||
382 | return $this->xRenderer->render($this)->__toString(); |
||
383 | } |
||
0 ignored issues
–
show
|
|||
384 | } |
||
385 |