1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Esi\Pagination. |
7
|
|
|
* |
8
|
|
|
* Copyright (C) Eric Sizemore <https://www.secondversion.com/>. |
9
|
|
|
* Copyright (c) Ashley Dawson <[email protected]>. |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license. For the full copyright, |
12
|
|
|
* license information, and credits/acknowledgements, please view the LICENSE |
13
|
|
|
* and README files that were distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
/** |
16
|
|
|
* Esi\Pagination is a fork of AshleyDawson\SimplePagination (https://github.com/AshleyDawson/SimplePagination) which is: |
17
|
|
|
* Copyright (c) 2015-2019 Ashley Dawson |
18
|
|
|
* |
19
|
|
|
* For a list of changes made in Esi\Pagination in comparison to the original library {@see CHANGELOG.md}. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Esi\Pagination; |
23
|
|
|
|
24
|
|
|
use ArrayIterator; |
25
|
|
|
use Countable; |
26
|
|
|
use IteratorAggregate; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Pagination class. |
30
|
|
|
* |
31
|
|
|
* @implements IteratorAggregate<(int|string), int> |
32
|
|
|
* |
33
|
|
|
* @psalm-api |
34
|
|
|
*/ |
35
|
|
|
class Pagination implements Countable, IteratorAggregate |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Current page number of item collection. |
39
|
|
|
*/ |
40
|
|
|
private int $currentPageNumber = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* First page number of item collection. |
44
|
|
|
*/ |
45
|
|
|
private int $firstPageNumber = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Given a page range, first page number. |
49
|
|
|
*/ |
50
|
|
|
private int $firstPageNumberInRange = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Item collection. |
54
|
|
|
* |
55
|
|
|
* @var array<int> |
56
|
|
|
*/ |
57
|
|
|
private array $items = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Currently set amount of items we want per page. |
61
|
|
|
*/ |
62
|
|
|
private int $itemsPerPage = 10; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Last page number of item collection. |
66
|
|
|
*/ |
67
|
|
|
private int $lastPageNumber = 0; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Given a page range, last page number. |
71
|
|
|
*/ |
72
|
|
|
private int $lastPageNumberInRange = 0; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Optional metadata to include with pagination. |
76
|
|
|
* |
77
|
|
|
* @var array<int|string, string>|array<mixed> |
78
|
|
|
*/ |
79
|
|
|
private array $meta = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Next page number. Will be null if on the last page. |
83
|
|
|
*/ |
84
|
|
|
private ?int $nextPageNumber = null; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Page collection. |
88
|
|
|
* |
89
|
|
|
* @var array<int> |
90
|
|
|
*/ |
91
|
|
|
private array $pages = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Previous page number. Will be null if on the first page. |
95
|
|
|
*/ |
96
|
|
|
private ?int $previousPageNumber = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Total number of items in the collection. |
100
|
|
|
*/ |
101
|
|
|
private int $totalNumberOfItems = 0; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Total number of pages for the item collection. |
105
|
|
|
*/ |
106
|
|
|
private int $totalNumberOfPages = 0; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
2 |
|
public function count(): int |
112
|
|
|
{ |
113
|
2 |
|
return \count($this->items); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
public function getCurrentPageNumber(): int |
117
|
|
|
{ |
118
|
4 |
|
return $this->currentPageNumber; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
public function getFirstPageNumber(): int |
122
|
|
|
{ |
123
|
4 |
|
return $this->firstPageNumber; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
public function getFirstPageNumberInRange(): int |
127
|
|
|
{ |
128
|
4 |
|
return $this->firstPageNumberInRange; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the current collection. |
133
|
|
|
* |
134
|
|
|
* @return array<int> |
135
|
|
|
*/ |
136
|
5 |
|
public function getItems(): array |
137
|
|
|
{ |
138
|
5 |
|
return $this->items; |
139
|
|
|
} |
140
|
|
|
|
141
|
5 |
|
public function getItemsPerPage(): int |
142
|
|
|
{ |
143
|
5 |
|
return $this->itemsPerPage; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritDoc |
148
|
|
|
* |
149
|
|
|
* @return ArrayIterator<(int|string), int> |
150
|
|
|
*/ |
151
|
2 |
|
public function getIterator(): ArrayIterator |
152
|
|
|
{ |
153
|
2 |
|
return new ArrayIterator($this->items); |
154
|
|
|
} |
155
|
|
|
|
156
|
4 |
|
public function getLastPageNumber(): int |
157
|
|
|
{ |
158
|
4 |
|
return $this->lastPageNumber; |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
public function getLastPageNumberInRange(): int |
162
|
|
|
{ |
163
|
4 |
|
return $this->lastPageNumberInRange; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array<int|string, string>|array<mixed> |
168
|
|
|
*/ |
169
|
4 |
|
public function getMeta(): array |
170
|
|
|
{ |
171
|
4 |
|
return $this->meta; |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
public function getNextPageNumber(): ?int |
175
|
|
|
{ |
176
|
4 |
|
return $this->nextPageNumber; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array<int> |
181
|
|
|
*/ |
182
|
5 |
|
public function getPages(): array |
183
|
|
|
{ |
184
|
5 |
|
return $this->pages; |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
public function getPreviousPageNumber(): ?int |
188
|
|
|
{ |
189
|
4 |
|
return $this->previousPageNumber; |
190
|
|
|
} |
191
|
|
|
|
192
|
5 |
|
public function getTotalNumberOfItems(): int |
193
|
|
|
{ |
194
|
5 |
|
return $this->totalNumberOfItems; |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
public function getTotalNumberOfPages(): int |
198
|
|
|
{ |
199
|
4 |
|
return $this->totalNumberOfPages; |
200
|
|
|
} |
201
|
|
|
|
202
|
8 |
|
public function setCurrentPageNumber(int $currentPageNumber): static |
203
|
|
|
{ |
204
|
8 |
|
$this->currentPageNumber = $currentPageNumber; |
205
|
|
|
|
206
|
8 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
8 |
|
public function setFirstPageNumber(int $firstPageNumber): static |
210
|
|
|
{ |
211
|
8 |
|
$this->firstPageNumber = $firstPageNumber; |
212
|
|
|
|
213
|
8 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
8 |
|
public function setFirstPageNumberInRange(int $firstPageNumberInRange): static |
217
|
|
|
{ |
218
|
8 |
|
$this->firstPageNumberInRange = $firstPageNumberInRange; |
219
|
|
|
|
220
|
8 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Sets the item collection to be paginated. |
225
|
|
|
* |
226
|
|
|
* @param array<int> $items |
227
|
|
|
*/ |
228
|
8 |
|
public function setItems(array $items): static |
229
|
|
|
{ |
230
|
8 |
|
$this->items = $items; |
231
|
|
|
|
232
|
8 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
8 |
|
public function setItemsPerPage(int $itemsPerPage): static |
236
|
|
|
{ |
237
|
8 |
|
$this->itemsPerPage = $itemsPerPage; |
238
|
|
|
|
239
|
8 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
8 |
|
public function setLastPageNumber(int $lastPageNumber): static |
243
|
|
|
{ |
244
|
8 |
|
$this->lastPageNumber = $lastPageNumber; |
245
|
|
|
|
246
|
8 |
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
8 |
|
public function setLastPageNumberInRange(int $lastPageNumberInRange): static |
250
|
|
|
{ |
251
|
8 |
|
$this->lastPageNumberInRange = $lastPageNumberInRange; |
252
|
|
|
|
253
|
8 |
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param array<int|string, string>|array<mixed> $meta |
258
|
|
|
*/ |
259
|
4 |
|
public function setMeta(array $meta): static |
260
|
|
|
{ |
261
|
4 |
|
$this->meta = $meta; |
262
|
|
|
|
263
|
4 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
8 |
|
public function setNextPageNumber(?int $nextPageNumber): static |
267
|
|
|
{ |
268
|
8 |
|
$this->nextPageNumber = $nextPageNumber; |
269
|
|
|
|
270
|
8 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param array<int> $pages |
275
|
|
|
*/ |
276
|
8 |
|
public function setPages(array $pages): static |
277
|
|
|
{ |
278
|
8 |
|
$this->pages = $pages; |
279
|
|
|
|
280
|
8 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
8 |
|
public function setPreviousPageNumber(?int $previousPageNumber): static |
284
|
|
|
{ |
285
|
8 |
|
$this->previousPageNumber = $previousPageNumber; |
286
|
|
|
|
287
|
8 |
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
8 |
|
public function setTotalNumberOfItems(int $totalNumberOfItems): static |
291
|
|
|
{ |
292
|
8 |
|
$this->totalNumberOfItems = $totalNumberOfItems; |
293
|
|
|
|
294
|
8 |
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
8 |
|
public function setTotalNumberOfPages(int $totalNumberOfPages): static |
298
|
|
|
{ |
299
|
8 |
|
$this->totalNumberOfPages = $totalNumberOfPages; |
300
|
|
|
|
301
|
8 |
|
return $this; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|