Passed
Push — v5.x ( 4cf44b...6eb5e2 )
by Thierry
02:02
created

Paginator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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