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