1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Phauthentic\Pagination; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Pagination Data Object |
20
|
|
|
*/ |
21
|
|
|
class PaginationParams implements PaginationParamsInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Sort direction |
25
|
|
|
* |
26
|
|
|
* This is usually asc or desc in SQL dialects |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $direction = 'asc'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The limit per page, or records per page |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $limit = 20; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $maxLimit = 200; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
protected $sortBy = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $page = 1; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $pageCount = 1; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $count = 0; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
public function getCount(): int |
68
|
|
|
{ |
69
|
|
|
return $this->count; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
protected function calculatePageCount() |
76
|
|
|
{ |
77
|
|
|
$count = $this->count / $this->limit; |
78
|
|
|
|
79
|
|
|
if ((int)$count === 0) { |
80
|
|
|
$count = 1; |
81
|
|
|
} else { |
82
|
|
|
$count = (int)ceil($count); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->pageCount = $count; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function setCount(int $count): PaginationParamsInterface |
92
|
|
|
{ |
93
|
|
|
$this->count = $count; |
94
|
|
|
$this->calculatePageCount(); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function setDirection(string $direction): PaginationParamsInterface |
103
|
|
|
{ |
104
|
|
|
$this->direction = $direction; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
public function getDirection(): string |
113
|
|
|
{ |
114
|
|
|
return $this->direction; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setSortBy(?string $sortBy): PaginationParamsInterface |
118
|
|
|
{ |
119
|
|
|
$this->sortBy = $sortBy; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getSortBy(): ?string |
125
|
|
|
{ |
126
|
|
|
return $this->sortBy; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function setPage(int $page): PaginationParamsInterface |
133
|
|
|
{ |
134
|
|
|
if ($page === 0) { |
135
|
|
|
throw new InvalidArgumentException('Page value must be greater than zero'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->page = $page; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
|
|
public function setMaxLimit(int $maxLimit): PaginationParamsInterface |
147
|
|
|
{ |
148
|
|
|
if ($maxLimit < 2) { |
149
|
|
|
throw new InvalidArgumentException('Max limit value must be greater than one'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->maxLimit = $maxLimit; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function setLimit(int $limit): PaginationParamsInterface |
161
|
|
|
{ |
162
|
|
|
if ($limit > $this->maxLimit) { |
163
|
|
|
throw new InvalidArgumentException(sprintf( |
164
|
|
|
'Limit must be smaller than %d', |
165
|
|
|
$this->maxLimit |
166
|
|
|
)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($limit < 1) { |
170
|
|
|
throw new InvalidArgumentException('Limit must be equal or greater than 1'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->limit = $limit; |
174
|
|
|
$this->calculatePageCount(); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
|
|
public function getLimit(): int |
183
|
|
|
{ |
184
|
|
|
return $this->limit; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
190
|
|
|
public function getOffset(): int |
191
|
|
|
{ |
192
|
|
|
return ($this->page - 1) * $this->limit; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @inheritDoc |
197
|
|
|
*/ |
198
|
|
|
public function getPage(): int |
199
|
|
|
{ |
200
|
|
|
return $this->page; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @inheritDoc |
205
|
|
|
*/ |
206
|
|
|
public function getPageCount() |
207
|
|
|
{ |
208
|
|
|
$this->calculatePageCount(); |
209
|
|
|
|
210
|
|
|
return $this->pageCount; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritDoc |
215
|
|
|
*/ |
216
|
|
|
public function getLastPage(): int |
217
|
|
|
{ |
218
|
|
|
return $this->pageCount; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @inheritDoc |
223
|
|
|
*/ |
224
|
|
|
public function getNextPage(): ?int |
225
|
|
|
{ |
226
|
|
|
if ($this->page < $this->getPageCount()) { |
227
|
|
|
return $this->page + 1; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritDoc |
235
|
|
|
*/ |
236
|
|
|
public function getPreviousPage(): ?int |
237
|
|
|
{ |
238
|
|
|
if ($this->page > 1) { |
239
|
|
|
return $this->page - 1; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return null; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritDoc |
247
|
|
|
*/ |
248
|
|
|
public function hasPreviousPage(): bool |
249
|
|
|
{ |
250
|
|
|
return $this->getPreviousPage() !== null; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritDoc |
255
|
|
|
*/ |
256
|
|
|
public function hasNextPage(): bool |
257
|
|
|
{ |
258
|
|
|
return $this->getNextPage() !== null; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the current state of the object as array |
263
|
|
|
* |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
public function toArray(): array |
267
|
|
|
{ |
268
|
|
|
return [ |
269
|
|
|
'page' => $this->page, |
270
|
|
|
'count' => $this->count, |
271
|
|
|
'lastPage' => $this->getLastPage(), |
272
|
|
|
'limit' => $this->limit, |
273
|
|
|
'direction' => $this->direction, |
274
|
|
|
'sortBy' => $this->sortBy, |
275
|
|
|
'nextPage' => $this->getNextPage(), |
276
|
|
|
'previousPage' => $this->getPreviousPage(), |
277
|
|
|
'hasNextPage' => $this->hasNextPage(), |
278
|
|
|
'hasPreviousPage' => $this->hasPreviousPage() |
279
|
|
|
]; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|