Passed
Push — master ( 0602c9...8f96ba )
by Thierry
02:02
created

Renderer::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
dl 0
loc 50
rs 8.4444
c 0
b 0
f 0
cc 8
nc 34
nop 0
1
<?php
2
3
/**
4
 * Renderer.php - Paginator renderer
5
 *
6
 * Render pagination links.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Ui\Pagination;
16
17
use Jaxon\Request\Factory\Parameter;
18
use Jaxon\Request\Factory\Request;
19
use Jaxon\Ui\View\Renderer as ViewRenderer;
20
use Jaxon\Ui\View\Store;
21
22
class Renderer
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Renderer
Loading history...
23
{
24
    /**
25
     * The template renderer.
26
     *
27
     * Will be used to render HTML code for links.
28
     *
29
     * @var ViewRenderer
30
     */
31
    protected $xRenderer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
32
33
    /**
34
     * The Jaxon request to be paginated
35
     *
36
     * @var Request
37
     */
38
    protected $xRequest = null;
39
40
    /**
41
     * @var string
42
     */
43
    protected $previousText = '&laquo;';
44
45
    /**
46
     * @var string
47
     */
48
    protected $nextText = '&raquo;';
49
50
    /**
51
     * @var string
52
     */
53
    protected $ellipsysText = '...';
54
55
    /**
56
     * @var integer
57
     */
58
    protected $totalPages = 0;
59
60
    /**
61
     * @var integer
62
     */
63
    protected $currentPage = 0;
64
65
    /**
66
     * @var integer
67
     */
68
    protected $maxPagesToShow = 10;
69
70
    /**
71
     * The class contructor
72
     *
73
     * @param ViewRenderer          $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 10 found
Loading history...
74
     */
