Passed
Push — master ( 98205f...6f7a23 )
by Thierry
02:22
created

Paginator::getPageLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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