1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Data\Paginator; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
11
|
|
|
use Yiisoft\Data\Reader\Filter\GreaterThan; |
12
|
|
|
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual; |
13
|
|
|
use Yiisoft\Data\Reader\Filter\LessThan; |
14
|
|
|
use Yiisoft\Data\Reader\Filter\LessThanOrEqual; |
15
|
|
|
use Yiisoft\Data\Reader\FilterableDataInterface; |
16
|
|
|
use Yiisoft\Data\Reader\FilterInterface; |
17
|
|
|
use Yiisoft\Data\Reader\LimitableDataInterface; |
18
|
|
|
use Yiisoft\Data\Reader\ReadableDataInterface; |
19
|
|
|
use Yiisoft\Data\Reader\Sort; |
20
|
|
|
use Yiisoft\Data\Reader\SortableDataInterface; |
21
|
|
|
|
22
|
|
|
use function array_reverse; |
23
|
|
|
use function count; |
24
|
|
|
use function key; |
25
|
|
|
use function reset; |
26
|
|
|
use function sprintf; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Keyset paginator. |
30
|
|
|
* |
31
|
|
|
* Advantages: |
32
|
|
|
* |
33
|
|
|
* - Performance does not depend on page number |
34
|
|
|
* - Consistent results regardless of insertions and deletions |
35
|
|
|
* |
36
|
|
|
* Disadvantages: |
37
|
|
|
* |
38
|
|
|
* - Total number of pages is not available |
39
|
|
|
* - Can not get to specific page, only "previous" and "next" |
40
|
|
|
* - Data cannot be unordered |
41
|
|
|
* |
42
|
|
|
* @link https://use-the-index-luke.com/no-offset |
43
|
|
|
* |
44
|
|
|
* @template TKey as array-key |
45
|
|
|
* @template TValue as array|object |
46
|
|
|
* |
47
|
|
|
* @implements PaginatorInterface<TKey, TValue> |
48
|
|
|
* |
49
|
|
|
* @psalm-type FilterCallback = Closure(GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual,KeysetFilterContext):FilterInterface |
50
|
|
|
*/ |
51
|
|
|
final class KeysetPaginator implements PaginatorInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Data reader being paginated. |
55
|
|
|
* |
56
|
|
|
* @psalm-var ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface |
57
|
|
|
*/ |
58
|
|
|
private ReadableDataInterface $dataReader; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int Maximum number of items per page. |
62
|
|
|
*/ |
63
|
|
|
private int $pageSize = self::DEFAULT_PAGE_SIZE; |
64
|
|
|
private ?string $firstValue = null; |
65
|
|
|
private ?string $lastValue = null; |
66
|
|
|
private ?string $currentFirstValue = null; |
67
|
|
|
private ?string $currentLastValue = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool Whether there is previous page. |
71
|
|
|
*/ |
72
|
|
|
private bool $hasPreviousPage = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool Whether there is next page. |
76
|
|
|
*/ |
77
|
|
|
private bool $hasNextPage = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @psalm-var FilterCallback|null |
81
|
|
|
*/ |
82
|
|
|
private ?Closure $filterCallback = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Reader cache against repeated scans. |
86
|
|
|
* See more {@see __clone()} and {@see initialize()}. |
87
|
|
|
* |
88
|
|
|
* @psalm-var null|array<TKey, TValue> |
89
|
|
|
*/ |
90
|
|
|
private ?array $readCache = null; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ReadableDataInterface $dataReader Data reader being paginated. |
94
|
|
|
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader |
95
|
|
|
* @psalm-suppress DocblockTypeContradiction Needed to allow validating `$dataReader` |
96
|
|
|
*/ |
97
|
44 |
|
public function __construct(ReadableDataInterface $dataReader) |
98
|
|
|
{ |
99
|
44 |
|
if (!$dataReader instanceof FilterableDataInterface) { |
100
|
1 |
|
throw new InvalidArgumentException(sprintf( |
101
|
1 |
|
'Data reader should implement "%s" to be used with keyset paginator.', |
102
|
1 |
|
FilterableDataInterface::class, |
103
|
1 |
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
43 |
|
if (!$dataReader instanceof SortableDataInterface) { |
107
|
1 |
|
throw new InvalidArgumentException(sprintf( |
108
|
1 |
|
'Data reader should implement "%s" to be used with keyset paginator.', |
109
|
1 |
|
SortableDataInterface::class, |
110
|
1 |
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
42 |
|
if (!$dataReader instanceof LimitableDataInterface) { |
114
|
1 |
|
throw new InvalidArgumentException(sprintf( |
115
|
1 |
|
'Data reader should implement "%s" to be used with keyset paginator.', |
116
|
1 |
|
LimitableDataInterface::class, |
117
|
1 |
|
)); |
118
|
|
|
} |
119
|
|
|
|
120
|
41 |
|
$sort = $dataReader->getSort(); |
121
|
|
|
|
122
|
41 |
|
if ($sort === null) { |
123
|
1 |
|
throw new RuntimeException('Data sorting should be configured to work with keyset pagination.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
40 |
|
if (empty($sort->getOrder())) { |
127
|
1 |
|
throw new RuntimeException('Data should be always sorted to work with keyset pagination.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
39 |
|
$this->dataReader = $dataReader; |
131
|
|
|
} |
132
|
|
|
|
133
|
36 |
|
public function __clone() |
134
|
|
|
{ |
135
|
36 |
|
$this->readCache = null; |
136
|
36 |
|
$this->hasPreviousPage = false; |
137
|
36 |
|
$this->hasNextPage = false; |
138
|
36 |
|
$this->currentFirstValue = null; |
139
|
36 |
|
$this->currentLastValue = null; |
140
|
|
|
} |
141
|
|
|
|
142
|
12 |
|
public function withNextPageToken(?string $token): static |
143
|
|
|
{ |
144
|
12 |
|
$new = clone $this; |
145
|
12 |
|
$new->firstValue = null; |
146
|
12 |
|
$new->lastValue = $token; |
147
|
12 |
|
return $new; |
148
|
|
|
} |
149
|
|
|
|
150
|
8 |
|
public function withPreviousPageToken(?string $token): static |
151
|
|
|
{ |
152
|
8 |
|
$new = clone $this; |
153
|
8 |
|
$new->firstValue = $token; |
154
|
8 |
|
$new->lastValue = null; |
155
|
8 |
|
return $new; |
156
|
|
|
} |
157
|
|
|
|
158
|
32 |
|
public function withPageSize(int $pageSize): static |
159
|
|
|
{ |
160
|
32 |
|
if ($pageSize < 1) { |
161
|
1 |
|
throw new InvalidArgumentException('Page size should be at least 1.'); |
162
|
|
|
} |
163
|
|
|
|
164
|
31 |
|
$new = clone $this; |
165
|
31 |
|
$new->pageSize = $pageSize; |
166
|
31 |
|
return $new; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns a new instance with defined closure for preparing the page value before use in data reader filters. |
171
|
|
|
* |
172
|
|
|
* @psalm-param FilterCallback|null $callback |
173
|
|
|
*/ |
174
|
2 |
|
public function withFilterCallback(?Closure $callback): self |
175
|
|
|
{ |
176
|
2 |
|
$new = clone $this; |
177
|
2 |
|
$new->filterCallback = $callback; |
178
|
2 |
|
return $new; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Reads items of the page. |
183
|
|
|
* |
184
|
|
|
* This method uses the read cache to prevent duplicate reads from the data source. See more {@see resetInternal()}. |
185
|
|
|
*/ |
186
|
35 |
|
public function read(): iterable |
187
|
|
|
{ |
188
|
35 |
|
if ($this->readCache !== null) { |
189
|
2 |
|
return $this->readCache; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** @var Sort $sort */ |
193
|
35 |
|
$sort = $this->dataReader->getSort(); |
|
|
|
|
194
|
|
|
/** @infection-ignore-all Any value more one in line below will be ignored into `readData()` method */ |
195
|
35 |
|
$dataReader = $this->dataReader->withLimit($this->pageSize + 1); |
|
|
|
|
196
|
|
|
|
197
|
35 |
|
if ($this->isGoingToPreviousPage()) { |
198
|
7 |
|
$sort = $this->reverseSort($sort); |
199
|
7 |
|
$dataReader = $dataReader->withSort($sort); |
200
|
|
|
} |
201
|
|
|
|
202
|
35 |
|
if ($this->isGoingSomewhere()) { |
203
|
11 |
|
$dataReader = $dataReader->withFilter($this->getFilter($sort)); |
204
|
11 |
|
$this->hasPreviousPage = $this->previousPageExist($dataReader, $sort); |
205
|
|
|
} |
206
|
|
|
|
207
|
35 |
|
$data = $this->readData($dataReader, $sort); |
208
|
|
|
|
209
|
35 |
|
if ($this->isGoingToPreviousPage()) { |
210
|
7 |
|
$data = $this->reverseData($data); |
211
|
|
|
} |
212
|
|
|
|
213
|
35 |
|
return $this->readCache = $data; |
214
|
|
|
} |
215
|
|
|
|
216
|
2 |
|
public function readOne(): array|object|null |
217
|
|
|
{ |
218
|
2 |
|
foreach ($this->read() as $item) { |
219
|
1 |
|
return $item; |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
return null; |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
public function getPageSize(): int |
226
|
|
|
{ |
227
|
2 |
|
return $this->pageSize; |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
public function getCurrentPageSize(): int |
231
|
|
|
{ |
232
|
2 |
|
$this->initialize(); |
233
|
2 |
|
return count($this->readCache); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
3 |
|
public function getPreviousPageToken(): ?string |
237
|
|
|
{ |
238
|
3 |
|
return $this->isOnFirstPage() ? null : $this->currentFirstValue; |
239
|
|
|
} |
240
|
|
|
|
241
|
9 |
|
public function getNextPageToken(): ?string |
242
|
|
|
{ |
243
|
9 |
|
return $this->isOnLastPage() ? null : $this->currentLastValue; |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
public function getSort(): ?Sort |
247
|
|
|
{ |
248
|
|
|
/** @psalm-var SortableDataInterface $this->dataReader */ |
249
|
1 |
|
return $this->dataReader->getSort(); |
250
|
|
|
} |
251
|
|
|
|
252
|
26 |
|
public function isOnFirstPage(): bool |
253
|
|
|
{ |
254
|
26 |
|
if ($this->lastValue === null && $this->firstValue === null) { |
255
|
20 |
|
return true; |
256
|
|
|
} |
257
|
|
|
|
258
|
6 |
|
$this->initialize(); |
259
|
6 |
|
return !$this->hasPreviousPage; |
260
|
|
|
} |
261
|
|
|
|
262
|
27 |
|
public function isOnLastPage(): bool |
263
|
|
|
{ |
264
|
27 |
|
$this->initialize(); |
265
|
27 |
|
return !$this->hasNextPage; |
266
|
|
|
} |
267
|
|
|
|
268
|
3 |
|
public function isPaginationRequired(): bool |
269
|
|
|
{ |
270
|
3 |
|
return !$this->isOnFirstPage() || !$this->isOnLastPage(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @psalm-assert array<TKey, TValue> $this->readCache |
275
|
|
|
*/ |
276
|
29 |
|
private function initialize(): void |
277
|
|
|
{ |
278
|
29 |
|
if ($this->readCache !== null) { |
279
|
12 |
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
21 |
|
$cache = []; |
283
|
|
|
|
284
|
21 |
|
foreach ($this->read() as $key => $value) { |
285
|
16 |
|
$cache[$key] = $value; |
286
|
|
|
} |
287
|
|
|
|
288
|
21 |
|
$this->readCache = $cache; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @psalm-param ReadableDataInterface<TKey, TValue> $dataReader |
293
|
|
|
* @psalm-return array<TKey, TValue> |
294
|
|
|
*/ |
295
|
35 |
|
private function readData(ReadableDataInterface $dataReader, Sort $sort): array |
296
|
|
|
{ |
297
|
35 |
|
$data = []; |
298
|
35 |
|
[$field] = $this->getFieldAndSortingFromSort($sort); |
299
|
|
|
|
300
|
35 |
|
foreach ($dataReader->read() as $key => $item) { |
301
|
29 |
|
if ($this->currentFirstValue === null) { |
302
|
29 |
|
$this->currentFirstValue = (string) ArrayHelper::getValue($item, $field); |
303
|
|
|
} |
304
|
|
|
|
305
|
29 |
|
if (count($data) === $this->pageSize) { |
306
|
16 |
|
$this->hasNextPage = true; |
307
|
|
|
} else { |
308
|
29 |
|
$this->currentLastValue = (string) ArrayHelper::getValue($item, $field); |
309
|
29 |
|
$data[$key] = $item; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
35 |
|
return $data; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @psalm-param array<TKey, TValue> $data |
318
|
|
|
* @psalm-return array<TKey, TValue> |
319
|
|
|
*/ |
320
|
7 |
|
private function reverseData(array $data): array |
321
|
|
|
{ |
322
|
7 |
|
[$this->currentFirstValue, $this->currentLastValue] = [$this->currentLastValue, $this->currentFirstValue]; |
323
|
7 |
|
[$this->hasPreviousPage, $this->hasNextPage] = [$this->hasNextPage, $this->hasPreviousPage]; |
324
|
7 |
|
return array_reverse($data, true); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader |
329
|
|
|
*/ |
330
|
12 |
|
private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort): bool |
331
|
|
|
{ |
332
|
12 |
|
$reverseFilter = $this->getReverseFilter($sort); |
333
|
|
|
|
334
|
12 |
|
return !empty($dataReader->withFilter($reverseFilter)->readOne()); |
|
|
|
|
335
|
|
|
} |
336
|
|
|
|
337
|
12 |
|
private function getFilter(Sort $sort): FilterInterface |
338
|
|
|
{ |
339
|
12 |
|
$value = $this->getValue(); |
340
|
12 |
|
[$field, $sorting] = $this->getFieldAndSortingFromSort($sort); |
341
|
|
|
|
342
|
12 |
|
$filter = $sorting === SORT_ASC ? new GreaterThan($field, $value) : new LessThan($field, $value); |
343
|
12 |
|
if ($this->filterCallback === null) { |
344
|
11 |
|
return $filter; |
345
|
|
|
} |
346
|
|
|
|
347
|
1 |
|
return ($this->filterCallback)( |
348
|
1 |
|
$filter, |
349
|
1 |
|
new KeysetFilterContext( |
350
|
1 |
|
$field, |
351
|
1 |
|
$value, |
352
|
1 |
|
$sorting, |
353
|
1 |
|
false, |
354
|
1 |
|
) |
355
|
1 |
|
); |
356
|
|
|
} |
357
|
|
|
|
358
|
13 |
|
private function getReverseFilter(Sort $sort): FilterInterface |
359
|
|
|
{ |
360
|
13 |
|
$value = $this->getValue(); |
361
|
13 |
|
[$field, $sorting] = $this->getFieldAndSortingFromSort($sort); |
362
|
|
|
|
363
|
13 |
|
$filter = $sorting === SORT_ASC ? new LessThanOrEqual($field, $value) : new GreaterThanOrEqual($field, $value); |
364
|
13 |
|
if ($this->filterCallback === null) { |
365
|
12 |
|
return $filter; |
366
|
|
|
} |
367
|
|
|
|
368
|
1 |
|
return ($this->filterCallback)( |
369
|
1 |
|
$filter, |
370
|
1 |
|
new KeysetFilterContext( |
371
|
1 |
|
$field, |
372
|
1 |
|
$value, |
373
|
1 |
|
$sorting, |
374
|
1 |
|
true, |
375
|
1 |
|
) |
376
|
1 |
|
); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @psalm-suppress NullableReturnStatement, InvalidNullableReturnType, PossiblyNullArgument The code calling this |
381
|
|
|
* method must ensure that at least one of the properties `$firstValue` or `$lastValue` is not `null`. |
382
|
|
|
*/ |
383
|
14 |
|
private function getValue(): string |
384
|
|
|
{ |
385
|
14 |
|
return $this->isGoingToPreviousPage() ? $this->firstValue : $this->lastValue; |
|
|
|
|
386
|
|
|
} |
387
|
|
|
|
388
|
7 |
|
private function reverseSort(Sort $sort): Sort |
389
|
|
|
{ |
390
|
7 |
|
$order = $sort->getOrder(); |
391
|
|
|
|
392
|
7 |
|
foreach ($order as &$sorting) { |
393
|
7 |
|
$sorting = $sorting === 'asc' ? 'desc' : 'asc'; |
394
|
|
|
} |
395
|
|
|
|
396
|
7 |
|
return $sort->withOrder($order); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @psalm-return array{0: string, 1: int} |
401
|
|
|
*/ |
402
|
38 |
|
private function getFieldAndSortingFromSort(Sort $sort): array |
403
|
|
|
{ |
404
|
38 |
|
$order = $sort->getOrder(); |
405
|
|
|
|
406
|
38 |
|
return [ |
407
|
38 |
|
(string) key($order), |
408
|
38 |
|
reset($order) === 'asc' ? SORT_ASC : SORT_DESC, |
409
|
38 |
|
]; |
410
|
|
|
} |
411
|
|
|
|
412
|
38 |
|
private function isGoingToPreviousPage(): bool |
413
|
|
|
{ |
414
|
38 |
|
return $this->firstValue !== null && $this->lastValue === null; |
415
|
|
|
} |
416
|
|
|
|
417
|
35 |
|
private function isGoingSomewhere(): bool |
418
|
|
|
{ |
419
|
35 |
|
return $this->firstValue !== null || $this->lastValue !== null; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|