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