1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Pagination; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Bdf\Prime\Exception\PrimeException; |
7
|
|
|
use Bdf\Prime\PrimeSerializable; |
8
|
|
|
use Bdf\Prime\Query\Contract\ReadOperation; |
9
|
|
|
use Bdf\Prime\Query\Pagination\WalkStrategy\PaginationWalkStrategy; |
10
|
|
|
use Bdf\Prime\Query\Pagination\WalkStrategy\WalkCursor; |
11
|
|
|
use Bdf\Prime\Query\Pagination\WalkStrategy\WalkStrategyInterface; |
12
|
|
|
use Bdf\Prime\Query\ReadCommandInterface; |
13
|
|
|
use LogicException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Query Walker |
17
|
|
|
* |
18
|
|
|
* Permet de parcourir des collections contenant de gros volume d'entités. |
19
|
|
|
* Le parcourt se fait par paquet d'entités définis par la limit de la query |
20
|
|
|
* Une fois la limite atteinte, la classe lance la requête suivante |
|
|
|
|
21
|
|
|
* |
22
|
|
|
* Attention, le walker ne gère pas les objects collection |
23
|
|
|
* |
24
|
|
|
* @author Seb |
|
|
|
|
25
|
|
|
* @package Bdf\Prime\Query\Pagination |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
class Walker extends PrimeSerializable implements \Iterator, PaginatorInterface |
28
|
|
|
{ |
29
|
|
|
const DEFAULT_PAGE = 1; |
30
|
|
|
const DEFAULT_LIMIT = 150; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* First page |
34
|
|
|
* |
35
|
|
|
* @var int |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
protected $startPage; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The current offset |
41
|
|
|
* |
42
|
|
|
* @var int |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
protected $offset; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array|null |
48
|
|
|
*/ |
49
|
|
|
private $collection; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var WalkStrategyInterface |
53
|
|
|
*/ |
54
|
|
|
private $strategy; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var WalkCursor |
58
|
|
|
*/ |
59
|
|
|
private $cursor; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var ReadCommandInterface |
63
|
|
|
*/ |
64
|
|
|
private $query; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var int |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
private $page; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var int |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
private $maxRows; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create a query walker |
78
|
|
|
* |
79
|
|
|
* @param ReadCommandInterface $query |
80
|
|
|
* @param int $maxRows |
|
|
|
|
81
|
|
|
* @param int $page |
|
|
|
|
82
|
|
|
*/ |
83
|
27 |
|
public function __construct(ReadCommandInterface $query, $maxRows = null, $page = null) |
84
|
|
|
{ |
85
|
27 |
|
$this->query = $query; |
86
|
27 |
|
$this->page = 0; |
87
|
27 |
|
$this->maxRows = $maxRows ?: self::DEFAULT_LIMIT; |
|
|
|
|
88
|
27 |
|
$this->startPage = $page ?: self::DEFAULT_PAGE; |
|
|
|
|
89
|
27 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Change the walk strategy |
93
|
|
|
* |
94
|
|
|
* @param WalkStrategyInterface $strategy |
95
|
|
|
* |
96
|
|
|
* @return Walker |
97
|
|
|
*/ |
|
|
|
|
98
|
21 |
|
public function setStrategy(WalkStrategyInterface $strategy): self |
99
|
|
|
{ |
100
|
21 |
|
if ($this->cursor !== null) { |
101
|
|
|
throw new LogicException('Cannot change walk strategy during walk'); |
102
|
|
|
} |
103
|
|
|
|
104
|
21 |
|
$this->strategy = $strategy; |
105
|
|
|
|
106
|
21 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the current active walk strategy |
111
|
|
|
* |
112
|
|
|
* @return WalkStrategyInterface |
113
|
|
|
*/ |
114
|
24 |
|
public function getStrategy(): WalkStrategyInterface |
115
|
|
|
{ |
116
|
24 |
|
if ($this->strategy) { |
117
|
17 |
|
return $this->strategy; |
118
|
|
|
} |
119
|
|
|
|
120
|
8 |
|
return $this->strategy = new PaginationWalkStrategy(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Load the first page of collection |
125
|
|
|
* |
126
|
|
|
* @throws PrimeException |
127
|
|
|
*/ |
128
|
|
|
#[ReadOperation] |
|
|
|
|
129
|
23 |
|
public function load() |
|
|
|
|
130
|
|
|
{ |
131
|
23 |
|
$this->page = $this->startPage; |
132
|
23 |
|
$this->cursor = $this->getStrategy()->initialize($this->query, $this->maxRows, $this->page); |
133
|
20 |
|
$this->loadCollection(); |
134
|
20 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return ReadCommandInterface |
138
|
|
|
*/ |
139
|
2 |
|
public function query(): ReadCommandInterface |
140
|
|
|
{ |
141
|
2 |
|
if ($this->cursor) { |
142
|
1 |
|
return $this->cursor->query; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return $this->query; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function collection() |
152
|
|
|
{ |
153
|
|
|
return $this->collection; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
5 |
|
public function size() |
160
|
|
|
{ |
161
|
5 |
|
return $this->query->paginationCount(); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function order($attribute = null) |
168
|
|
|
{ |
169
|
|
|
$orders = $this->query()->getOrders(); |
|
|
|
|
170
|
|
|
|
171
|
|
|
if ($attribute === null) { |
172
|
|
|
return $orders; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return isset($orders[$attribute]) ? $orders[$attribute] : null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
3 |
|
public function limit() |
182
|
|
|
{ |
183
|
3 |
|
return $this->cursor->query->getLimit(); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
1 |
|
public function offset() |
190
|
|
|
{ |
191
|
1 |
|
return $this->cursor->query->getOffset(); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
3 |
|
public function page() |
198
|
|
|
{ |
199
|
3 |
|
return $this->page; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
3 |
|
public function pageMaxRows() |
206
|
|
|
{ |
207
|
3 |
|
return $this->maxRows; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
20 |
|
protected function loadCollection() |
214
|
|
|
{ |
215
|
20 |
|
$this->cursor = $this->strategy->next($this->cursor); |
216
|
20 |
|
$this->collection = $this->cursor->entities; |
217
|
|
|
|
218
|
|
|
// Test if the collection has numerical keys. |
219
|
|
|
// We have to add the offset to the numerical key. |
220
|
20 |
|
if (isset($this->collection[0])) { |
221
|
18 |
|
$this->offset = ($this->page - $this->startPage) * $this->maxRows; |
|
|
|
|
222
|
|
|
} else { |
223
|
17 |
|
$this->offset = null; |
224
|
|
|
} |
225
|
20 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* SPL - Iterator |
229
|
|
|
* |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
16 |
|
public function current() |
233
|
|
|
{ |
234
|
16 |
|
return current($this->collection); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* SPL - Iterator |
239
|
|
|
* |
240
|
|
|
* {@inheritdoc} |
241
|
|
|
*/ |
242
|
11 |
|
public function key() |
243
|
|
|
{ |
244
|
11 |
|
if ($this->offset !== null) { |
245
|
10 |
|
return $this->offset + key($this->collection); |
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
return key($this->collection); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* SPL - Iterator |
253
|
|
|
* |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
* |
256
|
|
|
* @throws PrimeException |
257
|
|
|
*/ |
258
|
|
|
#[ReadOperation] |
|
|
|
|
259
|
16 |
|
public function next() |
|
|
|
|
260
|
|
|
{ |
261
|
16 |
|
if (false === next($this->collection)) { |
|
|
|
|
262
|
16 |
|
$this->page++; |
263
|
16 |
|
$this->loadCollection(); |
264
|
|
|
} |
265
|
16 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* SPL - Iterator |
269
|
|
|
* |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
16 |
|
public function valid() |
273
|
|
|
{ |
274
|
16 |
|
return false !== current($this->collection); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* SPL - Iterator |
279
|
|
|
* |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
* |
282
|
|
|
* @throws PrimeException |
283
|
|
|
*/ |
284
|
|
|
#[ReadOperation] |
|
|
|
|
285
|
19 |
|
public function rewind() |
|
|
|
|
286
|
|
|
{ |
287
|
19 |
|
if (($this->page == $this->startPage) && count($this->collection)) { |
|
|
|
|
288
|
|
|
reset($this->collection); |
|
|
|
|
289
|
|
|
} else { |
290
|
19 |
|
$this->load(); |
291
|
|
|
} |
292
|
16 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* {@inheritdoc} |
296
|
|
|
*/ |
297
|
3 |
|
public function count() |
298
|
|
|
{ |
299
|
3 |
|
return count($this->collection); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
|
|
|
|
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
|
|
|
|
305
|
|
|
public function pushAll(array $items) |
306
|
|
|
{ |
307
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
|
|
|
|
311
|
|
|
* {@inheritdoc} |
312
|
|
|
*/ |
|
|
|
|
313
|
|
|
public function push($item) |
314
|
|
|
{ |
315
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
|
|
|
|
319
|
|
|
* {@inheritdoc} |
320
|
|
|
*/ |
|
|
|
|
321
|
|
|
public function put($key, $item) |
322
|
|
|
{ |
323
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* {@inheritdoc} |
328
|
|
|
*/ |
|
|
|
|
329
|
|
|
public function all() |
330
|
|
|
{ |
331
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
|
|
|
|
335
|
|
|
* {@inheritdoc} |
336
|
|
|
*/ |
|
|
|
|
337
|
|
|
public function get($key, $default = null) |
338
|
|
|
{ |
339
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
|
|
|
|
343
|
|
|
* {@inheritdoc} |
344
|
|
|
*/ |
|
|
|
|
345
|
|
|
public function has($key) |
346
|
|
|
{ |
347
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
|
|
|
|
351
|
|
|
* {@inheritdoc} |
352
|
|
|
*/ |
|
|
|
|
353
|
|
|
public function remove($key) |
354
|
|
|
{ |
355
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* {@inheritdoc} |
360
|
|
|
*/ |
|
|
|
|
361
|
|
|
public function clear() |
362
|
|
|
{ |
363
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
*/ |
|
|
|
|
369
|
|
|
public function keys() |
370
|
|
|
{ |
371
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* {@inheritdoc} |
376
|
|
|
*/ |
|
|
|
|
377
|
|
|
public function isEmpty() |
378
|
|
|
{ |
379
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
|
|
|
|
383
|
|
|
* {@inheritdoc} |
384
|
|
|
*/ |
|
|
|
|
385
|
1 |
|
public function map($callback) |
386
|
|
|
{ |
387
|
1 |
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
|
|
|
|
391
|
|
|
* {@inheritdoc} |
392
|
|
|
*/ |
|
|
|
|
393
|
|
|
public function filter($callback = null) |
394
|
|
|
{ |
395
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
|
|
|
|
399
|
|
|
* {@inheritdoc} |
400
|
|
|
*/ |
|
|
|
|
401
|
|
|
public function groupBy($groupBy, $mode = self::GROUPBY) |
402
|
|
|
{ |
403
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
|
|
|
|
407
|
|
|
* {@inheritdoc} |
408
|
|
|
*/ |
|
|
|
|
409
|
|
|
public function contains($element) |
410
|
|
|
{ |
411
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
|
|
|
|
415
|
|
|
* {@inheritdoc} |
416
|
|
|
*/ |
|
|
|
|
417
|
|
|
public function indexOf($value, $strict = false) |
418
|
|
|
{ |
419
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
|
|
|
|
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
|
|
|
|
425
|
|
|
public function merge($items) |
426
|
|
|
{ |
427
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
|
|
|
|
431
|
|
|
* {@inheritdoc} |
432
|
|
|
*/ |
|
|
|
|
433
|
|
|
public function sort(callable $callback = null) |
434
|
|
|
{ |
435
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
|
|
|
|
439
|
|
|
* {@inheritdoc} |
440
|
|
|
*/ |
|
|
|
|
441
|
|
|
public function offsetExists($offset) |
442
|
|
|
{ |
443
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
|
|
|
|
447
|
|
|
* {@inheritdoc} |
448
|
|
|
*/ |
|
|
|
|
449
|
|
|
public function offsetGet($offset) |
450
|
|
|
{ |
451
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
|
|
|
|
455
|
|
|
* {@inheritdoc} |
456
|
|
|
*/ |
|
|
|
|
457
|
|
|
public function offsetSet($offset, $value) |
458
|
|
|
{ |
459
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
|
|
|
|
463
|
|
|
* {@inheritdoc} |
464
|
|
|
*/ |
|
|
|
|
465
|
|
|
public function offsetUnset($offset) |
466
|
|
|
{ |
467
|
|
|
throw new BadMethodCallException('Collection methods are not supported by the Walker'); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|