1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Pagination - Simple, lightweight and universal service that implements pagination on collections of things. |
7
|
|
|
* |
8
|
|
|
* @author Eric Sizemore <[email protected]> |
9
|
|
|
* @version 2.0.0 |
10
|
|
|
* @copyright (C) 2024 Eric Sizemore |
11
|
|
|
* @license The MIT License (MIT) |
12
|
|
|
* |
13
|
|
|
* Copyright (C) 2024 Eric Sizemore<https://www.secondversion.com/>. |
14
|
|
|
* Copyright (c) 2015-2019 Ashley Dawson<[email protected]> |
15
|
|
|
* |
16
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
17
|
|
|
* of this software and associated documentation files (the "Software"), to |
18
|
|
|
* deal in the Software without restriction, including without limitation the |
19
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
20
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
21
|
|
|
* furnished to do so, subject to the following conditions: |
22
|
|
|
* |
23
|
|
|
* The above copyright notice and this permission notice shall be included in |
24
|
|
|
* all copies or substantial portions of the Software. |
25
|
|
|
* |
26
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
27
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
28
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
29
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
30
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
31
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
32
|
|
|
* THE SOFTWARE. |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Esi\Pagination is a fork of AshleyDawson\SimplePagination (https://github.com/AshleyDawson/SimplePagination) which is: |
37
|
|
|
* Copyright (c) 2015-2019 Ashley Dawson |
38
|
|
|
* |
39
|
|
|
* For a list of changes made in Esi\Pagination in comparison to the original library {@see CHANGELOG.md}. |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
namespace Esi\Pagination; |
43
|
|
|
|
44
|
|
|
use ArrayIterator; |
45
|
|
|
use Countable; |
46
|
|
|
use IteratorAggregate; |
47
|
|
|
|
48
|
|
|
use function count; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Class Pagination |
52
|
|
|
* |
53
|
|
|
* @implements IteratorAggregate<(int|string), int> |
54
|
|
|
*/ |
55
|
|
|
class Pagination implements IteratorAggregate, Countable |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var array<int> |
59
|
|
|
*/ |
60
|
|
|
private array $items = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array<int> |
64
|
|
|
*/ |
65
|
|
|
private array $pages = []; |
66
|
|
|
|
67
|
|
|
private int $totalNumberOfPages; |
68
|
|
|
|
69
|
|
|
private int $currentPageNumber; |
70
|
|
|
|
71
|
|
|
private int $firstPageNumber; |
72
|
|
|
|
73
|
|
|
private int $lastPageNumber; |
74
|
|
|
|
75
|
|
|
private ?int $previousPageNumber = null; |
76
|
|
|
|
77
|
|
|
private ?int $nextPageNumber = null; |
78
|
|
|
|
79
|
|
|
private int $itemsPerPage; |
80
|
|
|
|
81
|
|
|
private int $totalNumberOfItems; |
82
|
|
|
|
83
|
|
|
private int $firstPageNumberInRange; |
84
|
|
|
|
85
|
|
|
private int $lastPageNumberInRange; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var array<int|string, string>|array<mixed> |
89
|
|
|
*/ |
90
|
|
|
private array $meta; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array<int> |
94
|
|
|
*/ |
95
|
5 |
|
public function getItems(): array |
96
|
|
|
{ |
97
|
5 |
|
return $this->items; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array<int> $items |
102
|
|
|
*/ |
103
|
8 |
|
public function setItems(array $items): static |
104
|
|
|
{ |
105
|
8 |
|
$this->items = $items; |
106
|
8 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
public function getCurrentPageNumber(): int |
110
|
|
|
{ |
111
|
4 |
|
return $this->currentPageNumber; |
112
|
|
|
} |
113
|
|
|
|
114
|
8 |
|
public function setCurrentPageNumber(int $currentPageNumber): static |
115
|
|
|
{ |
116
|
8 |
|
$this->currentPageNumber = $currentPageNumber; |
117
|
8 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
public function getFirstPageNumber(): int |
121
|
|
|
{ |
122
|
4 |
|
return $this->firstPageNumber; |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
public function setFirstPageNumber(int $firstPageNumber): static |
126
|
|
|
{ |
127
|
8 |
|
$this->firstPageNumber = $firstPageNumber; |
128
|
8 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function getFirstPageNumberInRange(): int |
132
|
|
|
{ |
133
|
4 |
|
return $this->firstPageNumberInRange; |
134
|
|
|
} |
135
|
|
|
|
136
|
8 |
|
public function setFirstPageNumberInRange(int $firstPageNumberInRange): static |
137
|
|
|
{ |
138
|
8 |
|
$this->firstPageNumberInRange = $firstPageNumberInRange; |
139
|
8 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
public function getItemsPerPage(): int |
143
|
|
|
{ |
144
|
4 |
|
return $this->itemsPerPage; |
145
|
|
|
} |
146
|
|
|
|
147
|
8 |
|
public function setItemsPerPage(int $itemsPerPage): static |
148
|
|
|
{ |
149
|
8 |
|
$this->itemsPerPage = $itemsPerPage; |
150
|
8 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
public function getLastPageNumber(): int |
154
|
|
|
{ |
155
|
4 |
|
return $this->lastPageNumber; |
156
|
|
|
} |
157
|
|
|
|
158
|
8 |
|
public function setLastPageNumber(int $lastPageNumber): static |
159
|
|
|
{ |
160
|
8 |
|
$this->lastPageNumber = $lastPageNumber; |
161
|
8 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
4 |
|
public function getLastPageNumberInRange(): int |
165
|
|
|
{ |
166
|
4 |
|
return $this->lastPageNumberInRange; |
167
|
|
|
} |
168
|
|
|
|
169
|
8 |
|
public function setLastPageNumberInRange(int $lastPageNumberInRange): static |
170
|
|
|
{ |
171
|
8 |
|
$this->lastPageNumberInRange = $lastPageNumberInRange; |
172
|
8 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
4 |
|
public function getNextPageNumber(): ?int |
176
|
|
|
{ |
177
|
4 |
|
return $this->nextPageNumber; |
178
|
|
|
} |
179
|
|
|
|
180
|
8 |
|
public function setNextPageNumber(?int $nextPageNumber): static |
181
|
|
|
{ |
182
|
8 |
|
$this->nextPageNumber = $nextPageNumber; |
183
|
8 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array<int> |
188
|
|
|
*/ |
189
|
5 |
|
public function getPages(): array |
190
|
|
|
{ |
191
|
5 |
|
return $this->pages; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array<int> $pages |
196
|
|
|
*/ |
197
|
8 |
|
public function setPages(array $pages): static |
198
|
|
|
{ |
199
|
8 |
|
$this->pages = $pages; |
200
|
8 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
4 |
|
public function getPreviousPageNumber(): ?int |
204
|
|
|
{ |
205
|
4 |
|
return $this->previousPageNumber; |
206
|
|
|
} |
207
|
|
|
|
208
|
8 |
|
public function setPreviousPageNumber(?int $previousPageNumber): static |
209
|
|
|
{ |
210
|
8 |
|
$this->previousPageNumber = $previousPageNumber; |
211
|
8 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
5 |
|
public function getTotalNumberOfItems(): int |
215
|
|
|
{ |
216
|
5 |
|
return $this->totalNumberOfItems; |
217
|
|
|
} |
218
|
|
|
|
219
|
8 |
|
public function setTotalNumberOfItems(int $totalNumberOfItems): static |
220
|
|
|
{ |
221
|
8 |
|
$this->totalNumberOfItems = $totalNumberOfItems; |
222
|
8 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
4 |
|
public function getTotalNumberOfPages(): int |
226
|
|
|
{ |
227
|
4 |
|
return $this->totalNumberOfPages; |
228
|
|
|
} |
229
|
|
|
|
230
|
8 |
|
public function setTotalNumberOfPages(int $totalNumberOfPages): static |
231
|
|
|
{ |
232
|
8 |
|
$this->totalNumberOfPages = $totalNumberOfPages; |
233
|
8 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
* |
239
|
|
|
* @return ArrayIterator<(int|string), int> |
240
|
|
|
*/ |
241
|
2 |
|
public function getIterator(): ArrayIterator |
242
|
|
|
{ |
243
|
2 |
|
return new ArrayIterator($this->items); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
2 |
|
public function count(): int |
250
|
|
|
{ |
251
|
2 |
|
return count($this->items); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return array<int|string, string>|array<mixed> |
256
|
|
|
*/ |
257
|
3 |
|
public function getMeta(): array |
258
|
|
|
{ |
259
|
3 |
|
return $this->meta; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param array<int|string, string>|array<mixed> $meta |
264
|
|
|
*/ |
265
|
3 |
|
public function setMeta(array $meta): static |
266
|
|
|
{ |
267
|
3 |
|
$this->meta = $meta; |
268
|
3 |
|
return $this; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|