Passed
Push — feature/code_improvement ( 1b7fb9...abfce7 )
by Thierry
02:51
created

Renderer::getPageLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
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\Contracts\Template\Renderer as TemplateRenderer;
18
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 TemplateRenderer
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
     * The class contructor
67
     *
68
     * @param TemplateRenderer          $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...
69
     */
70
    public function __construct(TemplateRenderer $xRenderer)
71
    {
72
        $this->xRenderer = $xRenderer;
73
    }
74
75
    /**
76
     * Set the text for the previous page link
77
     *
78
     * @param string $text The text for the previous page link
79
     *
80
     * @return void
81
     */
82
    public function setPreviousText($text)
83
    {
84
        $this->previousText = $text;
85
    }
86
87
    /**
88
     * Set the text for the next page link
89
     *
90
     * @param string $text The text for the previous page link
91
     *
92
     * @return void
93
     */
94
    public function setNextText($text)
95
    {
96
        $this->nextText = $text;
97
    }
98
99
    /**
100
     * Set the request to be paginated
101
     *
102
     * @param Request $xRequest The request to be paginated
103
     *
104
     * @return void
105
     */
106
    public function setRequest(Request $xRequest)
107
    {
108
        $this->xRequest = $xRequest;
109
        // Append the page number to the parameter list, if not yet given.
110
        if(($this->xRequest) && !$this->xRequest->hasPageNumber())
111
        {
112
            $this->xRequest->addParameter(Parameter::PAGE_NUMBER, 0);
113
        }
114
    }
115
116
    /**
117
     * Get the js call to a given page
118
     *
119
     * @param int $pageNum The page number
120
     *
121
     * @return string
122
     */
123
    protected function getPageCall($pageNum)
124
    {
125
        return $this->xRequest->setPageNumber($pageNum)->getScript();
126
    }
127
128
    /**
129
     * Render the previous link.
130
     *
131
     * @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...
132
     * @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...
133
     * @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...
134
     * @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...
135
     *
136
     * @return string
137
     */
138
    protected function getLink($nNumber, $sTemplate, $sEnabledText, $sDisabledText)
139
    {
140
        if($nNumber > 0)
141
        {
142
            return $this->xRenderer->render('pagination::links/' . $sTemplate, [
143
                'text' => $sEnabledText,
144
                'call' => $this->getPageCall($nNumber),
145
            ]);
146
        }
147
        return $this->xRenderer->render('pagination::links/disabled', ['text' => $sDisabledText]);
148
    }
149
150
    /**
151
     * Render the previous link.
152
     *
153
     * @return string
154
     */
155
    protected function getPrevLink()
156
    {
157
        $nNumber = ($this->currentPage > 1 ? $this->currentPage - 1 : 0);
158
        return $this->getLink($nNumber, 'prev', $this->previousText, $this->previousText);
159
    }
160
161
    /**
162
     * Render the next link.
163
     *
164
     * @return string
165
     */
166
    protected function getNextLink()
167
    {
168
        $nNumber = ($this->currentPage < $this->totalPages ? $this->currentPage + 1 : 0);
169
        return $this->getLink($nNumber, 'next', $this->nextText, $this->nextText);
170
    }
171
172
    /**
173
     * Render the pagination links.
174
     *
175
     * @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...
176
     *
177
     * @return string
178
     */
179
    protected function getPageLink($nNumber)
180
    {
181
        $sTemplate = ($nNumber == $this->currentPage ? 'current' : 'enabled');
182
        return $this->getLink($nNumber, $sTemplate, $nNumber, $this->ellipsysText);
183
    }
184
185
    /**
186
     * Render an HTML pagination control.
187
     *
188
     * @param array     $aPageNumbers       The page numbers to be rendered
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 7 found
Loading history...
189
     * @param integer   $currentPage        The current 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 2 spaces after parameter name; 8 found
Loading history...
190
     * @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 3 spaces after parameter name; 9 found
Loading history...
191
     *
192
     * @return string
193
     */
194
    public function render(array $aPageNumbers, $currentPage, $totalPages)
195
    {
196
        $this->currentPage = $currentPage;
197
        $this->totalPages = $totalPages;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
198
199
        $aLinks = array_map(function($nNumber) {
200
            return $this->getPageLink($nNumber);
201
        }, $aPageNumbers);
202
203
        return $this->xRenderer->render('pagination::wrapper', [
204
            'links' => $aLinks,
205
            'prev' => $this->getPrevLink(),
206
            'next' => $this->getNextLink(),
207
        ]);
208
    }
209
}
210