75
    public function __construct(ViewRenderer $xRenderer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
76
    {
77
        $this->xRenderer = $xRenderer;
78
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
79
80
    /**
81
     * Set the text for the previous page link
82
     *
83
     * @param string $text The text for the previous page link
84
     *
85
     * @return void
86
     */
87
    public function setPreviousText($text)
88
    {
89
        $this->previousText = $text;
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Set the text for the next page link
94
     *
95
     * @param string $text The text for the previous page link
96
     *
97
     * @return void
98
     */
99
    public function setNextText($text)
100
    {
101
        $this->nextText = $text;
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Set the request to be paginated
106
     *
107
     * @param Request $xRequest The request to be paginated
108
     *
109
     * @return void
110
     */
111
    public function setRequest(Request $xRequest)
112
    {
113
        $this->xRequest = $xRequest;
114
        // Append the page number to the parameter list, if not yet given.
115
        if(($this->xRequest) && !$this->xRequest->hasPageNumber())
116
        {
117
            $this->xRequest->addParameter(Parameter::PAGE_NUMBER, 0);
118
        }
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Set the current page number
123
     *
124
     * @param int $currentPage The current page number
125
     *
126
     * @return void
127
     */
128
    public function setCurrentPage($currentPage)
129
    {
130
        $this->currentPage = intval($currentPage);
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Set the max number of pages to show
135
     *
136
     * @param int $maxPagesToShow The max number of pages to show
137
     *
138
     * @return void
139
     */
140
    public function setMaxPagesToShow($maxPagesToShow)
141
    {
142
        $this->maxPagesToShow = intval($maxPagesToShow);
143
        if($this->maxPagesToShow < 4)
144
        {
145
            $this->maxPagesToShow = 4;
146
        }
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * Get the js call to a given page
151
     *
152
     * @param int $pageNum The page number
153
     *
154
     * @return string
155
     */
156
    protected function getPageCall($pageNum)
157
    {
158
        return $this->xRequest->setPageNumber($pageNum)->getScript();
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * Render a link to a page.
163
     *
164
     * @param string    $sTemplate      The template for the link to the page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
165
     * @param string    $sText          The text of the link if it is enabled
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 10 found
Loading history...
166
     * @param string    $sCall          The call of the link if it is enabled
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 10 found
Loading history...
167
     *
168
     * @return null|Store
169
     */
170
    protected function renderLink($sTemplate, $sText, $sCall)
171
    {
172
        return $this->xRenderer->render('pagination::links/' . $sTemplate, [
173
            'text' => $sText,
174
            'call' => $sCall,
175
        ]);
176
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
177
178
    /**
179
     * Get the previous page data.
180
     *
181
     * @return array
182
     */
183
    protected function getPrevLink()
184
    {
185
        if($this->currentPage <= 1)
186
        {
187
            return ['disabled', $this->previousText, ''];
188
        }
189
        return ['enabled', $this->previousText, $this->getPageCall($this->currentPage - 1)];
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Get the next page data.
194
     *
195
     * @return array
196
     */
197
    protected function getNextLink()
198
    {
199
        if($this->currentPage >= $this->totalPages)
200
        {
201
            return ['disabled', $this->nextText, ''];
202
        }
203
        return ['enabled', $this->nextText, $this->getPageCall($this->currentPage + 1)];
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * Get a page data.
208
     *
209
     * @param integer        $nNumber         The page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
210
     *
211
     * @return array
212
     */
213
    protected function getPageLink($nNumber)
214
    {
215
        if($nNumber < 1)
216
        {
217
            return ['disabled', $this->ellipsysText, ''];
218
        }
219
        $sTemplate = ($nNumber === $this->currentPage ? 'current' : 'enabled');
220
        return [$sTemplate, $nNumber, $this->getPageCall($nNumber)];
221
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
222
223
    /**
224
     * Get the array of page numbers to be printed.
225
     *
226
     * Example: [1, 0, 4, 5, 6, 0, 10]
227
     *
228
     * @return array
229
     */
230
    protected function getPageNumbers()
231
    {
232
        $pageNumbers = [];
233
234
        if($this->totalPages <= $this->maxPagesToShow)
235
        {
236
            for($i = 0; $i < $this->totalPages; $i++)
237
            {
238
                $pageNumbers[] = $i + 1;
239
            }
240
241
            return $pageNumbers;
242
        }
243
244
        // Determine the sliding range, centered around the current page.
245
        $numAdjacents = (int)floor(($this->maxPagesToShow - 4) / 2);
246
247
        $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...
248
        $slidingEndOffset = $numAdjacents + 3 - $this->currentPage;
249
        if($slidingEndOffset < 0)
250
        {
251
            $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...
252
            $slidingEndOffset = 0;
253
        }
254
255
        $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...
256
        $slidingStartOffset = $this->currentPage + $numAdjacents + 2 - $this->totalPages;
257
        if($slidingStartOffset < 0)
258
        {
259
            $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...
260
            $slidingStartOffset = 0;
261
        }
262
263
        // Build the list of page numbers.
264
        if($slidingStart > 1)
265
        {
266
            $pageNumbers[] = 1;
267
            $pageNumbers[] = 0; // Ellipsys;
268
        }
269
        for($i = $slidingStart - $slidingStartOffset; $i <= $slidingEnd + $slidingEndOffset; $i++)
270
        {
271
            $pageNumbers[] = $i;
272
        }
273
        if($slidingEnd < $this->totalPages)
274
        {
275
            $pageNumbers[] = 0; // Ellipsys;
276
            $pageNumbers[] = $this->totalPages;
277
        }
278
279
        return $pageNumbers;
280
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
281
282
    /**
283
     * Get the pages.
284
     *
285
     * @param integer   $totalPages         The total number of pages
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
286
     *
287
     * @return array
288
     */
289
    public function getPages($totalPages)
290
    {
291
        $this->totalPages = $totalPages;
292
293
        $aPageNumbers = $this->getPageNumbers();
294
        $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...
295
        array_walk($aPageNumbers, function($nNumber) use(&$aPages) {
296
            $aPages[] = $this->getPageLink($nNumber);
297
        });
298
        $aPages[] = $this->getNextLink();
299
300
        return $aPages;
301
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
302
303
    /**
304
     * Render an HTML pagination control.
305
     *
306
     * @param integer   $totalPages         The total number of pages
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
307
     *
308
     * @return null|Store
309
     */
310
    public function render($totalPages)
311
    {
312
        $aLinks = array_map(function($aPage) {
313
            return $this->renderLink($aPage[0], $aPage[1], $aPage[2]);
314
        }, $this->getPages($totalPages));
315
316
        $aPrevLink = array_shift($aLinks); // The first entry in the array
317
        $aNextLink = array_pop($aLinks); // The last entry in the array
318
        return $this->xRenderer->render('pagination::wrapper',
319
            ['links' => $aLinks, 'prev' => $aPrevLink, 'next' => $aNextLink]);
320
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
321
}
322