1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Pagination; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Collection\CollectionInterface; |
6
|
|
|
use Bdf\Prime\Exception\PrimeException; |
7
|
|
|
use Bdf\Prime\PrimeSerializable; |
8
|
|
|
use Bdf\Prime\Query\QueryInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Abstract paginator |
12
|
|
|
* |
13
|
|
|
* Provide method for pagination |
|
|
|
|
14
|
|
|
* |
15
|
|
|
* @author Seb |
|
|
|
|
16
|
|
|
* @package Bdf\Prime\Query\Pagination |
|
|
|
|
17
|
|
|
* |
18
|
|
|
* |
19
|
|
|
* @todo retourner une nouvelle instance du paginator sur les methodes de collection ? |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractPaginator extends PrimeSerializable |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_PAGE = 1; |
24
|
|
|
const DEFAULT_LIMIT = 20; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Current query |
28
|
|
|
* |
29
|
|
|
* @var QueryInterface |
30
|
|
|
*/ |
31
|
|
|
protected $query; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Current collection |
35
|
|
|
* |
36
|
|
|
* @var array|CollectionInterface |
37
|
|
|
*/ |
38
|
|
|
protected $collection = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Total size of the collection |
42
|
|
|
* |
43
|
|
|
* @var int |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
protected $size; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Current page |
49
|
|
|
* |
50
|
|
|
* @var int |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
protected $page; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Number of entities loaded in the collection |
56
|
|
|
* |
57
|
|
|
* @var int |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
protected $maxRows; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
7 |
|
public function collection() |
|
|
|
|
66
|
|
|
{ |
67
|
7 |
|
return $this->collection; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the query |
72
|
|
|
* |
73
|
|
|
* @return QueryInterface |
74
|
|
|
*/ |
75
|
2 |
|
public function query() |
76
|
|
|
{ |
77
|
2 |
|
return $this->query; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Load entities |
82
|
|
|
* |
83
|
|
|
* @throws PrimeException |
|
|
|
|
84
|
|
|
*/ |
85
|
25 |
|
protected function loadCollection() |
86
|
|
|
{ |
87
|
25 |
|
if ($this->maxRows > -1) { |
88
|
25 |
|
$this->query->limitPage($this->page, $this->maxRows); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
25 |
|
$this->collection = $this->query->all(); |
92
|
25 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
2 |
|
public function order($attribute = null) |
98
|
|
|
{ |
99
|
2 |
|
$orders = $this->query->getOrders(); |
|
|
|
|
100
|
|
|
|
101
|
2 |
|
if ($attribute === null) { |
102
|
2 |
|
return $orders; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return isset($orders[$attribute]) ? $orders[$attribute] : null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
public function limit() |
112
|
|
|
{ |
113
|
1 |
|
return $this->query->getLimit(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function offset() |
120
|
|
|
{ |
121
|
1 |
|
return $this->query->getOffset(); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
4 |
|
public function page() |
128
|
|
|
{ |
129
|
4 |
|
return $this->query->getPage(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
4 |
|
public function pageMaxRows() |
136
|
|
|
{ |
137
|
4 |
|
return $this->query->getLimit(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
8 |
|
public function size() |
144
|
|
|
{ |
145
|
8 |
|
if ($this->size === null) { |
146
|
8 |
|
$this->buildSize(); |
147
|
|
|
} |
148
|
|
|
|
149
|
8 |
|
return $this->size; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Find size of the collection |
154
|
|
|
*/ |
155
|
8 |
|
protected function buildSize() |
156
|
|
|
{ |
157
|
8 |
|
$currentSize = $this->count(); |
158
|
|
|
|
159
|
8 |
|
if (!$this->query->isLimitQuery()) { |
|
|
|
|
160
|
3 |
|
$this->size = $currentSize; |
161
|
5 |
|
} elseif ($currentSize + $this->query->getOffset() < $this->query->getLimit()) { |
|
|
|
|
162
|
1 |
|
$this->size = $currentSize + $this->query->getOffset(); |
|
|
|
|
163
|
|
|
} else { |
164
|
4 |
|
$this->size = $this->query->paginationCount(); |
|
|
|
|
165
|
|
|
} |
166
|
8 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* SPL - Countable |
170
|
|
|
* |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
9 |
|
public function count() |
174
|
|
|
{ |
175
|
9 |
|
return count($this->collection); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
//--------- collection interface |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
|
|
|
|
183
|
|
|
public function pushAll(array $items) |
184
|
|
|
{ |
185
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
186
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->collection->pushAll($items); |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
|
|
|
|
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
|
|
|
|
197
|
|
|
public function push($item) |
198
|
|
|
{ |
199
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
200
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->collection->push($item); |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
|
|
|
|
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
|
|
|
|
211
|
|
|
public function put($key, $item) |
212
|
|
|
{ |
213
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
214
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$this->collection->put($key, $item); |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
|
|
|
|
223
|
|
|
* SPL - ArrayAccess |
224
|
|
|
* |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function offsetSet($key, $value) |
228
|
|
|
{ |
229
|
|
|
$this->collection[$key] = $value; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
|
|
public function all() |
236
|
|
|
{ |
237
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
238
|
|
|
return $this->collection; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this->collection->all(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
|
|
|
|
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
|
|
|
|
247
|
7 |
|
public function get($key, $default = null) |
248
|
|
|
{ |
249
|
7 |
|
if (!($this->collection instanceof CollectionInterface)) { |
250
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
251
|
|
|
} |
252
|
|
|
|
253
|
7 |
|
return $this->collection->get($key, $default); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
|
|
|
|
257
|
|
|
* SPL - ArrayAccess |
258
|
|
|
* |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function offsetGet($key) |
262
|
|
|
{ |
263
|
|
|
return $this->collection[$key]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
|
|
|
|
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
|
|
|
|
269
|
|
|
public function has($key) |
270
|
|
|
{ |
271
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
272
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->collection->has($key); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
|
|
|
|
279
|
|
|
* SPL - ArrayAccess |
280
|
|
|
* |
281
|
|
|
* {@inheritdoc} |
282
|
|
|
*/ |
283
|
|
|
public function offsetExists($key) |
284
|
|
|
{ |
285
|
|
|
return isset($this->collection[$key]); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
|
|
|
|
289
|
|
|
* {@inheritdoc} |
290
|
|
|
*/ |
|
|
|
|
291
|
|
|
public function remove($key) |
292
|
|
|
{ |
293
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
294
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$this->collection->remove($key); |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
|
|
|
|
303
|
|
|
* SPL - ArrayAccess |
304
|
|
|
* |
305
|
|
|
* {@inheritdoc} |
306
|
|
|
*/ |
307
|
|
|
public function offsetUnset($key) |
308
|
|
|
{ |
309
|
|
|
unset($this->collection[$key]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* {@inheritdoc} |
314
|
|
|
*/ |
|
|
|
|
315
|
1 |
|
public function clear() |
316
|
|
|
{ |
317
|
1 |
|
if (!($this->collection instanceof CollectionInterface)) { |
318
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
319
|
|
|
} |
320
|
|
|
|
321
|
1 |
|
$this->collection->clear(); |
322
|
|
|
|
323
|
1 |
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* {@inheritdoc} |
328
|
|
|
*/ |
|
|
|
|
329
|
|
|
public function keys() |
330
|
|
|
{ |
331
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
332
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this->collection->keys(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritdoc} |
340
|
|
|
*/ |
|
|
|
|
341
|
2 |
|
public function isEmpty() |
342
|
|
|
{ |
343
|
2 |
|
if (!($this->collection instanceof CollectionInterface)) { |
344
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
345
|
|
|
} |
346
|
|
|
|
347
|
2 |
|
return $this->collection->isEmpty(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
|
|
|
|
351
|
|
|
* {@inheritdoc} |
352
|
|
|
*/ |
|
|
|
|
353
|
1 |
|
public function map($callback) |
354
|
|
|
{ |
355
|
1 |
|
if (!($this->collection instanceof CollectionInterface)) { |
356
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
357
|
|
|
} |
358
|
|
|
|
359
|
1 |
|
$this->collection = $this->collection->map($callback); |
360
|
|
|
|
361
|
1 |
|
return $this; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
|
|
|
|
365
|
|
|
* {@inheritdoc} |
366
|
|
|
*/ |
|
|
|
|
367
|
1 |
|
public function filter($callback = null) |
368
|
|
|
{ |
369
|
1 |
|
if (!($this->collection instanceof CollectionInterface)) { |
370
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
371
|
|
|
} |
372
|
|
|
|
373
|
1 |
|
$this->collection = $this->collection->filter($callback); |
374
|
|
|
|
375
|
1 |
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
|
|
|
|
379
|
|
|
* {@inheritdoc} |
380
|
|
|
*/ |
|
|
|
|
381
|
1 |
|
public function groupBy($groupBy, $mode = PaginatorInterface::GROUPBY) |
382
|
|
|
{ |
383
|
1 |
|
if (!($this->collection instanceof CollectionInterface)) { |
384
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
385
|
|
|
} |
386
|
|
|
|
387
|
1 |
|
$this->collection = $this->collection->groupBy($groupBy, $mode); |
388
|
|
|
|
389
|
1 |
|
return $this; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
|
|
|
|
393
|
|
|
* {@inheritdoc} |
394
|
|
|
*/ |
|
|
|
|
395
|
|
|
public function contains($element) |
396
|
|
|
{ |
397
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
398
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
return $this->collection->contains($element); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
|
|
|
|
405
|
|
|
* {@inheritdoc} |
406
|
|
|
*/ |
|
|
|
|
407
|
|
|
public function indexOf($value, $strict = false) |
408
|
|
|
{ |
409
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
410
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $this->collection->indexOf($value, $strict); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
|
|
|
|
417
|
|
|
* {@inheritdoc} |
418
|
|
|
*/ |
|
|
|
|
419
|
|
|
public function merge($items) |
420
|
|
|
{ |
421
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
422
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$this->collection = $this->collection->merge($items); |
426
|
|
|
|
427
|
|
|
return $this; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
|
|
|
|
431
|
|
|
* {@inheritdoc} |
432
|
|
|
*/ |
|
|
|
|
433
|
|
|
public function sort(callable $callback = null) |
434
|
|
|
{ |
435
|
|
|
if (!($this->collection instanceof CollectionInterface)) { |
436
|
|
|
throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$this->collection = $this->collection->sort($callback); |
440
|
|
|
|
441
|
|
|
return $this; |
442
|
|
|
} |
443
|
|
|
} |