1
|
|
|
<?php |
2
|
|
|
namespace Tuum\Pagination; |
3
|
|
|
|
4
|
|
|
use Tuum\Pagination\Paginate\Paginate; |
5
|
|
|
use Tuum\Pagination\Paginate\PaginateInterface; |
6
|
|
|
use Tuum\Pagination\ToHtml\ToBootstrap3; |
7
|
|
|
|
8
|
|
|
class Inputs |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
public $defaultLimit = 20; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $pagerKey = '_page'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $limitKey = '_limit'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int|null |
27
|
|
|
*/ |
28
|
|
|
private $total = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $list = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $path = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
public $inputs = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string class name of ToHtmlInterface. |
47
|
|
|
*/ |
48
|
|
|
private $defaultToHtml = ToBootstrap3::class; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Inputs constructor. |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function __construct() |
55
|
|
|
{ |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* get the limit, i.e. number of data per page. |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getLimit() |
64
|
|
|
{ |
65
|
|
|
$limit = $this->getInt($this->limitKey, 0); |
66
|
|
|
return $limit > 1 ? $limit : $this->defaultLimit; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* get the offset for retrieving data. |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getOffset() |
75
|
|
|
{ |
76
|
|
|
return $this->getLimit() * ($this->getPage() - 1); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* get the current page number, starting from 1. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
public function getPage() |
85
|
|
|
{ |
86
|
|
|
$page = $this->getInt($this->pagerKey, 1); |
87
|
|
|
return $page > 0 ? $page : 1; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $key |
92
|
|
|
* @param int $default |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
private function getInt($key, $default) |
96
|
|
|
{ |
97
|
|
|
if (!isset($this->inputs[$key])) { |
98
|
|
|
return $default; |
99
|
|
|
} |
100
|
|
|
if (!$this->inputs[$key]) { |
101
|
|
|
return $default; |
102
|
|
|
} |
103
|
|
|
$value = (int) $this->inputs[$key]; |
104
|
|
|
return is_integer($value) ? $value : $default; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* get any key from query. |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* @param null|mixed $alt |
112
|
|
|
* @return null|mixed |
113
|
|
|
*/ |
114
|
|
|
public function get($key, $alt = null) |
115
|
|
|
{ |
116
|
|
|
return array_key_exists($key, $this->inputs) |
117
|
|
|
? $this->inputs[$key] |
118
|
|
|
: $this->inputs[$key] = $alt; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* get total number of data. |
123
|
|
|
* - total: number of all the possible data which can be retrieved. |
124
|
|
|
* - count: number of data in the current list. |
125
|
|
|
* |
126
|
|
|
* @return int|null |
127
|
|
|
*/ |
128
|
|
|
public function getTotal() |
129
|
|
|
{ |
130
|
|
|
return $this->total; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* set the total of data. |
135
|
|
|
* |
136
|
|
|
* @param int|null $total |
137
|
|
|
*/ |
138
|
|
|
public function setTotal($total) |
139
|
|
|
{ |
140
|
|
|
$this->total = $total; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* set the data for list. |
145
|
|
|
* |
146
|
|
|
* @param mixed $list |
147
|
|
|
*/ |
148
|
|
|
public function setList($list) |
149
|
|
|
{ |
150
|
|
|
$this->list = $list; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* get the data for list. |
155
|
|
|
* |
156
|
|
|
* @return null|array|mixed |
157
|
|
|
*/ |
158
|
|
|
public function getList() |
159
|
|
|
{ |
160
|
|
|
return $this->list; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* get the count, i.e. number of data in the current list. |
165
|
|
|
* count is the number of data in the current list. |
166
|
|
|
* |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
|
|
public function getCount() |
170
|
|
|
{ |
171
|
|
|
if (isset($this->list) && is_array($this->list)) { |
172
|
|
|
return count($this->list); |
173
|
|
|
} |
174
|
|
|
return 0; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* calculates the first page number, that is 1. |
179
|
|
|
* |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
|
|
public function calcFirstPage() |
183
|
|
|
{ |
184
|
|
|
return 1; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* calculates the last pager number. |
189
|
|
|
* |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
|
|
public function calcLastPage() |
193
|
|
|
{ |
194
|
|
|
$total = $this->getTotal(); |
195
|
|
|
if (!$total) { |
|
|
|
|
196
|
|
|
return $this->getPage() + 1; |
197
|
|
|
} |
198
|
|
|
$pages = $this->getLimit(); |
199
|
|
|
return (integer)(ceil($total / $pages)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param null|int $page |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getPath($page = null) |
207
|
|
|
{ |
208
|
|
|
if (is_null($page)) { |
209
|
|
|
return $this->path; |
210
|
|
|
} |
211
|
|
|
$page = (int)$page; |
212
|
|
|
return $this->path . '?' . $this->pagerKey . '=' . $page; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return PaginateInterface |
217
|
|
|
*/ |
218
|
|
|
public function getPagination() |
219
|
|
|
{ |
220
|
|
|
return Paginate::forge($this); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function __toString() |
227
|
|
|
{ |
228
|
|
|
$class = $this->defaultToHtml; |
229
|
|
|
$toHtml = new $class(Paginate::forge($this)); |
230
|
|
|
return (string) $toHtml; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* set a class to convert to html pagination. |
235
|
|
|
* The class must implement ToHtmlInterface. |
236
|
|
|
* |
237
|
|
|
* @param string $defaultToHtml |
238
|
|
|
*/ |
239
|
|
|
public function setDefaultToHtml($defaultToHtml) |
240
|
|
|
{ |
241
|
|
|
$this->defaultToHtml = $defaultToHtml; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..