Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

Paginator::wrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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