Passed
Branch master (a18575)
by Thierry
02:38
created

Renderer::getPageCall()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 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\Utils\Pagination;
16
17
use Jaxon\Utils\View\Renderer as ViewRenderer;
18
use Jaxon\Utils\Template\Engine;
19
use Jaxon\Request\Factory\Request;
20
use Jaxon\Request\Factory\Parameter;
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 the previous link.
163
     *
164
     * @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...
165
     * @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...
166
     * @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...
167
     * @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...
168
     *
169
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Jaxon\Utils\View\Store?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
170
     */
171
    protected function getLink($nNumber, $sTemplate, $sEnabledText, $sDisabledText)
172
    {
173
        if($nNumber > 0)
174
        {
175
            return $this->xRenderer->render('pagination::links/' . $sTemplate, [
176
                'text' => $sEnabledText,
177
                'call' => $this->getPageCall($nNumber),
178
            ]);
179
        }
180
        return $this->xRenderer->render('pagination::links/disabled', ['text' => $sDisabledText]);
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * Render the previous link.
185
     *
186
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Jaxon\Utils\View\Store?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
187
     */
188
    protected function getPrevLink()
189
    {
190
        $nNumber = ($this->currentPage > 1 ? $this->currentPage - 1 : 0);
191
        return $this->getLink($nNumber, 'prev', $this->previousText, $this->previousText);
192
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
193
194
    /**
195
     * Render the next link.
196
     *
197
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Jaxon\Utils\View\Store?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
198
     */
199
    protected function getNextLink()
200
    {
201
        $nNumber = ($this->currentPage < $this->totalPages ? $this->currentPage + 1 : 0);
202
        return $this->getLink($nNumber, 'next', $this->nextText, $this->nextText);
203
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
204
205
    /**
206
     * Render the pagination links.
207
     *
208
     * @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...
209
     *
210
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Jaxon\Utils\View\Store?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
211
     */
212
    protected function getPageLink($nNumber)
213
    {
214
        $sTemplate = ($nNumber == $this->currentPage ? 'current' : 'enabled');
215
        return $this->getLink($nNumber, $sTemplate, $nNumber, $this->ellipsysText);
216
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
217
218
    /**
219
     * Get the array of page numbers to be printed.
220
     *
221
     * Example: [1, 0, 4, 5, 6, 0, 10]
222
     *
223
     * @return array
224
     */
225
    protected function getPageNumbers()
226
    {
227
        $pageNumbers = [];
228
229
        if($this->totalPages <= $this->maxPagesToShow)
230
        {
231
            for($i = 0; $i < $this->totalPages; $i++)
232
            {
233
                $pageNumbers[] = $i + 1;
234
            }
235
        }
236
        else
237
        {
238
            // Determine the sliding range, centered around the current page.
239
            $numAdjacents = (int)floor(($this->maxPagesToShow - 4) / 2);
240
241
            $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...
242
            $slidingEndOffset = $numAdjacents + 3 - $this->currentPage;
243
            if($slidingEndOffset < 0)
244
            {
245
                $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...
246
                $slidingEndOffset = 0;
247
            }
248
249
            $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...
250
            $slidingStartOffset = $this->currentPage + $numAdjacents + 2 - $this->totalPages;
251
            if($slidingStartOffset < 0)
252
            {
253
                $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...
254
                $slidingStartOffset = 0;
255
            }
256
257
            // Build the list of page numbers.
258
            if($slidingStart > 1)
259
            {
260
                $pageNumbers[] = 1;
261
                $pageNumbers[] = 0; // Ellipsys;
262
            }
263
            for($i = $slidingStart - $slidingStartOffset; $i <= $slidingEnd + $slidingEndOffset; $i++)
264
            {
265
                $pageNumbers[] = $i;
266
            }
267
            if($slidingEnd < $this->totalPages)
268
            {
269
                $pageNumbers[] = 0; // Ellipsys;
270
                $pageNumbers[] = $this->totalPages;
271
            }
272
        }
273
274
        return $pageNumbers;
275
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
276
277
    /**
278
     * Render an HTML pagination control.
279
     *
280
     * @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...
281
     *
282
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Jaxon\Utils\View\Store?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
283
     */
284
    public function render($totalPages)
285
    {
286
        $this->totalPages = $totalPages;
287
288
        $aLinks = array_map(function($nNumber) {
289
            return $this->getPageLink($nNumber);
290
        }, $this->getPageNumbers());
291
292
        return $this->xRenderer->render('pagination::wrapper', [
293
            'links' => $aLinks,
294
            'prev' => $this->getPrevLink(),
295
            'next' => $this->getNextLink(),
296
        ]);
297
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
298
}
299