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
|
|
|
* Attributs |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $attributes = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
3 |
|
public function getCount(): int |
75
|
|
|
{ |
76
|
3 |
|
return $this->count; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
11 |
|
protected function calculatePageCount() |
83
|
|
|
{ |
84
|
11 |
|
$count = $this->count / $this->limit; |
85
|
|
|
|
86
|
11 |
|
if ((int)$count === 0) { |
87
|
9 |
|
$count = 1; |
88
|
|
|
} else { |
89
|
9 |
|
$count = (int)ceil($count); |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
$this->pageCount = $count; |
93
|
11 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
10 |
|
public function setCount(int $count): PaginationParamsInterface |
99
|
|
|
{ |
100
|
10 |
|
$this->count = $count; |
101
|
10 |
|
$this->calculatePageCount(); |
102
|
|
|
|
103
|
10 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
5 |
|
public function setDirection(string $direction): PaginationParamsInterface |
110
|
|
|
{ |
111
|
5 |
|
$this->direction = $direction; |
112
|
|
|
|
113
|
5 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
6 |
|
public function getDirection(): string |
120
|
|
|
{ |
121
|
6 |
|
return $this->direction; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
6 |
|
public function setSortBy(?string $sortBy): PaginationParamsInterface |
128
|
|
|
{ |
129
|
6 |
|
$this->sortBy = $sortBy; |
130
|
|
|
|
131
|
6 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
8 |
|
public function getSortBy(): ?string |
138
|
|
|
{ |
139
|
8 |
|
return $this->sortBy; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
4 |
|
public function setPage(int $page): PaginationParamsInterface |
146
|
|
|
{ |
147
|
4 |
|
if ($page === 0) { |
148
|
1 |
|
throw new InvalidArgumentException('Page value must be greater than zero'); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
$this->page = $page; |
152
|
|
|
|
153
|
3 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
1 |
|
public function setMaxLimit(int $maxLimit): PaginationParamsInterface |
160
|
|
|
{ |
161
|
1 |
|
if ($maxLimit < 2) { |
162
|
1 |
|
throw new InvalidArgumentException('Max limit value must be greater than one'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->maxLimit = $maxLimit; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
10 |
|
public function setLimit(int $limit): PaginationParamsInterface |
174
|
|
|
{ |
175
|
10 |
|
if ($limit > $this->maxLimit) { |
176
|
1 |
|
throw new InvalidArgumentException(sprintf( |
177
|
1 |
|
'Limit must be smaller than %d', |
178
|
1 |
|
$this->maxLimit |
179
|
|
|
)); |
180
|
|
|
} |
181
|
|
|
|
182
|
9 |
|
if ($limit < 1) { |
183
|
1 |
|
throw new InvalidArgumentException('Limit must be equal or greater than 1'); |
184
|
|
|
} |
185
|
|
|
|
186
|
8 |
|
$this->limit = $limit; |
187
|
8 |
|
$this->calculatePageCount(); |
188
|
|
|
|
189
|
8 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritDoc |
194
|
|
|
*/ |
195
|
8 |
|
public function getLimit(): int |
196
|
|
|
{ |
197
|
8 |
|
return $this->limit; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
8 |
|
public function getOffset(): int |
204
|
|
|
{ |
205
|
8 |
|
return ($this->page - 1) * $this->limit; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritDoc |
210
|
|
|
*/ |
211
|
3 |
|
public function getPage(): int |
212
|
|
|
{ |
213
|
3 |
|
return $this->page; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritDoc |
218
|
|
|
*/ |
219
|
3 |
|
public function getPageCount(): int |
220
|
|
|
{ |
221
|
3 |
|
$this->calculatePageCount(); |
222
|
|
|
|
223
|
3 |
|
return $this->pageCount; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritDoc |
228
|
|
|
*/ |
229
|
3 |
|
public function getLastPage(): int |
230
|
|
|
{ |
231
|
3 |
|
return $this->pageCount; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @inheritDoc |
236
|
|
|
*/ |
237
|
3 |
|
public function getNextPage(): ?int |
238
|
|
|
{ |
239
|
3 |
|
if ($this->page < $this->getPageCount()) { |
240
|
2 |
|
return $this->page + 1; |
241
|
|
|
} |
242
|
|
|
|
243
|
2 |
|
return null; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @inheritDoc |
248
|
|
|
*/ |
249
|
3 |
|
public function getPreviousPage(): ?int |
250
|
|
|
{ |
251
|
3 |
|
if ($this->page > 1) { |
252
|
2 |
|
return $this->page - 1; |
253
|
|
|
} |
254
|
|
|
|
255
|
3 |
|
return null; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @inheritDoc |
260
|
|
|
*/ |
261
|
3 |
|
public function hasPreviousPage(): bool |
262
|
|
|
{ |
263
|
3 |
|
return $this->getPreviousPage() !== null; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @inheritDoc |
268
|
|
|
*/ |
269
|
3 |
|
public function hasNextPage(): bool |
270
|
|
|
{ |
271
|
3 |
|
return $this->getNextPage() !== null; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @inheritDoc |
276
|
|
|
*/ |
277
|
|
|
public function addAttribute(string $name, $value): PaginationParamsInterface |
278
|
|
|
{ |
279
|
|
|
$this->attributes[$name] = $value; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @inheritDoc |
286
|
|
|
*/ |
287
|
|
|
public function getAttribute(string $name) |
288
|
|
|
{ |
289
|
|
|
if (isset($this->attributes[$name])) { |
290
|
|
|
return $this->attributes[$name]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return null; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns the current state of the object as array |
298
|
|
|
* |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
1 |
|
public function toArray(): array |
302
|
|
|
{ |
303
|
|
|
return [ |
304
|
1 |
|
'page' => $this->page, |
305
|
1 |
|
'count' => $this->count, |
306
|
1 |
|
'lastPage' => $this->getLastPage(), |
307
|
1 |
|
'limit' => $this->limit, |
308
|
1 |
|
'direction' => $this->direction, |
309
|
1 |
|
'sortBy' => $this->sortBy, |
310
|
1 |
|
'nextPage' => $this->getNextPage(), |
311
|
1 |
|
'previousPage' => $this->getPreviousPage(), |
312
|
1 |
|
'hasNextPage' => $this->hasNextPage(), |
313
|
1 |
|
'hasPreviousPage' => $this->hasPreviousPage(), |
314
|
1 |
|
'attributes' => $this->attributes |
315
|
|
|
]; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|