1
|
|
|
<?php |
2
|
|
|
namespace Suricate; |
3
|
|
|
|
4
|
|
|
class Collection implements \IteratorAggregate, \Countable, \ArrayAccess, Interfaces\ICollection |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
protected $items = []; |
8
|
|
|
protected $mapping = []; // to be deprecated ? |
9
|
|
|
|
10
|
|
|
const ITEM_TYPE = ''; |
11
|
|
|
|
12
|
|
|
public $pagination = [ |
13
|
|
|
'nbPages' => 0, |
14
|
|
|
'page' => 1, |
15
|
|
|
'nbItems' => 0, |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
//protected $iteratorPosition = 0; |
19
|
|
|
|
20
|
23 |
|
public function __construct($items = []) |
21
|
|
|
{ |
22
|
23 |
|
$this->items = $items; |
23
|
23 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function paginate($nbItemPerPage, $currentPage = 1) |
26
|
|
|
{ |
27
|
1 |
|
$this->pagination['page'] = $currentPage; |
28
|
1 |
|
$this->pagination['nbItems'] = count($this->items); |
29
|
1 |
|
$this->pagination['nbPages'] = ceil($this->pagination['nbItems'] / $nbItemPerPage); |
30
|
|
|
|
31
|
1 |
|
$this->items = array_slice($this->items, ($currentPage - 1) * $nbItemPerPage, $nbItemPerPage); |
32
|
|
|
|
33
|
1 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getPossibleValuesFor($args, $key = null) |
37
|
|
|
{ |
38
|
|
|
if (!is_array($args)) { |
39
|
|
|
$args = [ |
40
|
|
|
'format' => '%s', |
41
|
|
|
'data' => [$args] |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$values = []; |
46
|
|
|
foreach ($this->items as $item) { |
47
|
|
|
$itemValues = []; |
48
|
|
|
foreach ($args['data'] as $arg) { |
49
|
|
|
$itemValues[] = dataGet($item, $arg); |
50
|
|
|
} |
51
|
|
|
$arrayKey = ($key !== null) ? dataGet($item, $key) : null; |
52
|
|
|
$values[$arrayKey] = vsprintf($args['format'], $itemValues); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $values; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function getValuesFor($name) |
59
|
|
|
{ |
60
|
1 |
|
$values = []; |
61
|
1 |
|
foreach ($this->items as $item) { |
62
|
1 |
|
$values[] = dataGet($item, $name); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $values; |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
public function getItems() |
69
|
|
|
{ |
70
|
12 |
|
return $this->items; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/*public function addItemLink($linkId) |
74
|
|
|
{ |
75
|
|
|
$this->items[$this->itemOffset] = $linkId; |
76
|
|
|
// add mapping between item->index and $position in items pool |
77
|
|
|
$this->mapping[$this->itemOffset] = $linkId; |
78
|
|
|
|
79
|
|
|
$this->itemOffset++; |
80
|
|
|
}*/ |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
public function getItemFromKey($key) |
85
|
|
|
{ |
86
|
|
|
$invertedMapping = array_flip($this->mapping); |
87
|
|
|
if (isset($invertedMapping[$key])) { |
88
|
|
|
return $this->items[$invertedMapping[$key]]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Implementation of countable interface |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
1 |
|
public function count(): int |
99
|
|
|
{ |
100
|
1 |
|
return count($this->items); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Implementation of IteratorAggregate Interface |
105
|
|
|
* |
106
|
|
|
* @return \ArrayIterator |
107
|
|
|
*/ |
108
|
1 |
|
public function getIterator(): \ArrayIterator |
109
|
|
|
{ |
110
|
1 |
|
return new \ArrayIterator($this->items); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Implementation of ArrayAccess interface |
115
|
|
|
* |
116
|
|
|
* @param mixed $offset Offset to verify |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
1 |
|
public function offsetExists($offset): bool |
120
|
|
|
{ |
121
|
1 |
|
return isset($this->items[$offset]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Implementation of ArrayAccess Interface |
126
|
|
|
* |
127
|
|
|
* @param mixed $offset Offset to get |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function offsetGet($offset) |
131
|
|
|
{ |
132
|
|
|
$item =isset($this->items[$offset]) ? $this->items[$offset] : null; |
133
|
|
|
if (gettype($item) == 'object' || $item == null) { |
134
|
|
|
return $item; |
135
|
|
|
} |
136
|
|
|
// Lazy load |
137
|
|
|
$itemType = $this::ITEM_TYPE; |
138
|
|
|
$itemToLoad = new $itemType; |
139
|
|
|
$itemToLoad->load($this->items[$offset]); |
140
|
|
|
|
141
|
|
|
$this->items[$offset] = $itemToLoad; |
142
|
|
|
|
143
|
|
|
return $this->items[$offset]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Implementation of ArrayAccess Interface |
148
|
|
|
* |
149
|
|
|
* @param mixed $offset Offset to set |
150
|
|
|
* @param mixed $value Value to set |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function offsetSet($offset, $value): void |
154
|
|
|
{ |
155
|
|
|
if (is_null($offset)) { |
156
|
|
|
$this->items[] = $value; |
157
|
|
|
} else { |
158
|
|
|
$this->items[$offset] = $value; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Implementation of ArrayAccess Interface |
164
|
|
|
* |
165
|
|
|
* @param mixed $offset Offset to unset |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function offsetUnset($offset): void |
169
|
|
|
{ |
170
|
|
|
unset($this->items[$offset]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function cleanStr($str) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
|
176
|
|
|
$str = mb_strtolower($str, 'utf-8'); |
177
|
|
|
$str = strtr( |
178
|
|
|
$str, |
179
|
|
|
[ |
180
|
|
|
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'a'=>'a', 'a'=>'a', 'a'=>'a', 'ç'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'd'=>'d', 'd'=>'d', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'g'=>'g', 'g'=>'g', 'g'=>'g', 'h'=>'h', 'h'=>'h', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', '?'=>'i', 'j'=>'j', 'k'=>'k', '?'=>'k', 'l'=>'l', 'l'=>'l', 'l'=>'l', '?'=>'l', 'l'=>'l', 'ñ'=>'n', 'n'=>'n', 'n'=>'n', 'n'=>'n', '?'=>'n', '?'=>'n', 'ð'=>'o', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'o'=>'o', 'o'=>'o', 'o'=>'o', 'œ'=>'o', 'ø'=>'o', 'r'=>'r', 'r'=>'r', 's'=>'s', 's'=>'s', 's'=>'s', 'š'=>'s', '?'=>'s', 't'=>'t', 't'=>'t', 't'=>'t', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'w'=>'w', 'ý'=>'y', 'ÿ'=>'y', 'y'=>'y', 'z'=>'z', 'z'=>'z', 'ž'=>'z' |
181
|
|
|
] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
return $str; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// to be deprecated |
188
|
|
|
public function getFirstItem() |
189
|
|
|
{ |
190
|
|
|
foreach ($this->items as $currentItem) { |
191
|
|
|
return $currentItem; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// to be deprecated |
196
|
|
|
public function getRandom($nb = 1) |
197
|
|
|
{ |
198
|
|
|
$keys = (array) array_rand($this->items, $nb); |
199
|
|
|
$result = []; |
200
|
|
|
foreach ($keys as $currentKey) { |
201
|
|
|
$result[$currentKey] = $this->items[$currentKey]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $result; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Helpers |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get first item of the collection |
211
|
|
|
* |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
1 |
|
public function first() |
215
|
|
|
{ |
216
|
1 |
|
foreach ($this->items as $currentItem) { |
217
|
1 |
|
return $currentItem; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get last item of the collection |
223
|
|
|
* |
224
|
|
|
* @return mixed |
225
|
|
|
*/ |
226
|
1 |
|
public function last() |
227
|
|
|
{ |
228
|
1 |
|
if (count($this->items)) { |
229
|
1 |
|
return end($this->items); |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Check if collection is empty |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
1 |
|
public function isEmpty(): bool |
241
|
|
|
{ |
242
|
1 |
|
return empty($this->items); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return the sum of the collection |
247
|
|
|
* |
248
|
|
|
* @param mixed $field Field to use for sum |
249
|
|
|
* @return double|integer |
250
|
|
|
*/ |
251
|
1 |
|
public function sum($field = null) |
252
|
|
|
{ |
253
|
1 |
|
if ($field === null) { |
254
|
1 |
|
return array_sum($this->items); |
255
|
|
|
} |
256
|
1 |
|
$result = 0; |
257
|
1 |
|
foreach ($this->items as $item) { |
258
|
1 |
|
$result += dataGet($item, $field); |
259
|
|
|
} |
260
|
1 |
|
return $result; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function random($nbItems = 1) |
264
|
|
|
{ |
265
|
|
|
if ($this->isEmpty()) { |
266
|
|
|
return null; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$keys = array_rand($this->items, $nbItems); |
270
|
|
|
|
271
|
|
|
if (is_array($keys)) { |
272
|
|
|
return array_intersect_key($this->items, array_flip($keys)); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->items[$keys]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function shuffle() |
279
|
|
|
{ |
280
|
|
|
shuffle($this->items); |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
public function unique() |
286
|
|
|
{ |
287
|
1 |
|
return new static(array_unique($this->items)); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Apply a closure to each element of the collection |
292
|
|
|
* |
293
|
|
|
* @param \Closure $callback Closure to apply |
294
|
|
|
* @return Collection |
295
|
|
|
*/ |
296
|
|
|
public function each(\Closure $callback): Collection |
297
|
|
|
{ |
298
|
|
|
array_map($callback, $this->items); |
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Sort a collection using a closure |
304
|
|
|
* |
305
|
|
|
* @param \Closure $closure Closure to apply for sorting, similar to uasort() closure |
306
|
|
|
* @return Collection |
307
|
|
|
*/ |
308
|
|
|
public function sort(\Closure $closure): Collection |
309
|
|
|
{ |
310
|
|
|
uasort($this->items, $closure); |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function sortBy($field, $reverse = false) |
316
|
|
|
{ |
317
|
|
|
if ($reverse) { |
318
|
|
|
$sortFunction = function ($a, $b) use ($field) { |
319
|
|
|
$first = dataGet($a, $field); |
320
|
|
|
$second = dataGet($b, $field); |
321
|
|
|
if ($first == $second) { |
322
|
|
|
return 0; |
323
|
|
|
} |
324
|
|
|
return ($first > $second) ? -1 : 1; |
325
|
|
|
}; |
326
|
|
|
} else { |
327
|
|
|
$sortFunction = function ($a, $b) use ($field) { |
328
|
|
|
$first = dataGet($a, $field); |
329
|
|
|
$second = dataGet($b, $field); |
330
|
|
|
if ($first == $second) { |
331
|
|
|
return 0; |
332
|
|
|
} |
333
|
|
|
return ($first < $second) ? -1 : 1; |
334
|
|
|
}; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
usort($this->items, $sortFunction); |
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
1 |
|
public function filter(\Closure $closure) |
344
|
|
|
{ |
345
|
1 |
|
return new static(array_filter($this->items, $closure)); |
346
|
|
|
} |
347
|
|
|
|
348
|
1 |
|
public function search($value, $strict = false) |
349
|
|
|
{ |
350
|
1 |
|
return array_search($value, $this->items, $strict); |
351
|
|
|
} |
352
|
|
|
|
353
|
1 |
|
public function has($key) |
354
|
|
|
{ |
355
|
1 |
|
return $this->offsetExists($key); |
356
|
|
|
} |
357
|
|
|
|
358
|
1 |
|
public function keys() |
359
|
|
|
{ |
360
|
1 |
|
return array_keys($this->items); |
361
|
|
|
} |
362
|
|
|
|
363
|
1 |
|
public function prepend($item) |
364
|
|
|
{ |
365
|
1 |
|
array_unshift($this->items, $item); |
366
|
|
|
|
367
|
1 |
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
1 |
|
public function push($item) |
371
|
|
|
{ |
372
|
1 |
|
$this->items[] = $item; |
373
|
|
|
|
374
|
1 |
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
1 |
|
public function put($key, $val) |
378
|
|
|
{ |
379
|
1 |
|
$this->items[$key] = $val; |
380
|
|
|
|
381
|
1 |
|
return $this; |
382
|
|
|
} |
383
|
1 |
|
public function shift() |
384
|
|
|
{ |
385
|
1 |
|
return array_shift($this->items); |
386
|
|
|
} |
387
|
|
|
|
388
|
1 |
|
public function pop() |
389
|
|
|
{ |
390
|
1 |
|
return array_pop($this->items); |
391
|
|
|
} |
392
|
|
|
|
393
|
1 |
|
public function reverse() |
394
|
|
|
{ |
395
|
1 |
|
return new static(array_reverse($this->items)); |
396
|
|
|
} |
397
|
|
|
|
398
|
1 |
|
public function reduce(callable $callback, $initial = null) |
399
|
|
|
{ |
400
|
1 |
|
return array_reduce($this->items, $callback, $initial); |
401
|
|
|
} |
402
|
|
|
|
403
|
2 |
|
public function slice($offset, $length = null, $preserveKeys = false) |
404
|
|
|
{ |
405
|
2 |
|
return new static(array_slice($this->items, $offset, $length, $preserveKeys)); |
406
|
|
|
} |
407
|
|
|
|
408
|
1 |
|
public function take($limit = null) |
409
|
|
|
{ |
410
|
1 |
|
if ($limit < 0) { |
411
|
|
|
return $this->slice(abs($limit), $limit); |
412
|
|
|
} |
413
|
|
|
|
414
|
1 |
|
return $this->slice(0, $limit); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public function splice($offset, $length = null, $replacement = []) |
418
|
|
|
{ |
419
|
|
|
return new static(array_splice($this->items, $offset, $length, $replacement)); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function chunk($size, $preserveKeys = false) |
423
|
|
|
{ |
424
|
|
|
$result = new static; |
425
|
|
|
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) { |
426
|
|
|
$result->push(new static($chunk)); |
427
|
|
|
} |
428
|
|
|
return $result; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.