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