Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

Paginator::getPageNumbers()   B

Complexity

Conditions 8
Paths 34

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 50
rs 8.4444
cc 8
nc 34
nop 0
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\Plugin\Response\Paginator;
42
43
use function array_walk;
44
use function ceil;
45
use function floor;
46
use function max;
47
use function trim;
48
49
class Paginator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Paginator
Loading history...
50
{
51
    /**
52
     * @var integer
53
     */
54
    protected $nTotalItems = 0;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
55
56
    /**
57
     * @var integer
58
     */
59
    protected $nTotalPages = 0;
60
61
    /**
62
     * @var integer
63
     */
64
    protected $nItemsPerPage = 0;
65
66
    /**
67
     * @var integer
68
     */
69
    protected $nCurrentPage = 0;
70
71
    /**
72
     * @var integer
73
     */
74
    protected $nMaxPages = 10;
75
76
    /**
77
     * @var string
78
     */
79
    protected $sPreviousText = '&laquo;';
80
81
    /**
82
     * @var string
83
     */
84
    protected $sNextText = '&raquo;';
85
86
    /**
87
     * @var string
88
     */
89
    protected $sEllipsysText = '...';
90
91
    /**
92
     * @var string
93
     */
94
    protected $sWrapperId = '';
95
96
    /**
97
     * The constructor.
98
     *
99
     * @param int $nCurrentPage     The current page number
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
100
     * @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...
101
     * @param int $nTotalItems      The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
102
     */
103
    public function __construct(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
104
    {
105
        $this->setup($nCurrentPage, $nItemsPerPage, $nTotalItems);
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Setup the paginator
110
     *
111
     * @param int $nCurrentPage     The current page number
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
112
     * @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...
113
     * @param int $nTotalItems      The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
114
     *
115
     * @return Paginator
116
     */
117
    public function setup(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
118
    {
119
        return $this->setCurrentPage($nCurrentPage)
120
            ->setItemsPerPage($nItemsPerPage)
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
121
            ->setTotalItems($nTotalItems);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Set the text for the previous page link
126
     *
127
     * @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...
128
     *
129
     * @return Paginator
130
     */
131
    public function setPreviousText(string $sText): Paginator
132
    {
133
        $this->sPreviousText = $sText;
134
        return $this;
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * Set the text for the next page link
139
     *
140
     * @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...
141
     *
142
     * @return Paginator
143
     */
144
    public function setNextText(string $sText): Paginator
145
    {
146
        $this->sNextText = $sText;
147
        return $this;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Update the number of pages
152
     *
153
     * @return Paginator
154
     */
155
    protected function updateTotalPages(): Paginator
156
    {
157
        $this->nTotalPages = ($this->nItemsPerPage === 0 ? 0 :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
158
            (int)ceil($this->nTotalItems / $this->nItemsPerPage));
159
        return $this;
160
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
161
162
    /**
163
     * Set the max number of pages to show
164
     *
165
     * @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...
166
     *
167
     * @return Paginator
168
     */
169
    public function setMaxPages(int $nMaxPages): Paginator
170
    {
171
        $this->nMaxPages = max($nMaxPages, 4);
172
        return $this;
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
174
175
    /**
176
     * Set the current page number
177
     *
178
     * @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...
179
     *
180
     * @return Paginator
181
     */
182
    protected function setCurrentPage(int $nCurrentPage): Paginator
183
    {
184
        $this->nCurrentPage = $nCurrentPage;
185
        return $this;
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
189
     * Set the number of items per page
190
     *
191
     * @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...
192
     *
193
     * @return Paginator
194
     */
195
    protected function setItemsPerPage(int $nItemsPerPage): Paginator
196
    {
197
        $this->nItemsPerPage = $nItemsPerPage;
198
        return $this->updateTotalPages();
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
200
201
    /**
202
     * Set the total number of items
203
     *
204
     * @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...
205
     *
206
     * @return Paginator
207
     */
208
    protected function setTotalItems(int $nTotalItems): Paginator
209
    {
210
        $this->nTotalItems = $nTotalItems;
211
        return $this->updateTotalPages();
212
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
213
214
    /**
215
     * Get the previous page data.
216
     *
217
     * @return Page
218
     */
219
    protected function getPrevPage(): Page
220
    {
221
        return $this->nCurrentPage <= 1 ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
222
            new Page('disabled', $this->sPreviousText, 0) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
223
            new Page('enabled', $this->sPreviousText, $this->nCurrentPage - 1);
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Get the next page data.
228
     *
229
     * @return Page
230
     */
231
    protected function getNextPage(): Page
232
    {
233
        return $this->nCurrentPage >= $this->nTotalPages ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
234
            new Page('disabled', $this->sNextText, 0) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
235
            new Page('enabled', $this->sNextText, $this->nCurrentPage + 1);
236
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
237
238
    /**
239
     * Get a page data.
240
     *
241
     * @param integer $nNumber    The page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
242
     *
243
     * @return Page
244
     */
245
    protected function getPage(int $nNumber): Page
246
    {
247
        if($nNumber < 1)
248
        {
249
            return new Page('disabled', $this->sEllipsysText, 0);
250
        }
251
        $sType = ($nNumber === $this->nCurrentPage ? 'current' : 'enabled');
252
        return new Page($sType, "$nNumber", $nNumber);
253
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
254
255
    /**
256
     * Get the array of page numbers to be printed.
257
     *
258
     * Example: [1, 0, 4, 5, 6, 0, 10]
259
     *
260
     * @return array
261
     */
262
    protected function getPageNumbers(): array
263
    {
264
        $aPageNumbers = [];
265
266
        if($this->nTotalPages <= $this->nMaxPages)
267
        {
268
            for($i = 0; $i < $this->nTotalPages; $i++)
269
            {
270
                $aPageNumbers[] = $i + 1;
271
            }
272
273
            return $aPageNumbers;
274
        }
275
276
        // Determine the sliding range, centered around the current page.
277
        $nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2);
278
279
        $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...
280
        $nSlidingEndOffset = $nNumAdjacents + 3 - $this->nCurrentPage;
281
        if($nSlidingEndOffset < 0)
282
        {
283
            $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...
284
            $nSlidingEndOffset = 0;
285
        }
286
287
        $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...
288
        $nSlidingStartOffset = $this->nCurrentPage + $nNumAdjacents + 2 - $this->nTotalPages;
289
        if($nSlidingStartOffset < 0)
290
        {
291
            $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...
292
            $nSlidingStartOffset = 0;
293
        }
294
295
        // Build the list of page numbers.
296
        if($nSlidingStart > 1)
297
        {
298
            $aPageNumbers[] = 1;
299
            $aPageNumbers[] = 0; // Ellipsys;
300
        }
301
        for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++)
302
        {
303
            $aPageNumbers[] = $i;
304
        }
305
        if($nSlidingEnd < $this->nTotalPages)
306
        {
307
            $aPageNumbers[] = 0; // Ellipsys;
308
            $aPageNumbers[] = $this->nTotalPages;
309
        }
310
311
        return $aPageNumbers;
312
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
313
314
    /**
315
     * Get the links (pages raw data).
316
     *
317
     * @return array<Page>
318
     */
319
    public function pages(): array
320
    {
321
        if($this->nTotalPages < 2)
322
        {
323
            return [];
324
        }
325
326
        $aPageNumbers = $this->getPageNumbers();
327
        $aPages = [$this->getPrevPage()];
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->getPage($nNumber);
330
        });
331
        $aPages[] = $this->getNextPage();
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 pagination wrapper HTML
338
     *
339
     * @param string $sWrapperId        The pagination wrapper id
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 8 found
Loading history...
340
     *
341
     * @return string
342
     */
343
    public function wrapper(string $sWrapperId): string
344
    {
345
        $this->sWrapperId = trim($sWrapperId);
346
347
        return '<nav id="' . $this->sWrapperId . '"></nav>';
348
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
349
350
    /**
351
     * Get the pagination wrapper id
352
     *
353
     * @return string
354
     */
355
    public function wrapperId(): string
356
    {
357
        return $this->sWrapperId;
358
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
359
}
360