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