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