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

Paginator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 30
dl 0
loc 174
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 5 1
A __toString() 0 7 2
A setNextText() 0 4 1
A updateTotalPages() 0 4 2
A setMaxPagesToShow() 0 4 1
A setTotalItems() 0 4 1
A __construct() 0 3 1
A setItemsPerPage() 0 4 1
A render() 0 3 1
A setPreviousText() 0 4 1
A getPages() 0 5 1
A setCurrentPage() 0 4 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
0 ignored issues
show
Coding Style introduced by
Empty line not required before block comment
Loading history...
4
The MIT License (MIT)
0 ignored issues
show
Coding Style introduced by
First line of comment not aligned correctly; expected 4 spaces but found 0
Loading history...
5
6
Copyright (c) 2014 Jason Grimes
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
7
8
Permission is hereby granted, free of charge, to any person obtaining a copy
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
9
of this software and associated documentation files (the "Software"), to deal
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
10
in the Software without restriction, including without limitation the rights
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
12
copies of the Software, and to permit persons to whom the Software is
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
13
furnished to do so, subject to the following conditions:
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
14
15
The above copyright notice and this permission notice shall be included in all
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
16
copies or substantial portions of the Software.
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
24
SOFTWARE.
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 4 spaces but found 0
Loading history...
25
*/
26
27
/**
28
 * Paginator.php - Jaxon Paginator
29
 *
30
 * Create pagination links from  an Jaxon request and a data array.
31
 *
32
 * @package jaxon-core
33
 * @author Jason Grimes
34
 * @author Thierry Feuzeu
35
 * @copyright 2014 Jason Grimes
36
 * @copyright 2016 Thierry Feuzeu
37
 * @license https://opensource.org/licenses/MIT MIT License
38
 * @link https://github.com/jaxon-php/jaxon-core
39
 */
40
41
namespace Jaxon\Ui\Pagination;
42
43
use Jaxon\Request\Factory\Request;
44
use Jaxon\Ui\View\Store;
45
46
class Paginator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Paginator
Loading history...
47
{
48
    /**
49
     * @var integer
50
     */
51
    protected $totalItems = 0;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
52
53
    /**
54
     * @var integer
55
     */
56
    protected $totalPages = 0;
57
58
    /**
59
     * @var integer
60
     */
61
    protected $itemsPerPage = 0;
62
63
    /**
64
     * The pagination renderer
65
     *
66
     * @var Renderer
67
     */
68
    protected $xRenderer = null;
69
70
    /**
71
     * The constructor
72
     *
73
     * @param Renderer $xRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
74
     */
75
    public function __construct(Renderer $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 Paginator
86
     */
87
    public function setPreviousText($text)
88
    {
89
        $this->xRenderer->setPreviousText($text);
90
        return $this;
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 Paginator
99
     */
100
    public function setNextText($text)
101
    {
102
        $this->xRenderer->setNextText($text);
103
        return $this;
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Update the number of pages
108
     *
109
     * @return Paginator
110
     */
111
    protected function updateTotalPages()
112
    {
113
        $this->totalPages = ($this->itemsPerPage == 0 ? 0 : (int)ceil($this->totalItems / $this->itemsPerPage));
114
        return $this;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Set the max number of pages to show
119
     *
120
     * @param int $maxPagesToShow The max number of pages to show
121
     *
122
     * @return Paginator
123
     */
124
    public function setMaxPagesToShow($maxPagesToShow)
125
    {
126
        $this->xRenderer->setMaxPagesToShow($maxPagesToShow);
127
        return $this;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Set the current page number
132
     *
133
     * @param int $currentPage The current page number
134
     *
135
     * @return Paginator
136
     */
137
    protected function setCurrentPage($currentPage)
138
    {
139
        $this->xRenderer->setCurrentPage($currentPage);
140
        return $this;
141
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
142
143
    /**
144
     * Set the number of items per page
145
     *
146
     * @param int $itemsPerPage The number of items per page
147
     *
148
     * @return Paginator
149
     */
150
    protected function setItemsPerPage($itemsPerPage)
151
    {
152
        $this->itemsPerPage = intval($itemsPerPage);
153
        return $this->updateTotalPages();
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Set the total number of items
158
     *
159
     * @param int $totalItems The total number of items
160
     *
161
     * @return Paginator
162
     */
163
    protected function setTotalItems($totalItems)
164
    {
165
        $this->totalItems = intval($totalItems);
166
        return $this->updateTotalPages();
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Setup the paginator
171
     *
172
     * @param int $totalItems The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
173
     * @param int $itemsPerPage The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
174
     * @param int $currentPage The current page number
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
175
     * @param Request $xRequest The request to be paginated
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
176
     *
177
     * @return Paginator
178
     */
179
    public function setup($totalItems, $itemsPerPage, $currentPage, $xRequest)
180
    {
181
        $this->setTotalItems($totalItems)->setItemsPerPage($itemsPerPage)->setCurrentPage($currentPage);
182
        $this->xRenderer->setRequest($xRequest);
183
        return $this;
184
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
185
186
    /**
187
     * Get the pages.
188
     *
189
     * @return array
190
     */
191
    public function getPages()
192
    {
193
        return array_map(function($aPage) {
194
            return (object)['type' => $aPage[0], 'text' => $aPage[1], 'call' => $aPage[2]];
195
        }, $this->xRenderer->getPages($this->totalPages));
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * Render an HTML pagination control.
200
     *
201
     * @return null|Store
202
     */
203
    public function render()
204
    {
205
        return $this->xRenderer->render($this->totalPages);
206
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
    /**
209
     * Render an HTML pagination control.
210
     *
211
     * @return string
212
     */
213
    public function __toString()
214
    {
215
        if($this->totalPages < 2)
216
        {
217
            return '';
218
        }
219
        return $this->render()->__toString();
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
221
}
222