Passed
Push — master ( 2a458b...548923 )
by Thierry
07:20
created

Renderer::getLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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