|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrayy; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use voku\helper\UTF8; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Methods to manage arrays. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Arrayy extends CollectionMethods implements \Countable, \IteratorAggregate, \ArrayAccess |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $array = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Initializes |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $array |
|
23
|
|
|
*/ |
|
24
|
285 |
|
public function __construct($array = array()) |
|
25
|
|
|
{ |
|
26
|
285 |
|
if (!$array) { |
|
|
|
|
|
|
27
|
69 |
|
$array = array(); |
|
28
|
69 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ( |
|
31
|
285 |
|
is_string($array) |
|
32
|
|
|
|| |
|
33
|
285 |
|
(is_object($array) && method_exists($array, '__toString')) |
|
34
|
285 |
|
) { |
|
35
|
1 |
|
$array = (array)$array; |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
285 |
|
if (!is_array($array)) { |
|
39
|
2 |
|
throw new \InvalidArgumentException( |
|
40
|
|
|
'Passed value must be a array' |
|
41
|
2 |
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
283 |
|
$this->array = $array; |
|
45
|
283 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* magic to string |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
13 |
|
public function __toString() |
|
53
|
|
|
{ |
|
54
|
13 |
|
return $this->implode(','); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get a data by key |
|
59
|
|
|
* |
|
60
|
|
|
* @param $key |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function &__get($key) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->array[$key]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Assigns a value to the specified data |
|
71
|
|
|
* |
|
72
|
|
|
* @param $key |
|
73
|
|
|
* @param $value |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __set($key, $value) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->array[$key] = $value; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Whether or not an data exists by key |
|
82
|
|
|
* |
|
83
|
|
|
* @param $key |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __isset($key) |
|
88
|
|
|
{ |
|
89
|
|
|
return isset($this->array[$key]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Unsets an data by key |
|
94
|
|
|
* |
|
95
|
|
|
* @param mixed $key |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __unset($key) |
|
98
|
|
|
{ |
|
99
|
|
|
unset($this->array[$key]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Assigns a value to the specified offset |
|
104
|
|
|
* |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $offset |
|
107
|
|
|
* @param mixed $value |
|
108
|
|
|
*/ |
|
109
|
|
|
public function offsetSet($offset, $value) |
|
110
|
|
|
{ |
|
111
|
|
|
if (null === $offset) { |
|
112
|
|
|
$this->array[] = $value; |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->array[$offset] = $value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Whether or not an offset exists |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $offset |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public function offsetExists($offset) |
|
126
|
|
|
{ |
|
127
|
4 |
|
return isset($this->array[$offset]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Unsets an offset |
|
132
|
|
|
* |
|
133
|
|
|
* @param mixed $offset |
|
134
|
|
|
*/ |
|
135
|
|
|
public function offsetUnset($offset) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->offsetExists($offset)) { |
|
138
|
|
|
unset($this->array[$offset]); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the value at specified offset |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $offset |
|
146
|
|
|
* |
|
147
|
|
|
* @return null |
|
148
|
|
|
*/ |
|
149
|
4 |
|
public function offsetGet($offset) |
|
150
|
|
|
{ |
|
151
|
4 |
|
return $this->offsetExists($offset) ? $this->array[$offset] : null; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns a new ArrayIterator, thus implementing the IteratorAggregate |
|
156
|
|
|
* interface. |
|
157
|
|
|
* |
|
158
|
|
|
* @return \ArrayIterator An iterator for the values in the array |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function getIterator() |
|
161
|
|
|
{ |
|
162
|
1 |
|
return new \ArrayIterator($this->array); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* call object as function |
|
167
|
|
|
* |
|
168
|
|
|
* @param mixed $key |
|
169
|
|
|
* |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
public function __invoke($key = null) |
|
173
|
|
|
{ |
|
174
|
|
|
if ($key !== null) { |
|
175
|
|
|
if (isset($this->array[$key])) { |
|
176
|
|
|
return $this->array[$key]; |
|
177
|
|
|
} else { |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return (array)$this->array; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* get the current array from the "Arrayy"-object |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
184 |
|
public function getArray() |
|
191
|
|
|
{ |
|
192
|
184 |
|
return $this->array; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Creates a Arrayy object |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $array |
|
199
|
|
|
* |
|
200
|
|
|
* @return self |
|
201
|
|
|
*/ |
|
202
|
225 |
|
public static function create($array = array()) |
|
203
|
|
|
{ |
|
204
|
225 |
|
return new static($array); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
//////////////////////////////////////////////////////////////////// |
|
208
|
|
|
///////////////////////////// ANALYZE ////////////////////////////// |
|
209
|
|
|
//////////////////////////////////////////////////////////////////// |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Search for the value of the current array via $index. |
|
213
|
|
|
* |
|
214
|
|
|
* @param mixed $index |
|
215
|
|
|
* |
|
216
|
|
|
* @return self |
|
217
|
|
|
*/ |
|
218
|
7 |
|
public function searchValue($index) |
|
219
|
|
|
{ |
|
220
|
|
|
// init |
|
221
|
7 |
|
$return = array(); |
|
222
|
|
|
|
|
223
|
7 |
|
if (null !== $index) { |
|
224
|
7 |
|
$keyExists = isset($this->array[$index]); |
|
225
|
|
|
|
|
226
|
7 |
|
if ($keyExists !== false) { |
|
227
|
5 |
|
$return = array($this->array[$index]); |
|
228
|
5 |
|
} |
|
229
|
7 |
|
} |
|
230
|
|
|
|
|
231
|
7 |
|
return self::create((array)$return); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Search for the index of the current array via $value. |
|
236
|
|
|
* |
|
237
|
|
|
* @param mixed $value |
|
238
|
|
|
* |
|
239
|
|
|
* @return self |
|
240
|
|
|
*/ |
|
241
|
7 |
|
public function searchIndex($value) |
|
242
|
|
|
{ |
|
243
|
7 |
|
$key = array_search($value, $this->array, true); |
|
244
|
|
|
|
|
245
|
7 |
|
if ($key === false) { |
|
246
|
2 |
|
$return = array(); |
|
247
|
2 |
|
} else { |
|
248
|
5 |
|
$return = array($key); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
7 |
|
return self::create((array)$return); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Check if all items in an array match a truth test. |
|
256
|
|
|
* |
|
257
|
|
|
* @param \Closure $closure |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
8 |
View Code Duplication |
public function matches(\Closure $closure) |
|
|
|
|
|
|
262
|
|
|
{ |
|
263
|
|
|
// Reduce the array to only booleans |
|
264
|
8 |
|
$array = $this->each($closure); |
|
265
|
|
|
|
|
266
|
|
|
// Check the results |
|
267
|
8 |
|
if (count($array) === 0) { |
|
268
|
2 |
|
return true; |
|
269
|
|
|
} |
|
270
|
6 |
|
$array = array_search(false, $array, false); |
|
271
|
|
|
|
|
272
|
6 |
|
return is_bool($array); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Check if any item in an array matches a truth test. |
|
277
|
|
|
* |
|
278
|
|
|
* @param \Closure $closure |
|
279
|
|
|
* |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
8 |
View Code Duplication |
public function matchesAny(\Closure $closure) |
|
|
|
|
|
|
283
|
|
|
{ |
|
284
|
|
|
// Reduce the array to only booleans |
|
285
|
8 |
|
$array = $this->each($closure); |
|
286
|
|
|
|
|
287
|
|
|
// Check the results |
|
288
|
8 |
|
if (count($array) === 0) { |
|
289
|
2 |
|
return true; |
|
290
|
|
|
} |
|
291
|
6 |
|
$array = array_search(true, $array, false); |
|
292
|
|
|
|
|
293
|
6 |
|
return is_int($array); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Check if an item is in an array. |
|
298
|
|
|
* |
|
299
|
|
|
* @param mixed $value |
|
300
|
|
|
* |
|
301
|
|
|
* @return bool |
|
302
|
|
|
*/ |
|
303
|
9 |
|
public function contains($value) |
|
304
|
|
|
{ |
|
305
|
9 |
|
return in_array($value, $this->array, true); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Returns the average value of an array. |
|
310
|
|
|
* |
|
311
|
|
|
* @param int $decimals The number of decimals to return |
|
312
|
|
|
* |
|
313
|
|
|
* @return int The average value |
|
314
|
|
|
*/ |
|
315
|
10 |
|
public function average($decimals = null) |
|
316
|
|
|
{ |
|
317
|
10 |
|
$count = $this->count(); |
|
318
|
|
|
|
|
319
|
10 |
|
if (!$count) { |
|
320
|
2 |
|
return 0; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
8 |
|
if (!is_int($decimals)) { |
|
324
|
3 |
|
$decimals = null; |
|
325
|
3 |
|
} |
|
326
|
|
|
|
|
327
|
8 |
|
return round(array_sum($this->array) / $count, $decimals); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Count the values from the current array. |
|
332
|
|
|
* |
|
333
|
|
|
* INFO: only a alias for "$arrayy->size()" |
|
334
|
|
|
* |
|
335
|
|
|
* @return int |
|
336
|
|
|
*/ |
|
337
|
|
|
public function length() |
|
338
|
|
|
{ |
|
339
|
|
|
return $this->size(); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Count the values from the current array. |
|
344
|
|
|
* |
|
345
|
|
|
* INFO: only a alias for "$arrayy->size()" |
|
346
|
|
|
* |
|
347
|
|
|
* @return int |
|
348
|
|
|
*/ |
|
349
|
40 |
|
public function count() |
|
350
|
|
|
{ |
|
351
|
40 |
|
return $this->size(); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Get the size of an array. |
|
356
|
|
|
* |
|
357
|
|
|
* @return int |
|
358
|
|
|
*/ |
|
359
|
40 |
|
public function size() |
|
360
|
|
|
{ |
|
361
|
40 |
|
return count($this->array); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Get the max value from an array. |
|
366
|
|
|
* |
|
367
|
|
|
* @return mixed |
|
368
|
|
|
*/ |
|
369
|
10 |
|
public function max() |
|
370
|
|
|
{ |
|
371
|
10 |
|
if ($this->count() === 0) { |
|
372
|
1 |
|
return false; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
9 |
|
return max($this->array); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Get the min value from an array. |
|
380
|
|
|
* |
|
381
|
|
|
* @return mixed |
|
382
|
|
|
*/ |
|
383
|
10 |
|
public function min() |
|
384
|
|
|
{ |
|
385
|
10 |
|
if ($this->count() === 0) { |
|
386
|
1 |
|
return false; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
9 |
|
return min($this->array); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
//////////////////////////////////////////////////////////////////// |
|
393
|
|
|
//////////////////////////// FETCH FROM //////////////////////////// |
|
394
|
|
|
//////////////////////////////////////////////////////////////////// |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Find the first item in an array that passes the truth test, |
|
398
|
|
|
* otherwise return false |
|
399
|
|
|
* |
|
400
|
|
|
* @param \Closure $closure |
|
401
|
|
|
* |
|
402
|
|
|
* @return mixed|false false if we couldn't find the value |
|
403
|
|
|
*/ |
|
404
|
7 |
|
public function find(\Closure $closure) |
|
405
|
|
|
{ |
|
406
|
7 |
|
foreach ($this->array as $key => $value) { |
|
407
|
5 |
|
if ($closure($value, $key)) { |
|
408
|
4 |
|
return $value; |
|
409
|
|
|
} |
|
410
|
4 |
|
} |
|
411
|
|
|
|
|
412
|
3 |
|
return false; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Clean all falsy values from an array. |
|
417
|
|
|
* |
|
418
|
|
|
* @return self |
|
419
|
|
|
*/ |
|
420
|
7 |
|
public function clean() |
|
421
|
|
|
{ |
|
422
|
7 |
|
return $this->filter( |
|
423
|
|
|
function ($value) { |
|
424
|
6 |
|
return (bool)$value; |
|
425
|
|
|
} |
|
426
|
7 |
|
); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Get a random string from an array. |
|
431
|
|
|
* |
|
432
|
|
|
* @param null $take |
|
433
|
|
|
* |
|
434
|
|
|
* @return self |
|
435
|
|
|
*/ |
|
436
|
5 |
|
public function random($take = null) |
|
437
|
|
|
{ |
|
438
|
5 |
|
if ($take !== null) { |
|
439
|
|
|
return $this->array[array_rand($this->array)]; |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
5 |
|
shuffle($this->array); |
|
443
|
|
|
|
|
444
|
5 |
|
return $this->first($take); |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Return an array with all elements found in input array. |
|
449
|
|
|
* |
|
450
|
|
|
* @param array $search |
|
451
|
|
|
* |
|
452
|
|
|
* @return self |
|
453
|
|
|
*/ |
|
454
|
2 |
|
public function intersection(array $search) |
|
455
|
|
|
{ |
|
456
|
2 |
|
return self::create(array_values(array_intersect($this->array, $search))); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Return a boolean flag which indicates whether the two input arrays have any common elements. |
|
461
|
|
|
* |
|
462
|
|
|
* @param array $search |
|
463
|
|
|
* |
|
464
|
|
|
* @return bool |
|
465
|
|
|
*/ |
|
466
|
1 |
|
public function intersects(array $search) |
|
467
|
|
|
{ |
|
468
|
1 |
|
return count($this->intersection($search)->array) > 0; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
//////////////////////////////////////////////////////////////////// |
|
472
|
|
|
///////////////////////////// SLICERS ////////////////////////////// |
|
473
|
|
|
//////////////////////////////////////////////////////////////////// |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* Get the first value from an array. |
|
477
|
|
|
* |
|
478
|
|
|
* @param int|null $take |
|
479
|
|
|
* |
|
480
|
|
|
* @return self |
|
481
|
|
|
*/ |
|
482
|
27 |
|
public function first($take = null) |
|
483
|
|
|
{ |
|
484
|
27 |
|
if ($take === null) { |
|
485
|
13 |
|
$array = array_shift($this->array); |
|
486
|
13 |
|
} else { |
|
487
|
14 |
|
$array = array_splice($this->array, 0, $take, true); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
27 |
|
return self::create((array)$array); |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Get the last value from an array. |
|
495
|
|
|
* |
|
496
|
|
|
* @param int|null $take |
|
497
|
|
|
* |
|
498
|
|
|
* @return self |
|
499
|
|
|
*/ |
|
500
|
10 |
|
public function last($take = null) |
|
501
|
|
|
{ |
|
502
|
10 |
|
if ($take === null) { |
|
503
|
7 |
|
$array = self::create((array)array_pop($this->array)); |
|
504
|
7 |
|
} else { |
|
505
|
3 |
|
$array = $this->rest(-$take); |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
10 |
|
return $array; |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
/** |
|
512
|
|
|
* Get everything but the last..$to items. |
|
513
|
|
|
* |
|
514
|
|
|
* @param int $to |
|
515
|
|
|
* |
|
516
|
|
|
* @return self |
|
517
|
|
|
*/ |
|
518
|
10 |
|
public function initial($to = 1) |
|
519
|
|
|
{ |
|
520
|
10 |
|
$slice = count($this->array) - $to; |
|
521
|
|
|
|
|
522
|
10 |
|
return $this->first($slice); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Get the last elements from index $from. |
|
527
|
|
|
* |
|
528
|
|
|
* @param int $from |
|
529
|
|
|
* |
|
530
|
|
|
* @return self |
|
531
|
|
|
*/ |
|
532
|
13 |
|
public function rest($from = 1) |
|
533
|
|
|
{ |
|
534
|
13 |
|
return self::create(array_splice($this->array, $from)); |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
//////////////////////////////////////////////////////////////////// |
|
538
|
|
|
///////////////////////////// ACT UPON ///////////////////////////// |
|
539
|
|
|
//////////////////////////////////////////////////////////////////// |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Iterate over an array and execute a callback for each loop. |
|
543
|
|
|
* |
|
544
|
|
|
* @param \Closure $closure |
|
545
|
|
|
* |
|
546
|
|
|
* @return Arrayy |
|
547
|
|
|
*/ |
|
548
|
1 |
View Code Duplication |
public function at(\Closure $closure) |
|
|
|
|
|
|
549
|
|
|
{ |
|
550
|
1 |
|
$array = $this->array; |
|
551
|
|
|
|
|
552
|
1 |
|
foreach ($array as $key => $value) { |
|
553
|
1 |
|
$closure($value, $key); |
|
554
|
1 |
|
} |
|
555
|
|
|
|
|
556
|
1 |
|
return self::create($array); |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
//////////////////////////////////////////////////////////////////// |
|
560
|
|
|
////////////////////////////// ALTER /////////////////////////////// |
|
561
|
|
|
//////////////////////////////////////////////////////////////////// |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Merge the new $array into the current array. |
|
565
|
|
|
* |
|
566
|
|
|
* - replace duplicate keys from the current array with the key,values from the new $array |
|
567
|
|
|
* - create new indexes |
|
568
|
|
|
* |
|
569
|
|
|
* @param array $array |
|
570
|
|
|
* |
|
571
|
|
|
* @return self |
|
572
|
|
|
*/ |
|
573
|
8 |
|
public function mergeAppendNewIndex(array $array = array()) |
|
574
|
|
|
{ |
|
575
|
8 |
|
return self::create(array_merge($this->array, $array)); |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Merge the current array into the new $array. |
|
580
|
|
|
* |
|
581
|
|
|
* - replace duplicate keys from new $array with the key,values from the current array |
|
582
|
|
|
* - create new indexes |
|
583
|
|
|
* |
|
584
|
|
|
* @param array $array |
|
585
|
|
|
* |
|
586
|
|
|
* @return self |
|
587
|
|
|
*/ |
|
588
|
8 |
|
public function mergePrependNewIndex(array $array = array()) |
|
589
|
|
|
{ |
|
590
|
8 |
|
return self::create(array_merge($array, $this->array)); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
/** |
|
594
|
|
|
* Merge the new $array into the current array. |
|
595
|
|
|
* |
|
596
|
|
|
* - keep key,value from the current array, also if the index is in the new $array |
|
597
|
|
|
* |
|
598
|
|
|
* @param array $array |
|
599
|
|
|
* |
|
600
|
|
|
* @return self |
|
601
|
|
|
*/ |
|
602
|
8 |
|
public function mergeAppendKeepIndex(array $array = array()) |
|
603
|
|
|
{ |
|
604
|
8 |
|
return self::create(array_replace($array, $this->array)); |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
/** |
|
608
|
|
|
* Merge the the current array into the $array. |
|
609
|
|
|
* |
|
610
|
|
|
* - use key,value from the new $array, also if the index is in the current array |
|
611
|
|
|
* |
|
612
|
|
|
* @param array $array |
|
613
|
|
|
* |
|
614
|
|
|
* @return self |
|
615
|
|
|
*/ |
|
616
|
8 |
|
public function mergePrependKeepIndex(array $array = array()) |
|
617
|
|
|
{ |
|
618
|
8 |
|
return self::create(array_replace($this->array, $array)); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Return values that are only in the current array. |
|
623
|
|
|
* |
|
624
|
|
|
* @param array $array |
|
625
|
|
|
* |
|
626
|
|
|
* @return self |
|
627
|
|
|
*/ |
|
628
|
8 |
|
public function diff(array $array = array()) |
|
629
|
|
|
{ |
|
630
|
8 |
|
return self::create(array_diff($this->array, $array)); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
/** |
|
634
|
|
|
* Return values that are only in the new $array. |
|
635
|
|
|
* |
|
636
|
|
|
* @param array $array |
|
637
|
|
|
* |
|
638
|
|
|
* @return self |
|
639
|
|
|
*/ |
|
640
|
8 |
|
public function diffReverse(array $array = array()) |
|
641
|
|
|
{ |
|
642
|
8 |
|
return self::create(array_diff($array, $this->array)); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* Replace the first matched value in an array. |
|
647
|
|
|
* |
|
648
|
|
|
* @param string $search The string to replace |
|
649
|
|
|
* @param string $replacement What to replace it with |
|
650
|
|
|
* |
|
651
|
|
|
* @return self |
|
652
|
|
|
*/ |
|
653
|
3 |
|
public function replaceOneValue($search, $replacement = '') |
|
654
|
|
|
{ |
|
655
|
3 |
|
$array = $this->array; |
|
656
|
3 |
|
$key = array_search($search, $array, true); |
|
657
|
|
|
|
|
658
|
3 |
|
if ($key !== false) { |
|
659
|
3 |
|
$array[$key] = $replacement; |
|
660
|
3 |
|
} |
|
661
|
|
|
|
|
662
|
3 |
|
return self::create((array)$array); |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
/** |
|
666
|
|
|
* Replace values in an array. |
|
667
|
|
|
* |
|
668
|
|
|
* @param string $search The string to replace |
|
669
|
|
|
* @param string $replacement What to replace it with |
|
670
|
|
|
* |
|
671
|
|
|
* @return self |
|
672
|
|
|
*/ |
|
673
|
1 |
|
public function replaceValues($search, $replacement = '') |
|
674
|
|
|
{ |
|
675
|
1 |
|
$array = $this->each( |
|
676
|
|
|
function ($value) use ($search, $replacement) { |
|
677
|
1 |
|
return UTF8::str_replace($search, $replacement, $value); |
|
678
|
|
|
} |
|
679
|
1 |
|
); |
|
680
|
|
|
|
|
681
|
1 |
|
return self::create((array)$array); |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
/** |
|
685
|
|
|
* Replace the keys in an array with another set. |
|
686
|
|
|
* |
|
687
|
|
|
* @param array $keys An array of keys matching the array's size |
|
688
|
|
|
* |
|
689
|
|
|
* @return self |
|
690
|
|
|
*/ |
|
691
|
1 |
|
public function replaceKeys(array $keys) |
|
692
|
|
|
{ |
|
693
|
1 |
|
$values = array_values($this->array); |
|
694
|
|
|
|
|
695
|
1 |
|
return self::create(array_combine($keys, $values)); |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
/** |
|
699
|
|
|
* Iterate over an array and modify the array's value. |
|
700
|
|
|
* |
|
701
|
|
|
* @param \Closure $closure |
|
702
|
|
|
* |
|
703
|
|
|
* @return array |
|
704
|
|
|
*/ |
|
705
|
18 |
View Code Duplication |
public function each(\Closure $closure) |
|
|
|
|
|
|
706
|
|
|
{ |
|
707
|
18 |
|
$array = $this->array; |
|
708
|
|
|
|
|
709
|
18 |
|
foreach ($array as $key => &$value) { |
|
710
|
14 |
|
$value = $closure($value, $key); |
|
711
|
18 |
|
} |
|
712
|
|
|
|
|
713
|
18 |
|
return $array; |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
|
|
/** |
|
717
|
|
|
* Shuffle an array. |
|
718
|
|
|
* |
|
719
|
|
|
* @return self |
|
720
|
|
|
*/ |
|
721
|
1 |
|
public function shuffle() |
|
722
|
|
|
{ |
|
723
|
1 |
|
$array = $this->array; |
|
724
|
|
|
|
|
725
|
1 |
|
shuffle($array); |
|
726
|
|
|
|
|
727
|
1 |
|
return self::create($array); |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
/** |
|
731
|
|
|
* Sort an array by key. |
|
732
|
|
|
* |
|
733
|
|
|
* @param string $direction |
|
734
|
|
|
* |
|
735
|
|
|
* @return self |
|
736
|
|
|
*/ |
|
737
|
8 |
|
public function sortKeys($direction = 'ASC') |
|
738
|
|
|
{ |
|
739
|
8 |
|
$array = $this->array; |
|
740
|
8 |
|
$direction = strtolower($direction); |
|
741
|
|
|
|
|
742
|
8 |
|
if ($direction === 'desc') { |
|
743
|
1 |
|
$directionType = SORT_DESC; |
|
744
|
1 |
|
} else { |
|
745
|
7 |
|
$directionType = SORT_ASC; |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
8 |
|
if ($directionType === SORT_ASC) { |
|
749
|
7 |
|
ksort($array); |
|
750
|
7 |
|
} else { |
|
751
|
1 |
|
krsort($array); |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
8 |
|
return self::create($array); |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
/** |
|
758
|
|
|
* Implodes an array. |
|
759
|
|
|
* |
|
760
|
|
|
* @param string $with What to implode it with |
|
761
|
|
|
* |
|
762
|
|
|
* @return string |
|
763
|
|
|
*/ |
|
764
|
21 |
|
public function implode($with = '') |
|
765
|
|
|
{ |
|
766
|
21 |
|
return implode($with, $this->array); |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
|
|
/** |
|
770
|
|
|
* Find all items in an array that pass the truth test. |
|
771
|
|
|
* |
|
772
|
|
|
* @param \Closure $closure |
|
773
|
|
|
* |
|
774
|
|
|
* @return self |
|
775
|
|
|
*/ |
|
776
|
8 |
|
public function filter($closure = null) |
|
777
|
|
|
{ |
|
778
|
8 |
|
if (!$closure) { |
|
779
|
|
|
return $this->clean(); |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
8 |
|
$array = array_filter($this->array, $closure); |
|
783
|
|
|
|
|
784
|
8 |
|
return self::create($array); |
|
785
|
|
|
} |
|
786
|
|
|
|
|
787
|
|
|
/** |
|
788
|
|
|
* Invoke a function on all of an array's values. |
|
789
|
|
|
* |
|
790
|
|
|
* @param mixed $callable |
|
791
|
|
|
* @param array $arguments |
|
792
|
|
|
* |
|
793
|
|
|
* @return self |
|
794
|
|
|
*/ |
|
795
|
1 |
|
public function invoke($callable, $arguments = array()) |
|
796
|
|
|
{ |
|
797
|
|
|
// If one argument given for each iteration, create an array for it. |
|
798
|
1 |
|
if (!is_array($arguments)) { |
|
799
|
1 |
|
$arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray(); |
|
800
|
1 |
|
} |
|
801
|
|
|
|
|
802
|
|
|
// If the callable has arguments, pass them. |
|
803
|
1 |
|
if ($arguments) { |
|
|
|
|
|
|
804
|
1 |
|
$array = array_map($callable, $this->array, $arguments); |
|
805
|
1 |
|
} else { |
|
806
|
1 |
|
$array = array_map($callable, $this->array); |
|
807
|
|
|
} |
|
808
|
|
|
|
|
809
|
1 |
|
return self::create($array); |
|
810
|
|
|
} |
|
811
|
|
|
|
|
812
|
|
|
/** |
|
813
|
|
|
* Return all items that fail the truth test. |
|
814
|
|
|
* |
|
815
|
|
|
* @param \Closure $closure |
|
816
|
|
|
* |
|
817
|
|
|
* @return self |
|
818
|
|
|
*/ |
|
819
|
1 |
View Code Duplication |
public function reject(\Closure $closure) |
|
|
|
|
|
|
820
|
|
|
{ |
|
821
|
1 |
|
$filtered = array(); |
|
822
|
|
|
|
|
823
|
1 |
|
foreach ($this->array as $key => $value) { |
|
824
|
1 |
|
if (!$closure($value, $key)) { |
|
825
|
1 |
|
$filtered[$key] = $value; |
|
826
|
1 |
|
} |
|
827
|
1 |
|
} |
|
828
|
|
|
|
|
829
|
1 |
|
return self::create($filtered); |
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
/** |
|
833
|
|
|
* Remove the first value from an array. |
|
834
|
|
|
* |
|
835
|
|
|
* @return self |
|
836
|
|
|
*/ |
|
837
|
8 |
|
public function removeFirst() |
|
838
|
|
|
{ |
|
839
|
8 |
|
array_shift($this->array); |
|
840
|
|
|
|
|
841
|
8 |
|
return self::create($this->array); |
|
842
|
|
|
} |
|
843
|
|
|
|
|
844
|
|
|
/** |
|
845
|
|
|
* Remove the last value from an array. |
|
846
|
|
|
* |
|
847
|
|
|
* @return self |
|
848
|
|
|
*/ |
|
849
|
7 |
|
public function removeLast() |
|
850
|
|
|
{ |
|
851
|
7 |
|
array_pop($this->array); |
|
852
|
|
|
|
|
853
|
7 |
|
return self::create($this->array); |
|
854
|
|
|
} |
|
855
|
|
|
|
|
856
|
|
|
/** |
|
857
|
|
|
* Removes a particular value from an array (numeric or associative). |
|
858
|
|
|
* |
|
859
|
|
|
* @param mixed $value |
|
860
|
|
|
* |
|
861
|
|
|
* @return self |
|
862
|
|
|
*/ |
|
863
|
7 |
|
public function removeValue($value) |
|
864
|
|
|
{ |
|
865
|
7 |
|
$isNumericArray = true; |
|
866
|
7 |
|
foreach ($this->array as $key => $item) { |
|
867
|
6 |
|
if ($item === $value) { |
|
868
|
6 |
|
if (!is_int($key)) { |
|
869
|
|
|
$isNumericArray = false; |
|
870
|
|
|
} |
|
871
|
6 |
|
unset($this->array[$key]); |
|
872
|
6 |
|
} |
|
873
|
7 |
|
} |
|
874
|
|
|
|
|
875
|
7 |
|
if ($isNumericArray) { |
|
876
|
7 |
|
$this->array = array_values($this->array); |
|
877
|
7 |
|
} |
|
878
|
|
|
|
|
879
|
7 |
|
return self::create($this->array); |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
/** |
|
883
|
|
|
* Prepend a value to an array. |
|
884
|
|
|
* |
|
885
|
|
|
* @param mixed $value |
|
886
|
|
|
* |
|
887
|
|
|
* @return self |
|
888
|
|
|
*/ |
|
889
|
7 |
|
public function prepend($value) |
|
890
|
|
|
{ |
|
891
|
7 |
|
array_unshift($this->array, $value); |
|
892
|
|
|
|
|
893
|
7 |
|
return self::create($this->array); |
|
894
|
|
|
} |
|
895
|
|
|
|
|
896
|
|
|
/** |
|
897
|
|
|
* Append a value to an array. |
|
898
|
|
|
* |
|
899
|
|
|
* @param mixed $value |
|
900
|
|
|
* |
|
901
|
|
|
* @return self |
|
902
|
|
|
*/ |
|
903
|
8 |
|
public function append($value) |
|
904
|
|
|
{ |
|
905
|
8 |
|
$this->array[] = $value; |
|
906
|
|
|
|
|
907
|
8 |
|
return self::create($this->array); |
|
908
|
|
|
} |
|
909
|
|
|
|
|
910
|
|
|
/** |
|
911
|
|
|
* Return the array in the reverse order. |
|
912
|
|
|
* |
|
913
|
|
|
* @return self |
|
914
|
|
|
*/ |
|
915
|
7 |
|
public function reverse() |
|
916
|
|
|
{ |
|
917
|
7 |
|
$this->array = array_reverse($this->array); |
|
918
|
|
|
|
|
919
|
7 |
|
return self::create($this->array); |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
/** |
|
923
|
|
|
* duplicate free copy of an array |
|
924
|
|
|
* |
|
925
|
|
|
* @return self |
|
926
|
|
|
*/ |
|
927
|
7 |
|
public function unique() |
|
928
|
|
|
{ |
|
929
|
7 |
|
$this->array = array_reduce( |
|
|
|
|
|
|
930
|
7 |
|
$this->array, |
|
931
|
7 |
|
function ($resultArray, $value) { |
|
932
|
6 |
|
if (in_array($value, $resultArray, true) === false) { |
|
933
|
6 |
|
$resultArray[] = $value; |
|
934
|
6 |
|
} |
|
935
|
|
|
|
|
936
|
6 |
|
return $resultArray; |
|
937
|
7 |
|
}, |
|
938
|
7 |
|
array() |
|
939
|
7 |
|
); |
|
940
|
|
|
|
|
941
|
7 |
|
return self::create($this->array); |
|
942
|
|
|
} |
|
943
|
|
|
} |
|
944
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.