forked from makerlabs/PagerBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pager.php
257 lines (220 loc) · 5.2 KB
/
Pager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*
* This file is part of the PagerBundle package.
*
* (c) Marcin Butlak <contact@maker-labs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MakerLabs\PagerBundle;
use MakerLabs\PagerBundle\Adapter\PagerAdapterInterface;
/**
* Pager
*
* @author Marcin Butlak <contact@maker-labs.com>
*/
class Pager
{
/**
*
* @var integer
*/
protected $page = 1;
/**
*
* @var integer
*/
protected $limit = 20;
/**
*
* @var integer
*/
protected $maxPages = 10;
/**
* Constructor
*
* @param PagerAdapterInterface $adapter The pager adapter
* @param array $options Additional options
*/
public function __construct(PagerAdapterInterface $adapter, array $options = array())
{
$this->adapter = $adapter;
if (isset($options['limit'])) {
$this->setLimit($options['limit']);
}
if (isset($options['page'])) {
$this->setPage($options['page']);
}
if (isset($options['max_pages'])) {
$this->setMaxPages($options['max_pages']);
}
}
/**
* Sets the current page number
*
* @param integer $page The current page number
* @return Pager instance
*/
public function setPage($page)
{
$this->page = min($page > 0 ? $page : $this->getFirstPage(), $this->getLastPage());
return $this;
}
/**
* Returns the current page number
*
* @return integer
*/
public function getPage()
{
return $this->page;
}
/**
* Sets the results limit for one page
*
* @param integer $limit
* @return Pager instance
*/
public function setLimit($limit)
{
$this->limit = $limit > 0 ? $limit : 1;
$this->setPage($this->page);
return $this;
}
/**
* Returns the current results limit for one page
*
* @return integer
*/
public function getLimit()
{
return $this->limit;
}
/**
* Sets the number of pages shown
*
* @param integer
* @return Pager instance
*/
public function setMaxPages($maxPages)
{
$this->maxPages = $maxPages;
return $this;
}
/**
* Returns the number of pages shown
*
* @return integer
*/
public function getMaxPages()
{
return $this->maxPages;
}
/**
* Returns the next page number
*
* @return integer
*/
public function getNextPage()
{
return $this->page < $this->getLastPage() ? $this->page + 1 : $this->getLastPage();
}
/**
* Returns the previous page number
*
* @return integer
*/
public function getPreviousPage()
{
return $this->page > $this->getFirstPage() ? $this->page - 1 : $this->getFirstPage();
}
/**
* Returns true if the current page is first
*
* @return boolean
*/
public function isFirstPage()
{
return $this->page == 1;
}
/**
* Returns the first page number
*
* @return integer
*/
public function getFirstPage()
{
return 1;
}
/**
* Returns true if the current page is last
*
* @return boolean
*/
public function isLastPage()
{
return $this->page == $this->getLastPage();
}
/**
* Returns the last page number
*
* @return integer
*/
public function getLastPage()
{
return $this->hasResults() ? ceil($this->adapter->getTotalResults() / $this->limit) : $this->getFirstPage();
}
/**
* Returns true if the current result set requires pagination
*
* @return boolean
*/
public function isPaginable()
{
return $this->adapter->getTotalResults() > $this->limit;
}
/**
* Generates a page list
*
* @param integer $pages Number of pages to generate
* @return array The page list
*/
public function getPages()
{
$pages = $this->getMaxPages();
$tmp = $this->page - floor($pages / 2) + 1;
$start = $tmp > $this->getFirstPage() ? $tmp : $this->getFirstPage();
$end = min($start + $pages - 1, $this->getLastPage());
$begin = ($end - $pages + 1) > $this->getFirstPage() ? $end - $pages + 1 : $this->getFirstPage();
return range($begin, $end, 1);
}
/**
* Returns true if the current result set is not empty
*
* @return boolean
*/
public function hasResults()
{
return $this->adapter->getTotalResults() > 0;
}
/**
*
* Returns results list for the current page and limit
*
* @return array
*/
public function getResults()
{
return $this->hasResults() ? $this->adapter->getResults(($this->page - 1) * $this->limit, $this->limit) : array();
}
/**
* Returns the current adapter instance
*
* @return PagerAdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
}