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