Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

Renderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
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 the previous link.
164
     *
165
     * @param integer   $nNumber        The page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter name; 8 found
Loading history...
166
     * @param string    $sTemplate      The template for the call to the page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
167
     * @param string    $sEnabledText   The text of the link if it is enabled
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 3 found
Loading history...
168
     * @param string    $sDisabledText  The text of the link if it is disabled
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
169
     *
170
     * @return null|Store
171
     */
172
    protected function getLink($nNumber, $sTemplate, $sEnabledText, $sDisabledText)
173
    {
174
        if($nNumber > 0)
175
        {
176
            return $this->xRenderer->render('pagination::links/' . $sTemplate, [
177
                'text' => $sEnabledText,
178
                'call' => $this->getPageCall($nNumber),
179
            ]);
180
        }
181
        return $this->xRenderer->render('pagination::links/disabled', ['text' => $sDisabledText]);
182
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
183
184
    /**
185
     * Render the previous link.
186
     *
187
     * @return null|Store
188
     */
189
    protected function getPrevLink()
190
    {
191
        $nNumber = ($this->currentPage > 1 ? $this->currentPage - 1 : 0);
192
        return $this->getLink($nNumber, 'prev', $this->previousText, $this->previousText);
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
194
195
    /**
196
     * Render the next link.
197
     *
198
     * @return null|Store
199
     */
200
    protected function getNextLink()
201
    {
202
        $nNumber = ($this->currentPage < $this->totalPages ? $this->currentPage + 1 : 0);
203
        return $this->getLink($nNumber, 'next', $this->nextText, $this->nextText);
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * Render the pagination links.
208
     *
209
     * @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...
210
     *
211
     * @return null|Store
212
     */
213
    protected function getPageLink($nNumber)
214
    {
215
        $sTemplate = ($nNumber == $this->currentPage ? 'current' : 'enabled');
216
        return $this->getLink($nNumber, $sTemplate, $nNumber, $this->ellipsysText);
217
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
218
219
    /**
220
     * Get the array of page numbers to be printed.
221
     *
222
     * Example: [1, 0, 4, 5, 6, 0, 10]
223
     *
224
     * @return array
225
     */
226
    protected function getPageNumbers()
227
    {
228
        $pageNumbers = [];
229
230
        if($this->totalPages <= $this->maxPagesToShow)
231
        {
232
            for($i = 0; $i < $this->totalPages; $i++)
233
            {
234
                $pageNumbers[] = $i + 1;
235
            }
236
237
            return $pageNumbers;
238
        }
239
240
        // Determine the sliding range, centered around the current page.
241
        $numAdjacents = (int)floor(($this->maxPagesToShow - 4) / 2);
242
243
        $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...
244
        $slidingEndOffset = $numAdjacents + 3 - $this->currentPage;
245
        if($slidingEndOffset < 0)
246
        {
247
            $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...
248
            $slidingEndOffset = 0;
249
        }
250
251
        $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...
252
        $slidingStartOffset = $this->currentPage + $numAdjacents + 2 - $this->totalPages;
253
        if($slidingStartOffset < 0)
254
        {
255
            $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...
256
            $slidingStartOffset = 0;
257
        }
258
259
        // Build the list of page numbers.
260
        if($slidingStart > 1)
261
        {
262
            $pageNumbers[] = 1;
263
            $pageNumbers[] = 0; // Ellipsys;
264
        }
265
        for($i = $slidingStart - $slidingStartOffset; $i <= $slidingEnd + $slidingEndOffset; $i++)
266
        {
267
            $pageNumbers[] = $i;
268
        }
269
        if($slidingEnd < $this->totalPages)
270
        {
271
            $pageNumbers[] = 0; // Ellipsys;
272
            $pageNumbers[] = $this->totalPages;
273
        }
274
275
        return $pageNumbers;
276
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
277
278
    /**
279
     * Render an HTML pagination control.
280
     *
281
     * @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...
282
     *
283
     * @return null|Store
284
     */
285
    public function render($totalPages)
286
    {
287
        $this->totalPages = $totalPages;
288
289
        $aLinks = array_map(function($nNumber) {
290
            return $this->getPageLink($nNumber);
291
        }, $this->getPageNumbers());
292
293
        return $this->xRenderer->render('pagination::wrapper', [
294
            'links' => $aLinks,
295
            'prev' => $this->getPrevLink(),
296
            'next' => $this->getNextLink(),
297
        ]);
298
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
299
}
300