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