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
|
|
|
return (int) $this->inputs[$key]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* get any key from query. |
108
|
|
|
* |
109
|
|
|
* @param string $key |
110
|
|
|
* @param null|mixed $alt |
111
|
|
|
* @return null|mixed |
112
|
|
|
*/ |
113
|
|
|
public function get($key, $alt = null) |
114
|
|
|
{ |
115
|
|
|
return array_key_exists($key, $this->inputs) |
116
|
|
|
? $this->inputs[$key] |
117
|
|
|
: $this->inputs[$key] = $alt; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* get total number of data. |
122
|
|
|
* - total: number of all the possible data which can be retrieved. |
123
|
|
|
* - count: number of data in the current list. |
124
|
|
|
* |
125
|
|
|
* @return int|null |
126
|
|
|
*/ |
127
|
|
|
public function getTotal() |
128
|
|
|
{ |
129
|
|
|
return $this->total; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* set the total of data. |
134
|
|
|
* |
135
|
|
|
* @param int|null $total |
136
|
|
|
*/ |
137
|
|
|
public function setTotal($total) |
138
|
|
|
{ |
139
|
|
|
$this->total = $total; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* set the data for list. |
144
|
|
|
* |
145
|
|
|
* @param mixed $list |
146
|
|
|
*/ |
147
|
|
|
public function setList($list) |
148
|
|
|
{ |
149
|
|
|
$this->list = $list; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* get the data for list. |
154
|
|
|
* |
155
|
|
|
* @return null|array|mixed |
156
|
|
|
*/ |
157
|
|
|
public function getList() |
158
|
|
|
{ |
159
|
|
|
return $this->list; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* get the count, i.e. number of data in the current list. |
164
|
|
|
* count is the number of data in the current list. |
165
|
|
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
public function getCount() |
169
|
|
|
{ |
170
|
|
|
if (isset($this->list) && is_array($this->list)) { |
171
|
|
|
return count($this->list); |
172
|
|
|
} |
173
|
|
|
return 0; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* calculates the first page number, that is 1. |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function calcFirstPage() |
182
|
|
|
{ |
183
|
|
|
return 1; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* calculates the last pager number. |
188
|
|
|
* |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function calcLastPage() |
192
|
|
|
{ |
193
|
|
|
$total = $this->getTotal(); |
194
|
|
|
if (!$total) { |
|
|
|
|
195
|
|
|
return $this->getPage() + 1; |
196
|
|
|
} |
197
|
|
|
$pages = $this->getLimit(); |
198
|
|
|
return (integer)(ceil($total / $pages)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param null|int $page |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getPath($page = null) |
206
|
|
|
{ |
207
|
|
|
if (is_null($page)) { |
208
|
|
|
return $this->path; |
209
|
|
|
} |
210
|
|
|
$page = (int)$page; |
211
|
|
|
return $this->path . '?' . $this->pagerKey . '=' . $page; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return PaginateInterface |
216
|
|
|
*/ |
217
|
|
|
public function getPagination() |
218
|
|
|
{ |
219
|
|
|
return Paginate::forge($this); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function __toString() |
226
|
|
|
{ |
227
|
|
|
$class = $this->defaultToHtml; |
228
|
|
|
$toHtml = new $class(Paginate::forge($this)); |
229
|
|
|
return (string) $toHtml; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* set a class to convert to html pagination. |
234
|
|
|
* The class must implement ToHtmlInterface. |
235
|
|
|
* |
236
|
|
|
* @param string $defaultToHtml |
237
|
|
|
*/ |
238
|
|
|
public function setDefaultToHtml($defaultToHtml) |
239
|
|
|
{ |
240
|
|
|
$this->defaultToHtml = $defaultToHtml; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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..