This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
Abstract.php
504 lines (455 loc) · 14 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
abstract class CM_Paging_Abstract extends CM_Class_Abstract implements Iterator, Countable, CM_Cacheable {
private $_count = null;
private $_itemsRaw = null, $_items = array(), $_itemsRawTree = null;
private $_pageOffset = 0;
private $_pageSize = null;
private $_source = null;
private $_iteratorItems, $_iteratorPosition = 0;
private $_filters = array();
/** @var boolean */
private $_flattenItems = true;
/**
* @param CM_PagingSource_Abstract $source
*/
public function __construct(CM_PagingSource_Abstract $source = null) {
$this->_source = $source;
}
/**
* @param int|null $offset Negative: from end
* @param int|null $length
* @param bool|null $returnNonexistentItems
* @return array
*/
public function getItems($offset = null, $length = null, $returnNonexistentItems = false) {
$negativeOffset = false;
$itemsRaw = $this->_getItemsRaw();
// Count of available items
$count = count($itemsRaw);
if (null !== $this->_pageSize) {
$count = min($count, $this->_pageSize);
}
// Offset
if (null === $offset) {
$offset = 0;
}
if ($offset < 0) {
$negativeOffset = true;
$offset = $count - (-$offset);
}
$offset = max(0, min($offset, $count));
// Length
if (null === $length) {
$length = $count - $offset;
}
$length = max(0, min($length, $count - $offset));
if ($this->_canContainUnprocessableItems() || $this->_hasFilters() || $returnNonexistentItems || $negativeOffset) {
$items = $this->_getItems($offset, $length, $returnNonexistentItems, $negativeOffset);
} else {
$items = $this->_getItemsInstantiable($offset, $length);
}
return array_values($items);
}
/**
* @param int $countMax
* @return array
*/
public function getItemsEvenlyDistributed($countMax) {
$countMax = (int) $countMax;
$countMax = min($countMax, $this->getCount());
$count = $this->getCount();
$items = array();
if ($countMax > 0) {
$interval = ($countMax == 1) ? 1 : ($count - 1) / ($countMax - 1);
for ($i = 0; $i < $countMax; $i++) {
$index = (int) round($i * $interval);
$items[] = $this->getItem($index);
}
}
return $items;
}
/**
* Return Un-processed, un-filtered items
*
* @return array
*/
public function getItemsRaw() {
$itemsRaw = $this->_getItemsRaw();
if (null !== $this->_pageSize && count($itemsRaw) > $this->_pageSize) {
$itemsRaw = array_slice($itemsRaw, 0, $this->_pageSize);
}
return $itemsRaw;
}
/**
* @param string|null $keyName
* @return array[]
*/
public function getItemsRawTree($keyName = null) {
return CM_Util::getArrayTree($this->getItemsRaw(), 1, true, $keyName);
}
/**
* @param int $offset Negative: from end
* @return mixed|null Item at given index
*/
public function getItem($offset) {
$items = $this->getItems($offset, 1);
return array_shift($items);
}
/**
* @param float $mean Mean item index normalized between 0 and .5 (default)
* @throws CM_Exception_Invalid
* @throws CM_Exception_NotImplemented
* @return mixed|null
*/
public function getItemRand($mean = null) {
if (null !== $this->getPageSize()) {
throw new CM_Exception_Invalid('Can\'t get random item on a paged Paging.');
}
if (null === $mean) {
$mean = .5;
}
$mean = (float) $mean;
$p = rand(0, getrandmax() - 1) / getrandmax();
$N = $this->getCount();
if (.5 === $mean) {
$n = (int) floor($N * $p);
} else {
if ($mean < 0) {
throw new CM_Exception_Invalid('Normalized mean item index must be positive.');
}
if ($mean > .5) {
throw new CM_Exception_NotImplemented('Normalized mean item index cannot be greater than .5.');
}
if ($N <= 1) {
$n = 0;
} else {
$x = 1 + 1 / (($N - 1) * $mean);
$n = (int) floor(-log(1 - $p * (1 - pow($x, -$N))) / log($x));
}
}
$this->setPage($n + 1, 1);
$item = $this->getItem(0);
$this->_pageOffset = 0;
$this->_pageSize = null;
$this->_clearItems();
return $item;
}
/**
* @return int
*/
public function getCount() {
if ($this->_count === null && $this->_source) {
$count = ($this->_pageSize === null) ? null : (int) ceil($this->_pageSize * $this->_getPageFillRate());
$this->_setCount($this->_source->getCount($this->_getItemOffset(), $count));
}
return (int) $this->_count;
}
/**
* @param string $field
* @return int|float
* @throws CM_Exception_Invalid
*/
public function getSum($field) {
$field = (string) $field;
$sum = 0;
if ($this->_source) {
$itemsRaw = $this->_source->getItems(null, null);
foreach ($itemsRaw as $itemRaw) {
if (!array_key_exists($field, $itemRaw)) {
throw new CM_Exception_Invalid('Class has no requested field.', null, [
'className' => get_called_class(),
'field' => $field,
]);
}
$sum += $itemRaw[$field];
}
}
return $sum;
}
/**
* @return int
*/
public function getPage() {
return ($this->_pageOffset + 1);
}
/**
* @param int $page
* @param int $size
* @return CM_Paging_Abstract
*/
public function setPage($page, $size) {
$this->_clearItems();
$this->_pageOffset = max(((int) $page - 1), 0);
$this->_pageSize = max((int) $size, 0);
$this->_validatePageOffset();
return $this;
}
/**
* @return int|null
*/
public function getPageSize() {
return $this->_pageSize;
}
/**
* @return int
*/
public function getPageCount() {
if (!$this->getCount()) {
return 0;
}
if (!$this->_pageSize) {
return 0;
}
return (int) ceil($this->getCount() / $this->_pageSize);
}
/**
* @return bool
*/
public function isEmpty() {
return $this->getCount() == 0;
}
/**
* Filter result of getItems() by a callback
*
* @param Closure $filter function(mixed $item): boolean
*/
public function filter(Closure $filter) {
$this->_clearItems();
$this->_filters[] = $filter;
}
/**
* @param array|mixed $items
* @return static
*/
public function exclude($items) {
if (!is_array($items)) {
$items = array($items);
}
if (count($items) === 0) {
return $this;
}
$comparable = true;
foreach ($items as $item) {
$comparable &= ($item instanceof CM_Comparable);
}
if ($comparable) {
$filter = function ($item) use ($items) {
foreach ($items as $itemExcluded) {
if ($item->equals($itemExcluded)) {
return false;
}
}
return true;
};
} else {
$filter = function ($item) use ($items) {
return !in_array($item, $items);
};
}
$this->filter($filter);
return $this;
}
/**
* Items in the underlying source have changed
*/
public function _change() {
if (!$this->_source) {
throw new CM_Exception('Cannot change paging without source');
}
$this->_source->clearCache();
$this->_clearItems();
$this->_clearCount();
}
/**
* @param boolean $state
*/
public function setFlattenItems($state) {
$this->_flattenItems = (bool) $state;
}
/**
* @return bool
*/
protected function _canContainUnprocessableItems() {
return ($this->_getStalenessChance() != 0);
}
protected function _clearItems() {
$this->_items = array();
$this->_itemsRaw = null;
$this->_itemsRawTree = null;
$this->_iteratorPosition = 0;
$this->_iteratorItems = null;
}
/**
* @return int Multiple of items per page to load from CM_PagingSource_Abstract
*/
protected function _getPageFillRate() {
return 1 + $this->_getStalenessChance();
}
/**
* @return float Chance that an item contains stale (non-processable) data (0-1)
*/
protected function _getStalenessChance() {
if ($this->_source) {
return $this->_source->getStalenessChance();
}
return 0;
}
/**
* @param mixed $itemRaw
* @return mixed Processed item
* @throws CM_Exception_Nonexistent
*/
protected function _processItem($itemRaw) {
return $itemRaw;
}
/**
* @return array Raw items (might contain more than $this->_pageSize)
*/
protected function _getItemsRaw() {
if ($this->_itemsRaw === null) {
$this->_itemsRaw = array();
if ($this->_source) {
$count = ($this->_pageSize === null) ? null : (int) ceil($this->_pageSize * $this->_getPageFillRate());
$itemsRaw = $this->_source->getItems($this->_getItemOffset(), $count);
foreach ($itemsRaw as &$itemRaw) {
if ($this->_flattenItems) {
if (is_array($itemRaw) && count($itemRaw) == 1) {
$itemRaw = reset($itemRaw);
}
}
$this->_itemsRaw[] = $itemRaw;
}
}
}
return $this->_itemsRaw;
}
/**
* @param mixed $item
* @return boolean Whether the item is matched by any of the registered filters
*/
private function _isFilterMatch($item) {
foreach ($this->_filters as $filter) {
if (false === $filter($item)) {
return true;
}
}
return false;
}
/**
* @return bool
*/
private function _hasFilters() {
return count($this->_filters) > 0;
}
/**
* @return int OR null if no pageSize set
*/
private function _getItemOffset() {
if ($this->_pageSize === null) {
return null;
}
return (int) $this->_pageOffset * $this->_pageSize;
}
private function _clearCount() {
$this->_count = null;
}
/**
* @param int $offset
* @param int $length
* @param bool $returnNonexistentItems
* @param bool $allowBackwardsLookup
* @return array
*/
private function _getItems($offset, $length, $returnNonexistentItems, $allowBackwardsLookup) {
$itemsRaw = $this->_getItemsRaw();
$itemsRawCount = count($itemsRaw);
$items = array();
$direction = 1;
$i = 0;
while (count($items) < $length) {
$index = $offset + ($i * $direction);
if (array_key_exists($index, $this->_items)) {
$item = $this->_items[$index];
} else {
try {
$item = $this->_processItem($itemsRaw[$index]);
} catch (CM_Exception_Nonexistent $e) {
$item = null;
}
$this->_items[$index] = $item;
}
if ((is_null($item) && $returnNonexistentItems) || (!is_null($item) && !$this->_isFilterMatch($item))) {
$items[$index] = $item;
}
if (0 == $index && -1 == $direction) {
break;
}
if ($index == $itemsRawCount - 1) {
if ($offset == 0 || !$allowBackwardsLookup) {
break;
}
$i = 0;
$direction = -1;
}
$i++;
}
ksort($items);
return $items;
}
/**
* @param int $offset
* @param int $length
* @return array
*/
private function _getItemsInstantiable($offset, $length) {
$itemsRaw = $this->_getItemsRaw();
$items = array();
for ($i = $offset; $i < $offset + $length; $i++) {
if (!array_key_exists($i, $this->_items)) {
$this->_items[$i] = $this->_processItem($itemsRaw[$i]);
}
$items[$i] = $this->_items[$i];
}
return $items;
}
/**
* @param int $count
* @return CM_Paging_Abstract
*/
public function _setCount($count) {
$this->_count = max((int) $count, 0);
$this->_validatePageOffset();
return $this;
}
private function _validatePageOffset() {
if ($this->_pageSize !== null) {
if ($this->_pageOffset * $this->_pageSize >= $this->getCount()) {
if ($this->_pageSize == 0 || $this->getCount() == 0) {
$this->_pageOffset = 0;
} else {
$this->_pageOffset = (int) ceil($this->getCount() / $this->_pageSize) - 1;
}
}
}
}
/* Iterator functions */
function rewind() {
$this->_iteratorItems = $this->getItems();
$this->_iteratorPosition = 0;
}
function current() {
return $this->_iteratorItems[$this->_iteratorPosition];
}
function key() {
return $this->_iteratorPosition;
}
function next() {
++$this->_iteratorPosition;
}
function valid() {
return isset($this->_iteratorItems[$this->_iteratorPosition]);
}
public function count() {
return $this->getCount();
}
}