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