Passed
Push — feature/code_improvement ( 374d0e )
by Thierry
41s
created

Paginator::getCurrentPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
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  an 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\Utils\Pagination;
42
43
use Jaxon\Request\Factory\Request;
44
use Jaxon\Request\Factory\Parameter;
45
46
class Paginator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Paginator
Loading history...
47
{
48
    /**
49
     * @var integer
50
     */
51
    protected $totalItems = 0;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
52
53
    /**
54
     * @var integer
55
     */
56
    protected $totalPages = 0;
57
58
    /**
59
     * @var integer
60
     */
61
    protected $itemsPerPage = 0;
62
63
    /**
64
     * @var integer
65
     */
66
    protected $currentPage = 0;
67
68
    /**
69
     * @var integer
70
     */
71
    protected $maxPagesToShow = 10;
72
73
    /**
74
     * The pagination renderer
75
     *
76
     * @var Renderer
77
     */
78
    protected $xRenderer = null;
79
80
    /**
81
     * The constructor
82
     *
83
     * @param Renderer $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     */
85
    public function __construct(Renderer $xRenderer)
86
    {
87
        $this->xRenderer = $xRenderer;
88
    }
89
90
    /**
91
     * Set the text for the previous page link
92
     *
93
     * @param string $text The text for the previous page link
94
     *
95
     * @return Paginator
96
     */
97
    public function setPreviousText($text)
98
    {
99
        $this->xRenderer->setPreviousText($text);
100
        return $this;
101
    }
102
103
    /**
104
     * Set the text for the next page link
105
     *
106
     * @param string $text The text for the previous page link
107
     *
108
     * @return Paginator
109
     */
110
    public function setNextText($text)
111
    {
112
        $this->xRenderer->setNextText($text);
113
        return $this;
114
    }
115
116
    /**
117
     * Update the number of pages
118
     *
119
     * @return Paginator
120
     */
121
    protected function updateTotalPages()
122
    {
123
        $this->totalPages = ($this->itemsPerPage == 0 ? 0 : (int)ceil($this->totalItems / $this->itemsPerPage));
124
        return $this;
125
    }
126
127
    /**
128
     * Set the max number of pages to show
129
     *
130
     * @param int $maxPagesToShow The max number of pages to show
131
     *
132
     * @return Paginator
133
     * @throws \InvalidArgumentException if $maxPagesToShow is less than 3.
134
     */
135
    public function setMaxPagesToShow($maxPagesToShow)
136
    {
137
        $this->maxPagesToShow = intval($maxPagesToShow);
138
        if($this->maxPagesToShow < 4)
139
        {
140
            $this->maxPagesToShow = 10;
141
            throw new \InvalidArgumentException('maxPagesToShow cannot be less than 4.');
142
        }
143
        return $this;
144
    }
145
146
    /**
147
     * Set the current page number
148
     *
149
     * @param int $currentPage The current page number
150
     *
151
     * @return Paginator
152
     */
153
    public function setCurrentPage($currentPage)
154
    {
155
        $this->currentPage = intval($currentPage);
156
        return $this;
157
    }
158
159
    /**
160
     * Set the number of items per page
161
     *
162
     * @param int $itemsPerPage The number of items per page
163
     *
164
     * @return Paginator
165
     */
166
    public function setItemsPerPage($itemsPerPage)
167
    {
168
        $this->itemsPerPage = intval($itemsPerPage);
169
        return $this->updateTotalPages();
170
    }
171
172
    /**
173
     * Set the total number of items
174
     *
175
     * @param int $totalItems The total number of items
176
     *
177
     * @return Paginator
178
     */
179
    public function setTotalItems($totalItems)
180
    {
181
        $this->totalItems = intval($totalItems);
182
        return $this->updateTotalPages();
183
    }
184
185
    /**
186
     * Setup the paginator
187
     *
188
     * @param int $totalItems The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
189
     * @param int $itemsPerPage The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
190
     * @param int $currentPage The current page number
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
191
     * @param Request $xRequest The request to be paginated
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
192
     *
193
     * @return Paginator
194
     */
195
    public function setup($totalItems, $itemsPerPage, $currentPage, $xRequest)
196
    {
197
        $this->setTotalItems($totalItems)
198
            ->setItemsPerPage($itemsPerPage)
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
199
            ->setCurrentPage($currentPage);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
200
        $this->xRenderer->setRequest($xRequest);
201
        return $this;
202
    }
203
204
    /**
205
     * Get an array of paginated page data.
206
     *
207
     * Example: [1, 0, 4, 5, 6, 0, 10]
208
     *
209
     * @return array
210
     */
211
    protected function getPageNumbers()
212
    {
213
        $pageNumbers = [];
214
215
        if($this->totalPages <= $this->maxPagesToShow)
216
        {
217
            for($i = 0; $i < $this->totalPages; $i++)
218
            {
219
                $pageNumbers[] = $i + 1;
220
            }
221
        }
222
        else
223
        {
224
            // Determine the sliding range, centered around the current page.
225
            $numAdjacents = (int)floor(($this->maxPagesToShow - 4) / 2);
226
227
            $slidingStart = 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...
228
            $slidingEndOffset = $numAdjacents + 3 - $this->currentPage;
229
            if($slidingEndOffset < 0)
230
            {
231
                $slidingStart = $this->currentPage - $numAdjacents;
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...
232
                $slidingEndOffset = 0;
233
            }
234
235
            $slidingEnd = $this->totalPages;
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...
236
            $slidingStartOffset = $this->currentPage + $numAdjacents + 2 - $this->totalPages;
237
            if($slidingStartOffset < 0)
238
            {
239
                $slidingEnd = $this->currentPage + $numAdjacents;
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...
240
                $slidingStartOffset = 0;
241
            }
242
243
            // Build the list of page numbers.
244
            if($slidingStart > 1)
245
            {
246
                $pageNumbers[] = 1;
247
                $pageNumbers[] = 0; // Ellipsys;
248
            }
249
            for($i = $slidingStart - $slidingStartOffset; $i <= $slidingEnd + $slidingEndOffset; $i++)
250
            {
251
                $pageNumbers[] = $i;
252
            }
253
            if($slidingEnd < $this->totalPages)
254
            {
255
                $pageNumbers[] = 0; // Ellipsys;
256
                $pageNumbers[] = $this->totalPages;
257
            }
258
        }
259
260
        return $pageNumbers;
261
    }
262
263
    /**
264
     * Render an HTML pagination control.
265
     *
266
     * @return string
267
     */
268
    public function render()
269
    {
270
        if($this->totalPages > 1)
271
        {
272
            return $this->xRenderer->render($this->getPageNumbers(), $this->currentPage, $this->totalPages);
273
        }
274
        return '';
275
    }
276
277
    /**
278
     * Render an HTML pagination control.
279
     *
280
     * @return string
281
     */
282
    public function __toString()
283
    {
284
        return $this->render();
285
    }
286
}
287