1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrayy; |
4
|
|
|
|
5
|
|
|
use voku\helper\UTF8; |
6
|
|
|
|
7
|
|
|
/** @noinspection ClassReImplementsParentInterfaceInspection */ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Methods to manage arrays. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
class Arrayy extends \ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $array = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $iteratorClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $pathSeparator = '.'; |
31
|
|
|
|
32
|
|
|
/** @noinspection MagicMethodsValidityInspection */ |
33
|
|
|
/** |
34
|
|
|
* Initializes |
35
|
|
|
* |
36
|
|
|
* @param array $array |
37
|
|
|
* @param string $iteratorClass |
38
|
|
|
*/ |
39
|
768 |
|
public function __construct($array = array(), $iteratorClass = '\\Arrayy\\ArrayyIterator') |
40
|
|
|
{ |
41
|
768 |
|
$array = $this->fallbackForArray($array); |
42
|
766 |
|
$this->array = $array; |
43
|
|
|
|
44
|
766 |
|
$this->setIteratorClass($iteratorClass); |
45
|
766 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a value by key. |
49
|
|
|
* |
50
|
|
|
* @param $key |
51
|
|
|
* |
52
|
|
|
* @return mixed <p>Get a Value from the current array.</p> |
53
|
|
|
*/ |
54
|
2 |
|
public function &__get($key) |
55
|
|
|
{ |
56
|
2 |
|
$return = $this->get($key); |
57
|
|
|
|
58
|
2 |
|
if (is_array($return)) { |
59
|
|
|
return static::create($return); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Call object as function. |
67
|
|
|
* |
68
|
|
|
* @param mixed $key |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
1 |
|
public function __invoke($key = null) |
73
|
|
|
{ |
74
|
1 |
|
if ($key !== null) { |
75
|
1 |
|
if (isset($this->array[$key])) { |
76
|
1 |
|
return $this->array[$key]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return (array)$this->array; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Whether or not an element exists by key. |
87
|
|
|
* |
88
|
|
|
* @param mixed $key |
89
|
|
|
* |
90
|
|
|
* @return bool <p>True is the key/index exists, otherwise false.</p> |
91
|
|
|
*/ |
92
|
|
|
public function __isset($key) |
93
|
|
|
{ |
94
|
|
|
return $this->offsetExists($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Assigns a value to the specified element. |
99
|
|
|
* |
100
|
|
|
* @param mixed $key |
101
|
|
|
* @param mixed $value |
102
|
|
|
*/ |
103
|
2 |
|
public function __set($key, $value) |
104
|
|
|
{ |
105
|
2 |
|
$this->internalSet($key, $value); |
106
|
2 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* magic to string |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
16 |
|
public function __toString() |
114
|
|
|
{ |
115
|
16 |
|
return $this->toString(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Unset element by key. |
120
|
|
|
* |
121
|
|
|
* @param mixed $key |
122
|
|
|
*/ |
123
|
|
|
public function __unset($key) |
124
|
|
|
{ |
125
|
|
|
$this->internalRemove($key); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* alias: for "Arrayy->append()" |
130
|
|
|
* |
131
|
|
|
* @see Arrayy::append() |
132
|
|
|
* |
133
|
|
|
* @param mixed $value |
134
|
|
|
* |
135
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
136
|
|
|
*/ |
137
|
1 |
|
public function add($value) |
138
|
|
|
{ |
139
|
1 |
|
return $this->append($value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Append a value to the current array. |
144
|
|
|
* |
145
|
|
|
* @param mixed $value |
146
|
|
|
* |
147
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
148
|
|
|
*/ |
149
|
9 |
|
public function append($value) |
150
|
|
|
{ |
151
|
9 |
|
$this->array[] = $value; |
152
|
|
|
|
153
|
9 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sort the entries by value. |
158
|
|
|
* |
159
|
|
|
* @param int $sort_flags [optional] <p> |
160
|
|
|
* You may modify the behavior of the sort using the optional |
161
|
|
|
* parameter sort_flags, for details |
162
|
|
|
* see sort. |
163
|
|
|
* </p> |
164
|
|
|
* |
165
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
166
|
|
|
*/ |
167
|
4 |
|
public function asort($sort_flags = null) |
168
|
|
|
{ |
169
|
4 |
|
asort($this->array, $sort_flags); |
170
|
|
|
|
171
|
4 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Count the values from the current array. |
176
|
|
|
* |
177
|
|
|
* alias: for "Arrayy->size()" |
178
|
|
|
* |
179
|
|
|
* @see Arrayy::size() |
180
|
|
|
* |
181
|
|
|
* @return int |
182
|
|
|
*/ |
183
|
93 |
|
public function count() |
184
|
|
|
{ |
185
|
93 |
|
return $this->size(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Exchange the array for another one. |
190
|
|
|
* |
191
|
|
|
* @param array|Arrayy $data |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
1 |
|
public function exchangeArray($data) |
196
|
|
|
{ |
197
|
1 |
|
$this->array = $this->fallbackForArray($data); |
198
|
|
|
|
199
|
1 |
|
return $this->array; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Creates a copy of the ArrayyObject. |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
1 |
|
public function getArrayCopy() |
208
|
|
|
{ |
209
|
1 |
|
return $this->array; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns a new ArrayyIterator, thus implementing the IteratorAggregate interface. |
214
|
|
|
* |
215
|
|
|
* @return ArrayyIterator <p>An iterator for the values in the array.</p> |
216
|
|
|
*/ |
217
|
20 |
|
public function getIterator() |
218
|
|
|
{ |
219
|
20 |
|
$iterator = $this->getIteratorClass(); |
220
|
|
|
|
221
|
20 |
|
return new $iterator($this->array); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets the iterator classname for the ArrayObject. |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
20 |
|
public function getIteratorClass() |
230
|
|
|
{ |
231
|
20 |
|
return $this->iteratorClass; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Sort the entries by key |
236
|
|
|
* |
237
|
|
|
* @param int $sort_flags [optional] <p> |
238
|
|
|
* You may modify the behavior of the sort using the optional |
239
|
|
|
* parameter sort_flags, for details |
240
|
|
|
* see sort. |
241
|
|
|
* </p> |
242
|
|
|
* |
243
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
244
|
|
|
*/ |
245
|
4 |
|
public function ksort($sort_flags = null) |
246
|
|
|
{ |
247
|
4 |
|
ksort($this->array, $sort_flags); |
248
|
|
|
|
249
|
4 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Sort an array using a case insensitive "natural order" algorithm |
254
|
|
|
* |
255
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
256
|
|
|
*/ |
257
|
|
|
public function natcasesort() |
258
|
|
|
{ |
259
|
|
|
natcasesort($this->array); |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Sort entries using a "natural order" algorithm |
266
|
|
|
* |
267
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
268
|
|
|
*/ |
269
|
1 |
|
public function natsort() |
270
|
|
|
{ |
271
|
1 |
|
natsort($this->array); |
272
|
|
|
|
273
|
1 |
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Whether or not an offset exists. |
278
|
|
|
* |
279
|
|
|
* @param int|float|string $offset |
280
|
|
|
* |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
40 |
|
public function offsetExists($offset) |
284
|
|
|
{ |
285
|
40 |
|
if ($this->isEmpty()) { |
286
|
4 |
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// php cast "bool"-index into "int"-index |
290
|
36 |
|
if ((bool)$offset === $offset) { |
291
|
1 |
|
$offset = (int)$offset; |
292
|
1 |
|
} |
293
|
|
|
|
294
|
36 |
|
$tmpReturn = \array_key_exists($offset, $this->array); |
295
|
|
|
|
296
|
|
|
if ( |
297
|
|
|
$tmpReturn === true |
298
|
36 |
|
|| |
299
|
|
|
( |
300
|
|
|
$tmpReturn === false |
301
|
12 |
|
&& |
302
|
12 |
|
strpos((string)$offset, $this->pathSeparator) === false |
303
|
12 |
|
) |
304
|
36 |
|
) { |
305
|
34 |
|
return $tmpReturn; |
306
|
|
|
} |
307
|
|
|
|
308
|
3 |
|
$offsetExists = false; |
309
|
|
|
|
310
|
3 |
|
if (strpos((string)$offset, $this->pathSeparator) !== false) { |
311
|
|
|
|
312
|
3 |
|
$offsetExists = false; |
313
|
3 |
|
$explodedPath = explode($this->pathSeparator, (string)$offset); |
314
|
3 |
|
$lastOffset = \array_pop($explodedPath); |
315
|
3 |
|
$containerPath = implode($this->pathSeparator, $explodedPath); |
316
|
|
|
|
317
|
3 |
|
$this->callAtPath( |
318
|
3 |
|
$containerPath, |
319
|
|
|
function ($container) use ($lastOffset, &$offsetExists) { |
320
|
3 |
|
$offsetExists = \array_key_exists($lastOffset, $container); |
321
|
3 |
|
} |
322
|
3 |
|
); |
323
|
3 |
|
} |
324
|
|
|
|
325
|
3 |
|
return $offsetExists; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Returns the value at specified offset. |
330
|
|
|
* |
331
|
|
|
* @param mixed $offset |
332
|
|
|
* |
333
|
|
|
* @return mixed <p>Will return null if the offset did not exists.</p> |
334
|
|
|
*/ |
335
|
26 |
|
public function offsetGet($offset) |
336
|
|
|
{ |
337
|
26 |
|
return $this->offsetExists($offset) ? $this->get($offset) : null; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Assigns a value to the specified offset. |
342
|
|
|
* |
343
|
|
|
* @param mixed $offset |
344
|
|
|
* @param mixed $value |
345
|
|
|
*/ |
346
|
17 |
|
public function offsetSet($offset, $value) |
347
|
|
|
{ |
348
|
17 |
|
if ($offset === null) { |
349
|
4 |
|
$this->array[] = $value; |
350
|
4 |
|
} else { |
351
|
13 |
|
$this->internalSet($offset, $value); |
352
|
|
|
} |
353
|
17 |
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Unset an offset. |
357
|
|
|
* |
358
|
|
|
* @param mixed $offset |
359
|
|
|
*/ |
360
|
7 |
|
public function offsetUnset($offset) |
361
|
|
|
{ |
362
|
7 |
|
if ($this->isEmpty()) { |
363
|
1 |
|
return; |
364
|
|
|
} |
365
|
|
|
|
366
|
6 |
|
if (\array_key_exists($offset, $this->array)) { |
367
|
4 |
|
unset($this->array[$offset]); |
368
|
|
|
|
369
|
4 |
|
return; |
370
|
|
|
} |
371
|
|
|
|
372
|
3 |
|
if (strpos((string)$offset, $this->pathSeparator) !== false) { |
373
|
|
|
|
374
|
2 |
|
$path = explode($this->pathSeparator, (string)$offset); |
375
|
2 |
|
$pathToUnset = \array_pop($path); |
376
|
|
|
|
377
|
2 |
|
$this->callAtPath( |
378
|
2 |
|
implode($this->pathSeparator, $path), |
379
|
|
|
function (&$offset) use ($pathToUnset) { |
380
|
2 |
|
unset($offset[$pathToUnset]); |
381
|
2 |
|
} |
382
|
2 |
|
); |
383
|
|
|
|
384
|
2 |
|
} |
385
|
3 |
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Serialize the current "Arrayy"-object. |
389
|
|
|
* |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
1 |
|
public function serialize() |
393
|
|
|
{ |
394
|
1 |
|
return parent::serialize(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Sets the iterator classname for the current "Arrayy"-object. |
399
|
|
|
* |
400
|
|
|
* @param string $class |
401
|
|
|
* |
402
|
|
|
* @return void |
403
|
|
|
* |
404
|
|
|
* @throws \InvalidArgumentException |
405
|
|
|
*/ |
406
|
766 |
|
public function setIteratorClass($class) |
407
|
|
|
{ |
408
|
766 |
|
if (class_exists($class)) { |
409
|
766 |
|
$this->iteratorClass = $class; |
410
|
|
|
|
411
|
766 |
|
return; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
if (strpos($class, '\\') === 0) { |
415
|
|
|
$class = '\\' . $class; |
416
|
|
|
if (class_exists($class)) { |
417
|
|
|
$this->iteratorClass = $class; |
418
|
|
|
|
419
|
|
|
return; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
throw new \InvalidArgumentException('The iterator class does not exist: ' . $class); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Sort the entries with a user-defined comparison function and maintain key association. |
428
|
|
|
* |
429
|
|
|
* @param callable $function |
430
|
|
|
* |
431
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
432
|
|
|
* |
433
|
|
|
* @throws \InvalidArgumentException |
434
|
|
|
*/ |
435
|
|
View Code Duplication |
public function uasort($function) |
|
|
|
|
436
|
|
|
{ |
437
|
|
|
if (!is_callable($function)) { |
438
|
|
|
throw new \InvalidArgumentException( |
439
|
|
|
'Passed function must be callable' |
440
|
|
|
); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
uasort($this->array, $function); |
444
|
|
|
|
445
|
|
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Sort the entries by keys using a user-defined comparison function. |
450
|
|
|
* |
451
|
|
|
* @param callable $function |
452
|
|
|
* |
453
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
454
|
|
|
* |
455
|
|
|
* @throws \InvalidArgumentException |
456
|
|
|
*/ |
457
|
5 |
|
public function uksort($function) |
458
|
|
|
{ |
459
|
5 |
|
return $this->customSortKeys($function); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Unserialize an string and return this object. |
464
|
|
|
* |
465
|
|
|
* @param string $string |
466
|
|
|
* |
467
|
|
|
* @return static <p>(Mutable)</p> |
468
|
|
|
*/ |
469
|
1 |
|
public function unserialize($string) |
470
|
|
|
{ |
471
|
1 |
|
parent::unserialize($string); |
472
|
|
|
|
473
|
1 |
|
return $this; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Sort an array in reverse order and maintain index association. |
478
|
|
|
* |
479
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
480
|
|
|
*/ |
481
|
4 |
|
public function arsort() |
482
|
|
|
{ |
483
|
4 |
|
arsort($this->array); |
484
|
|
|
|
485
|
4 |
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Iterate over the current array and execute a callback for each loop. |
490
|
|
|
* |
491
|
|
|
* @param \Closure $closure |
492
|
|
|
* |
493
|
|
|
* @return static <p>(Immutable)</p> |
494
|
|
|
*/ |
495
|
2 |
View Code Duplication |
public function at(\Closure $closure) |
|
|
|
|
496
|
|
|
{ |
497
|
2 |
|
$array = $this->array; |
498
|
|
|
|
499
|
2 |
|
foreach ($array as $key => $value) { |
500
|
2 |
|
$closure($value, $key); |
501
|
2 |
|
} |
502
|
|
|
|
503
|
2 |
|
return static::create($array); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Returns the average value of the current array. |
508
|
|
|
* |
509
|
|
|
* @param int $decimals <p>The number of decimal-numbers to return.</p> |
510
|
|
|
* |
511
|
|
|
* @return int|double <p>The average value.</p> |
512
|
|
|
*/ |
513
|
10 |
|
public function average($decimals = 0) |
514
|
|
|
{ |
515
|
10 |
|
$count = $this->count(); |
516
|
|
|
|
517
|
10 |
|
if (!$count) { |
518
|
2 |
|
return 0; |
519
|
|
|
} |
520
|
|
|
|
521
|
8 |
|
if (!is_int($decimals)) { |
522
|
3 |
|
$decimals = 0; |
523
|
3 |
|
} |
524
|
|
|
|
525
|
8 |
|
return round(\array_sum($this->array) / $count, $decimals); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @param mixed $path |
530
|
|
|
* @param callable $callable |
531
|
|
|
* @param null|array $currentOffset |
532
|
|
|
*/ |
533
|
4 |
|
protected function callAtPath($path, $callable, &$currentOffset = null) |
534
|
|
|
{ |
535
|
4 |
|
if ($currentOffset === null) { |
536
|
4 |
|
$currentOffset = &$this->array; |
537
|
4 |
|
} |
538
|
|
|
|
539
|
4 |
|
$explodedPath = explode($this->pathSeparator, $path); |
540
|
4 |
|
$nextPath = \array_shift($explodedPath); |
541
|
|
|
|
542
|
4 |
|
if (!isset($currentOffset[$nextPath])) { |
543
|
|
|
return; |
544
|
|
|
} |
545
|
|
|
|
546
|
4 |
|
if (!empty($explodedPath)) { |
547
|
1 |
|
$this->callAtPath( |
548
|
1 |
|
implode($this->pathSeparator, $explodedPath), |
549
|
1 |
|
$callable, |
550
|
1 |
|
$currentOffset[$nextPath] |
551
|
1 |
|
); |
552
|
1 |
|
} else { |
553
|
4 |
|
$callable($currentOffset[$nextPath]); |
554
|
|
|
} |
555
|
4 |
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Changes all keys in an array. |
559
|
|
|
* |
560
|
|
|
* @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br /> |
561
|
|
|
* or <strong>CASE_LOWER</strong> (default)</p> |
562
|
|
|
* |
563
|
|
|
* @return static <p>(Immutable)</p> |
564
|
|
|
*/ |
565
|
1 |
|
public function changeKeyCase($case = CASE_LOWER) |
566
|
|
|
{ |
567
|
1 |
|
return static::create(UTF8::array_change_key_case($this->array, $case)); |
|
|
|
|
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Change the path separator of the array wrapper. |
572
|
|
|
* |
573
|
|
|
* By default, the separator is: "." |
574
|
|
|
* |
575
|
|
|
* @param string $separator <p>Separator to set.</p> |
576
|
|
|
* |
577
|
|
|
* @return static <p>Mutable</p> |
578
|
|
|
*/ |
579
|
1 |
|
public function changeSeparator($separator) |
580
|
|
|
{ |
581
|
1 |
|
$this->pathSeparator = $separator; |
582
|
|
|
|
583
|
1 |
|
return $this; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Create a chunked version of the current array. |
588
|
|
|
* |
589
|
|
|
* @param int $size <p>Size of each chunk.</p> |
590
|
|
|
* @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
591
|
|
|
* |
592
|
|
|
* @return static <p>(Immutable) A new array of chunks from the original array.</p> |
593
|
|
|
*/ |
594
|
4 |
|
public function chunk($size, $preserveKeys = false) |
595
|
|
|
{ |
596
|
4 |
|
$result = \array_chunk($this->array, $size, $preserveKeys); |
597
|
|
|
|
598
|
4 |
|
return static::create($result); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Clean all falsy values from the current array. |
603
|
|
|
* |
604
|
|
|
* @return static <p>(Immutable)</p> |
605
|
|
|
*/ |
606
|
8 |
|
public function clean() |
607
|
|
|
{ |
608
|
8 |
|
return $this->filter( |
609
|
|
|
function ($value) { |
610
|
7 |
|
return (bool)$value; |
611
|
|
|
} |
612
|
8 |
|
); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* WARNING!!! -> Clear the current array. |
617
|
|
|
* |
618
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with an empty array.</p> |
619
|
|
|
*/ |
620
|
4 |
|
public function clear() |
621
|
|
|
{ |
622
|
4 |
|
$this->array = array(); |
623
|
|
|
|
624
|
4 |
|
return $this; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Check if an item is in the current array. |
629
|
|
|
* |
630
|
|
|
* @param string|int|float $value |
631
|
|
|
* |
632
|
|
|
* @return bool |
633
|
|
|
*/ |
634
|
13 |
|
public function contains($value) |
635
|
|
|
{ |
636
|
13 |
|
return in_array($value, $this->array, true); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Check if an (case-insensitive) string is in the current array. |
641
|
|
|
* |
642
|
|
|
* @param string $value |
643
|
|
|
* |
644
|
|
|
* @return bool |
645
|
|
|
*/ |
646
|
13 |
|
public function containsCaseInsensitive($value) |
647
|
|
|
{ |
648
|
13 |
|
return in_array( |
649
|
13 |
|
UTF8::strtolower($value), |
650
|
13 |
|
\array_map( |
651
|
|
|
array( |
652
|
13 |
|
new UTF8(), |
653
|
13 |
|
'strtolower', |
654
|
13 |
|
), |
655
|
13 |
|
$this->array |
656
|
13 |
|
), |
657
|
|
|
true |
658
|
13 |
|
); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* Check if the given key/index exists in the array. |
663
|
|
|
* |
664
|
|
|
* @param string|int|float $key <p>key/index to search for</p> |
665
|
|
|
* |
666
|
|
|
* @return bool <p>Returns true if the given key/index exists in the array, false otherwise.</p> |
667
|
|
|
*/ |
668
|
4 |
|
public function containsKey($key) |
669
|
|
|
{ |
670
|
4 |
|
return $this->offsetExists($key); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Check if all given needles are present in the array as key/index. |
675
|
|
|
* |
676
|
|
|
* @param array $needles |
677
|
|
|
* |
678
|
|
|
* @return bool <p>Returns true if the given keys/indexes exists in the array, false otherwise.</p> |
679
|
|
|
*/ |
680
|
1 |
|
public function containsKeys(array $needles) |
681
|
|
|
{ |
682
|
1 |
|
return count(\array_intersect($needles, $this->keys()->getArray())) === count($needles); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* alias: for "Arrayy->contains()" |
687
|
|
|
* |
688
|
|
|
* @see Arrayy::contains() |
689
|
|
|
* |
690
|
|
|
* @param string|int|float $value |
691
|
|
|
* |
692
|
|
|
* @return bool |
693
|
|
|
*/ |
694
|
9 |
|
public function containsValue($value) |
695
|
|
|
{ |
696
|
9 |
|
return $this->contains($value); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* Check if all given needles are present in the array. |
701
|
|
|
* |
702
|
|
|
* @param array $needles |
703
|
|
|
* |
704
|
|
|
* @return bool <p>Returns true if the given values exists in the array, false otherwise.</p> |
705
|
|
|
*/ |
706
|
1 |
|
public function containsValues(array $needles) |
707
|
|
|
{ |
708
|
1 |
|
return count(\array_intersect($needles, $this->array)) === count($needles); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Creates an Arrayy object. |
713
|
|
|
* |
714
|
|
|
* @param array $array |
715
|
|
|
* |
716
|
|
|
* @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
717
|
|
|
*/ |
718
|
480 |
|
public static function create($array = array()) |
719
|
|
|
{ |
720
|
480 |
|
return new static($array); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
/** |
724
|
|
|
* WARNING: Creates an Arrayy object by reference. |
725
|
|
|
* |
726
|
|
|
* @param array $array |
727
|
|
|
* |
728
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
729
|
|
|
*/ |
730
|
1 |
|
public function createByReference(&$array = array()) |
731
|
|
|
{ |
732
|
1 |
|
$array = $this->fallbackForArray($array); |
733
|
|
|
|
734
|
1 |
|
$this->array = &$array; |
735
|
|
|
|
736
|
1 |
|
return $this; |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* Create an new Arrayy object via JSON. |
741
|
|
|
* |
742
|
|
|
* @param string $json |
743
|
|
|
* |
744
|
|
|
* @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
745
|
|
|
*/ |
746
|
5 |
|
public static function createFromJson($json) |
747
|
|
|
{ |
748
|
5 |
|
$array = UTF8::json_decode($json, true); |
749
|
|
|
|
750
|
5 |
|
return static::create($array); |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Create an new instance filled with values from an object that have implemented ArrayAccess. |
755
|
|
|
* |
756
|
|
|
* @param \ArrayAccess $object <p>Object that implements ArrayAccess</p> |
757
|
|
|
* |
758
|
|
|
* @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
759
|
|
|
*/ |
760
|
4 |
|
public static function createFromObject(\ArrayAccess $object) |
761
|
|
|
{ |
762
|
4 |
|
$array = new static(); |
763
|
4 |
|
foreach ($object as $key => $value) { |
764
|
|
|
/** @noinspection OffsetOperationsInspection */ |
765
|
3 |
|
$array[$key] = $value; |
766
|
4 |
|
} |
767
|
|
|
|
768
|
4 |
|
return $array; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* Create an new Arrayy object via string. |
773
|
|
|
* |
774
|
|
|
* @param string $str <p>The input string.</p> |
775
|
|
|
* @param string|null $delimiter <p>The boundary string.</p> |
776
|
|
|
* @param string|null $regEx <p>Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be |
777
|
|
|
* used.</p> |
778
|
|
|
* |
779
|
|
|
* @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
780
|
|
|
*/ |
781
|
8 |
|
public static function createFromString($str, $delimiter, $regEx = null) |
782
|
|
|
{ |
783
|
8 |
|
if ($regEx) { |
|
|
|
|
784
|
1 |
|
preg_match_all($regEx, $str, $array); |
785
|
|
|
|
786
|
1 |
|
if (!empty($array)) { |
787
|
1 |
|
$array = $array[0]; |
788
|
1 |
|
} |
789
|
|
|
|
790
|
1 |
|
} else { |
791
|
7 |
|
$array = explode($delimiter, $str); |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
// trim all string in the array |
795
|
8 |
|
\array_walk( |
796
|
|
|
$array, |
797
|
|
|
function (&$val) { |
798
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
799
|
8 |
|
if (is_string($val)) { |
800
|
8 |
|
$val = trim($val); |
801
|
8 |
|
} |
802
|
8 |
|
} |
803
|
8 |
|
); |
804
|
|
|
|
805
|
8 |
|
return static::create($array); |
806
|
|
|
} |
807
|
|
|
|
808
|
|
|
/** |
809
|
|
|
* Create an new instance containing a range of elements. |
810
|
|
|
* |
811
|
|
|
* @param mixed $low <p>First value of the sequence.</p> |
812
|
|
|
* @param mixed $high <p>The sequence is ended upon reaching the end value.</p> |
813
|
|
|
* @param int $step <p>Used as the increment between elements in the sequence.</p> |
814
|
|
|
* |
815
|
|
|
* @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
816
|
|
|
*/ |
817
|
1 |
|
public static function createWithRange($low, $high, $step = 1) |
818
|
|
|
{ |
819
|
1 |
|
return static::create(range($low, $high, $step)); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Custom sort by index via "uksort". |
824
|
|
|
* |
825
|
|
|
* @link http://php.net/manual/en/function.uksort.php |
826
|
|
|
* |
827
|
|
|
* @param callable $function |
828
|
|
|
* |
829
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
830
|
|
|
* |
831
|
|
|
* @throws \InvalidArgumentException |
832
|
|
|
*/ |
833
|
5 |
View Code Duplication |
public function customSortKeys($function) |
|
|
|
|
834
|
|
|
{ |
835
|
5 |
|
if (!is_callable($function)) { |
836
|
|
|
throw new \InvalidArgumentException( |
837
|
|
|
'Passed function must be callable' |
838
|
|
|
); |
839
|
|
|
} |
840
|
|
|
|
841
|
5 |
|
uksort($this->array, $function); |
842
|
|
|
|
843
|
5 |
|
return $this; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Custom sort by value via "usort". |
848
|
|
|
* |
849
|
|
|
* @link http://php.net/manual/en/function.usort.php |
850
|
|
|
* |
851
|
|
|
* @param callable $function |
852
|
|
|
* |
853
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
854
|
|
|
* |
855
|
|
|
* @throws \InvalidArgumentException |
856
|
|
|
*/ |
857
|
5 |
View Code Duplication |
public function customSortValues($function) |
|
|
|
|
858
|
|
|
{ |
859
|
5 |
|
if (!is_callable($function)) { |
860
|
|
|
throw new \InvalidArgumentException( |
861
|
|
|
'Passed function must be callable' |
862
|
|
|
); |
863
|
|
|
} |
864
|
|
|
|
865
|
5 |
|
usort($this->array, $function); |
866
|
|
|
|
867
|
5 |
|
return $this; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
/** |
871
|
|
|
* Return values that are only in the current array. |
872
|
|
|
* |
873
|
|
|
* @param array $array |
874
|
|
|
* |
875
|
|
|
* @return static <p>(Immutable)</p> |
876
|
|
|
*/ |
877
|
12 |
|
public function diff(array $array = array()) |
878
|
|
|
{ |
879
|
12 |
|
$result = \array_diff($this->array, $array); |
880
|
|
|
|
881
|
12 |
|
return static::create($result); |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
/** |
885
|
|
|
* Return values that are only in the current multi-dimensional array. |
886
|
|
|
* |
887
|
|
|
* @param array $array |
888
|
|
|
* @param null|array $helperVariableForRecursion <p>(only for internal usage)</p> |
889
|
|
|
* |
890
|
|
|
* @return static <p>(Immutable)</p> |
891
|
|
|
*/ |
892
|
1 |
|
public function diffRecursive(array $array = array(), $helperVariableForRecursion = null) |
893
|
|
|
{ |
894
|
1 |
|
$result = array(); |
895
|
|
|
|
896
|
|
|
if ( |
897
|
|
|
$helperVariableForRecursion !== null |
898
|
1 |
|
&& |
899
|
1 |
|
is_array($helperVariableForRecursion) |
900
|
1 |
|
) { |
901
|
1 |
|
$arrayForTheLoop = $helperVariableForRecursion; |
902
|
1 |
|
} else { |
903
|
1 |
|
$arrayForTheLoop = $this->array; |
904
|
|
|
} |
905
|
|
|
|
906
|
1 |
|
foreach ($arrayForTheLoop as $key => $value) { |
907
|
1 |
|
if (\array_key_exists($key, $array)) { |
908
|
1 |
|
if (is_array($value)) { |
909
|
1 |
|
$recursiveDiff = $this->diffRecursive($array[$key], $value); |
910
|
1 |
|
if (!empty($recursiveDiff)) { |
911
|
1 |
|
$result[$key] = $recursiveDiff; |
912
|
1 |
|
} |
913
|
1 |
|
} else { |
914
|
1 |
|
if ($value != $array[$key]) { |
915
|
1 |
|
$result[$key] = $value; |
916
|
1 |
|
} |
917
|
|
|
} |
918
|
1 |
|
} else { |
919
|
1 |
|
$result[$key] = $value; |
920
|
|
|
} |
921
|
1 |
|
} |
922
|
|
|
|
923
|
1 |
|
return static::create($result); |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
/** |
927
|
|
|
* Return values that are only in the new $array. |
928
|
|
|
* |
929
|
|
|
* @param array $array |
930
|
|
|
* |
931
|
|
|
* @return static <p>(Immutable)</p> |
932
|
|
|
*/ |
933
|
8 |
|
public function diffReverse(array $array = array()) |
934
|
|
|
{ |
935
|
8 |
|
$result = \array_diff($array, $this->array); |
936
|
|
|
|
937
|
8 |
|
return static::create($result); |
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
/** |
941
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
942
|
|
|
* |
943
|
|
|
* @return static <p>(Immutable)</p> |
944
|
|
|
*/ |
945
|
1 |
|
public function divide() |
946
|
|
|
{ |
947
|
1 |
|
return static::create( |
948
|
|
|
array( |
949
|
1 |
|
$this->keys(), |
950
|
1 |
|
$this->values(), |
951
|
|
|
) |
952
|
1 |
|
); |
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
/** |
956
|
|
|
* Iterate over the current array and modify the array's value. |
957
|
|
|
* |
958
|
|
|
* @param \Closure $closure |
959
|
|
|
* |
960
|
|
|
* @return static <p>(Immutable)</p> |
961
|
|
|
*/ |
962
|
4 |
View Code Duplication |
public function each(\Closure $closure) |
|
|
|
|
963
|
|
|
{ |
964
|
4 |
|
$array = $this->array; |
965
|
|
|
|
966
|
4 |
|
foreach ($array as $key => $value) { |
967
|
4 |
|
$array[$key] = $closure($value, $key); |
968
|
4 |
|
} |
969
|
|
|
|
970
|
4 |
|
return static::create($array); |
971
|
|
|
} |
972
|
|
|
|
973
|
|
|
/** |
974
|
|
|
* Check if a value is in the current array using a closure. |
975
|
|
|
* |
976
|
|
|
* @param \Closure $closure |
977
|
|
|
* |
978
|
|
|
* @return bool <p>Returns true if the given value is found, false otherwise.</p> |
979
|
|
|
*/ |
980
|
4 |
|
public function exists(\Closure $closure) |
981
|
|
|
{ |
982
|
4 |
|
$isExists = false; |
983
|
4 |
|
foreach ($this->array as $key => $value) { |
984
|
3 |
|
if ($closure($value, $key)) { |
985
|
1 |
|
$isExists = true; |
986
|
1 |
|
break; |
987
|
|
|
} |
988
|
4 |
|
} |
989
|
|
|
|
990
|
4 |
|
return $isExists; |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* create a fallback for array |
995
|
|
|
* |
996
|
|
|
* 1. use the current array, if it's a array |
997
|
|
|
* 2. call "getArray()" on object, if there is a "Arrayy"-object |
998
|
|
|
* 3. fallback to empty array, if there is nothing |
999
|
|
|
* 4. call "createFromObject()" on object, if there is a "\ArrayAccess"-object |
1000
|
|
|
* 5. call "__toArray()" on object, if the method exists |
1001
|
|
|
* 6. cast a string or object with "__toString()" into an array |
1002
|
|
|
* 7. throw a "InvalidArgumentException"-Exception |
1003
|
|
|
* |
1004
|
|
|
* @param $array |
1005
|
|
|
* |
1006
|
|
|
* @return array |
1007
|
|
|
* |
1008
|
|
|
* @throws \InvalidArgumentException |
1009
|
|
|
*/ |
1010
|
768 |
|
protected function fallbackForArray(&$array) |
1011
|
|
|
{ |
1012
|
768 |
|
if (is_array($array)) { |
1013
|
765 |
|
return $array; |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
11 |
|
if ($array instanceof self) { |
1017
|
1 |
|
return $array->getArray(); |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
10 |
|
if (!$array) { |
1021
|
6 |
|
return array(); |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
9 |
|
$isObject = is_object($array); |
1025
|
|
|
|
1026
|
9 |
|
if ($isObject && $array instanceof \ArrayAccess) { |
1027
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
1028
|
|
|
return static::createFromObject($array)->getArray(); |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
9 |
|
if ($isObject && $array instanceof \ArrayObject) { |
1032
|
|
|
return $array->getArrayCopy(); |
1033
|
|
|
} |
1034
|
|
|
|
1035
|
9 |
|
if ($isObject && method_exists($array, '__toArray')) { |
1036
|
|
|
return (array)$array->__toArray(); |
1037
|
|
|
} |
1038
|
|
|
|
1039
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
1040
|
|
|
if ( |
1041
|
9 |
|
is_string($array) |
1042
|
|
|
|| |
1043
|
2 |
|
($isObject && method_exists($array, '__toString')) |
1044
|
9 |
|
) { |
1045
|
7 |
|
return array((string)$array); |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
2 |
|
throw new \InvalidArgumentException( |
1049
|
|
|
'Passed value should be a array' |
1050
|
2 |
|
); |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
/** |
1054
|
|
|
* Find all items in an array that pass the truth test. |
1055
|
|
|
* |
1056
|
|
|
* @param \Closure|null $closure |
1057
|
|
|
* |
1058
|
|
|
* @return static <p>(Immutable)</p> |
1059
|
|
|
*/ |
1060
|
9 |
|
public function filter($closure = null) |
1061
|
|
|
{ |
1062
|
9 |
|
if (!$closure) { |
1063
|
1 |
|
return $this->clean(); |
1064
|
|
|
} |
1065
|
|
|
|
1066
|
9 |
|
$array = \array_filter($this->array, $closure); |
1067
|
|
|
|
1068
|
9 |
|
return static::create($array); |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
|
|
/** |
1072
|
|
|
* Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
1073
|
|
|
* within that. |
1074
|
|
|
* |
1075
|
|
|
* @param string $property |
1076
|
|
|
* @param string $value |
1077
|
|
|
* @param string $comparisonOp |
1078
|
|
|
* <p> |
1079
|
|
|
* 'eq' (equals),<br /> |
1080
|
|
|
* 'gt' (greater),<br /> |
1081
|
|
|
* 'gte' || 'ge' (greater or equals),<br /> |
1082
|
|
|
* 'lt' (less),<br /> |
1083
|
|
|
* 'lte' || 'le' (less or equals),<br /> |
1084
|
|
|
* 'ne' (not equals),<br /> |
1085
|
|
|
* 'contains',<br /> |
1086
|
|
|
* 'notContains',<br /> |
1087
|
|
|
* 'newer' (via strtotime),<br /> |
1088
|
|
|
* 'older' (via strtotime),<br /> |
1089
|
|
|
* </p> |
1090
|
|
|
* |
1091
|
|
|
* @return static <p>(Immutable)</p> |
1092
|
|
|
*/ |
1093
|
1 |
|
public function filterBy($property, $value, $comparisonOp = null) |
1094
|
|
|
{ |
1095
|
1 |
|
if (!$comparisonOp) { |
|
|
|
|
1096
|
1 |
|
$comparisonOp = is_array($value) ? 'contains' : 'eq'; |
1097
|
1 |
|
} |
1098
|
|
|
|
1099
|
|
|
$ops = array( |
1100
|
|
|
'eq' => function ($item, $prop, $value) { |
1101
|
1 |
|
return $item[$prop] === $value; |
1102
|
1 |
|
}, |
1103
|
|
|
'gt' => function ($item, $prop, $value) { |
1104
|
|
|
return $item[$prop] > $value; |
1105
|
1 |
|
}, |
1106
|
|
|
'ge' => function ($item, $prop, $value) { |
1107
|
|
|
return $item[$prop] >= $value; |
1108
|
1 |
|
}, |
1109
|
|
|
'gte' => function ($item, $prop, $value) { |
1110
|
|
|
return $item[$prop] >= $value; |
1111
|
1 |
|
}, |
1112
|
|
|
'lt' => function ($item, $prop, $value) { |
1113
|
1 |
|
return $item[$prop] < $value; |
1114
|
1 |
|
}, |
1115
|
|
|
'le' => function ($item, $prop, $value) { |
1116
|
|
|
return $item[$prop] <= $value; |
1117
|
1 |
|
}, |
1118
|
|
|
'lte' => function ($item, $prop, $value) { |
1119
|
|
|
return $item[$prop] <= $value; |
1120
|
1 |
|
}, |
1121
|
|
|
'ne' => function ($item, $prop, $value) { |
1122
|
|
|
return $item[$prop] !== $value; |
1123
|
1 |
|
}, |
1124
|
|
|
'contains' => function ($item, $prop, $value) { |
1125
|
1 |
|
return in_array($item[$prop], (array)$value, true); |
1126
|
1 |
|
}, |
1127
|
|
|
'notContains' => function ($item, $prop, $value) { |
1128
|
|
|
return !in_array($item[$prop], (array)$value, true); |
1129
|
1 |
|
}, |
1130
|
|
|
'newer' => function ($item, $prop, $value) { |
1131
|
|
|
return strtotime($item[$prop]) > strtotime($value); |
1132
|
1 |
|
}, |
1133
|
|
|
'older' => function ($item, $prop, $value) { |
1134
|
|
|
return strtotime($item[$prop]) < strtotime($value); |
1135
|
1 |
|
}, |
1136
|
1 |
|
); |
1137
|
|
|
|
1138
|
1 |
|
$result = \array_values( |
1139
|
1 |
|
\array_filter( |
1140
|
1 |
|
(array)$this->array, |
1141
|
|
|
function ($item) use ( |
1142
|
1 |
|
$property, |
1143
|
1 |
|
$value, |
1144
|
1 |
|
$ops, |
1145
|
1 |
|
$comparisonOp |
1146
|
|
|
) { |
1147
|
1 |
|
$item = (array)$item; |
1148
|
1 |
|
$itemArrayy = new Arrayy($item); |
1149
|
1 |
|
$item[$property] = $itemArrayy->get($property, array()); |
1150
|
|
|
|
1151
|
1 |
|
return $ops[$comparisonOp]($item, $property, $value); |
1152
|
|
|
} |
1153
|
1 |
|
) |
1154
|
1 |
|
); |
1155
|
|
|
|
1156
|
1 |
|
return static::create($result); |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
|
|
/** |
1160
|
|
|
* Find the first item in an array that passes the truth test, |
1161
|
|
|
* otherwise return false |
1162
|
|
|
* |
1163
|
|
|
* @param \Closure $closure |
1164
|
|
|
* |
1165
|
|
|
* @return mixed|false <p>Return false if we did not find the value.</p> |
1166
|
|
|
*/ |
1167
|
8 |
|
public function find(\Closure $closure) |
1168
|
|
|
{ |
1169
|
8 |
|
foreach ($this->array as $key => $value) { |
1170
|
6 |
|
if ($closure($value, $key)) { |
1171
|
5 |
|
return $value; |
1172
|
|
|
} |
1173
|
5 |
|
} |
1174
|
|
|
|
1175
|
3 |
|
return false; |
1176
|
|
|
} |
1177
|
|
|
|
1178
|
|
|
/** |
1179
|
|
|
* find by ... |
1180
|
|
|
* |
1181
|
|
|
* @param string $property |
1182
|
|
|
* @param string $value |
1183
|
|
|
* @param string $comparisonOp |
1184
|
|
|
* |
1185
|
|
|
* @return static <p>(Immutable)</p> |
1186
|
|
|
*/ |
1187
|
|
|
public function findBy($property, $value, $comparisonOp = 'eq') |
1188
|
|
|
{ |
1189
|
|
|
return $this->filterBy($property, $value, $comparisonOp); |
1190
|
|
|
} |
1191
|
|
|
|
1192
|
|
|
/** |
1193
|
|
|
* Get the first value from the current array. |
1194
|
|
|
* |
1195
|
|
|
* @return mixed <p>Return null if there wasn't a element.</p> |
1196
|
|
|
*/ |
1197
|
13 |
|
public function first() |
1198
|
|
|
{ |
1199
|
13 |
|
$tmpArray = $this->array; |
1200
|
13 |
|
$result = \array_shift($tmpArray); |
1201
|
|
|
|
1202
|
13 |
|
if ($result === null) { |
1203
|
3 |
|
return null; |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
10 |
|
return $result; |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
/** |
1210
|
|
|
* Get the first value(s) from the current array. |
1211
|
|
|
* |
1212
|
|
|
* @param int|null $number <p>How many values you will take?</p> |
1213
|
|
|
* |
1214
|
|
|
* @return static <p>(Immutable)</p> |
1215
|
|
|
*/ |
1216
|
28 |
|
public function firstsImmutable($number = null) |
1217
|
|
|
{ |
1218
|
28 |
|
if ($number === null) { |
1219
|
7 |
|
$arrayTmp = $this->array; |
1220
|
7 |
|
$array = (array)\array_shift($arrayTmp); |
1221
|
7 |
|
} else { |
1222
|
21 |
|
$number = (int)$number; |
1223
|
21 |
|
$arrayTmp = $this->array; |
1224
|
21 |
|
$array = \array_splice($arrayTmp, 0, $number, true); |
1225
|
|
|
} |
1226
|
|
|
|
1227
|
28 |
|
return static::create($array); |
1228
|
|
|
} |
1229
|
|
|
|
1230
|
|
|
/** |
1231
|
|
|
* Get the first value(s) from the current array. |
1232
|
|
|
* |
1233
|
|
|
* @param int|null $number <p>How many values you will take?</p> |
1234
|
|
|
* |
1235
|
|
|
* @return static <p>(Mutable)</p> |
1236
|
|
|
*/ |
1237
|
26 |
|
public function firstsMutable($number = null) |
1238
|
|
|
{ |
1239
|
26 |
|
if ($number === null) { |
1240
|
11 |
|
$this->array = (array)\array_shift($this->array); |
1241
|
11 |
|
} else { |
1242
|
15 |
|
$number = (int)$number; |
1243
|
15 |
|
$this->array = \array_splice($this->array, 0, $number, true); |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
26 |
|
return $this; |
1247
|
|
|
} |
1248
|
|
|
|
1249
|
|
|
/** |
1250
|
|
|
* Exchanges all keys with their associated values in an array. |
1251
|
|
|
* |
1252
|
|
|
* @return static <p>(Immutable)</p> |
1253
|
|
|
*/ |
1254
|
1 |
|
public function flip() |
1255
|
|
|
{ |
1256
|
1 |
|
$result = \array_flip($this->array); |
1257
|
|
|
|
1258
|
1 |
|
return static::create($result); |
1259
|
|
|
} |
1260
|
|
|
|
1261
|
|
|
/** |
1262
|
|
|
* Get a value from an array (optional using dot-notation). |
1263
|
|
|
* |
1264
|
|
|
* @param string $key <p>The key to look for.</p> |
1265
|
|
|
* @param mixed $fallback <p>Value to fallback to.</p> |
1266
|
|
|
* @param array $array <p>The array to get from, if it's set to "null" we use the current array from the |
1267
|
|
|
* class.</p> |
1268
|
|
|
* |
1269
|
|
|
* @return mixed |
1270
|
|
|
*/ |
1271
|
61 |
|
public function get($key, $fallback = null, $array = null) |
1272
|
|
|
{ |
1273
|
|
|
if ( |
1274
|
|
|
$array !== null |
1275
|
61 |
|
&& |
1276
|
3 |
|
is_array($array) |
1277
|
61 |
|
) { |
1278
|
3 |
|
$usedArray = $array; |
1279
|
3 |
|
} else { |
1280
|
59 |
|
$usedArray = $this->array; |
1281
|
|
|
} |
1282
|
|
|
|
1283
|
61 |
|
if ($key === null) { |
1284
|
1 |
|
return static::create($usedArray); |
1285
|
|
|
} |
1286
|
|
|
|
1287
|
|
|
// php cast "bool"-index into "int"-index |
1288
|
61 |
|
if ((bool)$key === $key) { |
1289
|
2 |
|
$key = (int)$key; |
1290
|
2 |
|
} |
1291
|
|
|
|
1292
|
61 |
|
if (\array_key_exists($key, $usedArray) === true) { |
1293
|
51 |
|
if (is_array($usedArray[$key])) { |
1294
|
6 |
|
return static::create($usedArray[$key]); |
1295
|
|
|
} |
1296
|
|
|
|
1297
|
47 |
|
return $usedArray[$key]; |
1298
|
|
|
} |
1299
|
|
|
|
1300
|
|
|
// Crawl through array, get key according to object or not |
1301
|
20 |
|
foreach (explode($this->pathSeparator, (string)$key) as $segment) { |
1302
|
20 |
|
if (!isset($usedArray[$segment])) { |
1303
|
19 |
|
return $fallback instanceof \Closure ? $fallback() : $fallback; |
1304
|
|
|
} |
1305
|
|
|
|
1306
|
5 |
|
$usedArray = $usedArray[$segment]; |
1307
|
5 |
|
} |
1308
|
|
|
|
1309
|
5 |
|
if (is_array($usedArray)) { |
1310
|
|
|
return static::create($usedArray); |
1311
|
|
|
} |
1312
|
|
|
|
1313
|
5 |
|
return $usedArray; |
1314
|
|
|
} |
1315
|
|
|
|
1316
|
|
|
/** |
1317
|
|
|
* Get the current array from the "Arrayy"-object. |
1318
|
|
|
* |
1319
|
|
|
* @return array |
1320
|
|
|
*/ |
1321
|
490 |
|
public function getArray() |
1322
|
|
|
{ |
1323
|
490 |
|
\array_map(array('self', 'internalGetArray'), $this->array); |
1324
|
|
|
|
1325
|
490 |
|
return $this->array; |
1326
|
|
|
} |
1327
|
|
|
|
1328
|
|
|
/** |
1329
|
|
|
* Returns the values from a single column of the input array, identified by |
1330
|
|
|
* the $columnKey, can be used to extract data-columns from multi-arrays. |
1331
|
|
|
* |
1332
|
|
|
* Info: Optionally, you may provide an $indexKey to index the values in the returned |
1333
|
|
|
* array by the values from the $indexKey column in the input array. |
1334
|
|
|
* |
1335
|
|
|
* @param mixed $columnKey |
1336
|
|
|
* @param mixed $indexKey |
1337
|
|
|
* |
1338
|
|
|
* @return static <p>(Immutable)</p> |
1339
|
|
|
*/ |
1340
|
1 |
|
public function getColumn($columnKey = null, $indexKey = null) |
1341
|
|
|
{ |
1342
|
1 |
|
$result = \array_column($this->array, $columnKey, $indexKey); |
1343
|
|
|
|
1344
|
1 |
|
return static::create($result); |
1345
|
|
|
} |
1346
|
|
|
|
1347
|
|
|
/** |
1348
|
|
|
* Get correct PHP constant for direction. |
1349
|
|
|
* |
1350
|
|
|
* @param int|string $direction |
1351
|
|
|
* |
1352
|
|
|
* @return int |
1353
|
|
|
*/ |
1354
|
38 |
|
protected function getDirection($direction) |
1355
|
|
|
{ |
1356
|
38 |
|
if (is_string($direction)) { |
1357
|
10 |
|
$direction = strtolower($direction); |
1358
|
|
|
|
1359
|
10 |
|
if ($direction === 'desc') { |
1360
|
2 |
|
$direction = SORT_DESC; |
1361
|
2 |
|
} else { |
1362
|
8 |
|
$direction = SORT_ASC; |
1363
|
|
|
} |
1364
|
10 |
|
} |
1365
|
|
|
|
1366
|
|
|
if ( |
1367
|
|
|
$direction !== SORT_DESC |
1368
|
38 |
|
&& |
1369
|
|
|
$direction !== SORT_ASC |
1370
|
38 |
|
) { |
1371
|
|
|
$direction = SORT_ASC; |
1372
|
|
|
} |
1373
|
|
|
|
1374
|
38 |
|
return $direction; |
1375
|
|
|
} |
1376
|
|
|
|
1377
|
|
|
/** |
1378
|
|
|
* alias: for "Arrayy->keys()" |
1379
|
|
|
* |
1380
|
|
|
* @see Arrayy::keys() |
1381
|
|
|
* |
1382
|
|
|
* @return static <p>(Immutable)</p> |
1383
|
|
|
*/ |
1384
|
1 |
|
public function getKeys() |
1385
|
|
|
{ |
1386
|
1 |
|
return $this->keys(); |
1387
|
|
|
} |
1388
|
|
|
|
1389
|
|
|
/** |
1390
|
|
|
* alias: for "Arrayy->randomImmutable()" |
1391
|
|
|
* |
1392
|
|
|
* @see Arrayy::randomImmutable() |
1393
|
|
|
* |
1394
|
|
|
* @return static <p>(Immutable)</p> |
1395
|
|
|
*/ |
1396
|
3 |
|
public function getRandom() |
1397
|
|
|
{ |
1398
|
3 |
|
return $this->randomImmutable(); |
1399
|
|
|
} |
1400
|
|
|
|
1401
|
|
|
/** |
1402
|
|
|
* alias: for "Arrayy->randomKey()" |
1403
|
|
|
* |
1404
|
|
|
* @see Arrayy::randomKey() |
1405
|
|
|
* |
1406
|
|
|
* @return mixed <p>Get a key/index or null if there wasn't a key/index.</p> |
1407
|
|
|
*/ |
1408
|
3 |
|
public function getRandomKey() |
1409
|
|
|
{ |
1410
|
3 |
|
return $this->randomKey(); |
1411
|
|
|
} |
1412
|
|
|
|
1413
|
|
|
/** |
1414
|
|
|
* alias: for "Arrayy->randomKeys()" |
1415
|
|
|
* |
1416
|
|
|
* @see Arrayy::randomKeys() |
1417
|
|
|
* |
1418
|
|
|
* @param int $number |
1419
|
|
|
* |
1420
|
|
|
* @return static <p>(Immutable)</p> |
1421
|
|
|
*/ |
1422
|
9 |
|
public function getRandomKeys($number) |
1423
|
|
|
{ |
1424
|
9 |
|
return $this->randomKeys($number); |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
/** |
1428
|
|
|
* alias: for "Arrayy->randomValue()" |
1429
|
|
|
* |
1430
|
|
|
* @see Arrayy::randomValue() |
1431
|
|
|
* |
1432
|
|
|
* @return mixed <p>get a random value or null if there wasn't a value.</p> |
1433
|
|
|
*/ |
1434
|
3 |
|
public function getRandomValue() |
1435
|
|
|
{ |
1436
|
3 |
|
return $this->randomValue(); |
1437
|
|
|
} |
1438
|
|
|
|
1439
|
|
|
/** |
1440
|
|
|
* alias: for "Arrayy->randomValues()" |
1441
|
|
|
* |
1442
|
|
|
* @see Arrayy::randomValues() |
1443
|
|
|
* |
1444
|
|
|
* @param int $number |
1445
|
|
|
* |
1446
|
|
|
* @return static <p>(Immutable)</p> |
1447
|
|
|
*/ |
1448
|
6 |
|
public function getRandomValues($number) |
1449
|
|
|
{ |
1450
|
6 |
|
return $this->randomValues($number); |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
|
|
/** |
1454
|
|
|
* Group values from a array according to the results of a closure. |
1455
|
|
|
* |
1456
|
|
|
* @param string $grouper <p>A callable function name.</p> |
1457
|
|
|
* @param bool $saveKeys |
1458
|
|
|
* |
1459
|
|
|
* @return static <p>(Immutable)</p> |
1460
|
|
|
*/ |
1461
|
3 |
|
public function group($grouper, $saveKeys = false) |
1462
|
|
|
{ |
1463
|
3 |
|
$array = (array)$this->array; |
1464
|
3 |
|
$result = array(); |
1465
|
|
|
|
1466
|
|
|
// Iterate over values, group by property/results from closure |
1467
|
3 |
|
foreach ($array as $key => $value) { |
1468
|
|
|
|
1469
|
3 |
|
$groupKey = is_callable($grouper) ? $grouper($value, $key) : $this->get($grouper, null, $value); |
1470
|
3 |
|
$newValue = $this->get($groupKey, null, $result); |
1471
|
|
|
|
1472
|
3 |
|
if ($groupKey instanceof self) { |
1473
|
|
|
$groupKey = $groupKey->getArray(); |
1474
|
|
|
} |
1475
|
|
|
|
1476
|
3 |
|
if ($newValue instanceof self) { |
1477
|
3 |
|
$newValue = $newValue->getArray(); |
1478
|
3 |
|
} |
1479
|
|
|
|
1480
|
|
|
// Add to results |
1481
|
3 |
|
if ($groupKey !== null) { |
1482
|
2 |
|
if ($saveKeys) { |
1483
|
1 |
|
$result[$groupKey] = $newValue; |
1484
|
1 |
|
$result[$groupKey][$key] = $value; |
1485
|
1 |
|
} else { |
1486
|
1 |
|
$result[$groupKey] = $newValue; |
1487
|
1 |
|
$result[$groupKey][] = $value; |
1488
|
|
|
} |
1489
|
2 |
|
} |
1490
|
|
|
|
1491
|
3 |
|
} |
1492
|
|
|
|
1493
|
3 |
|
return static::create($result); |
1494
|
|
|
} |
1495
|
|
|
|
1496
|
|
|
/** |
1497
|
|
|
* Check if an array has a given key. |
1498
|
|
|
* |
1499
|
|
|
* @param mixed $key |
1500
|
|
|
* |
1501
|
|
|
* @return bool |
1502
|
|
|
*/ |
1503
|
22 |
|
public function has($key) |
1504
|
|
|
{ |
1505
|
|
|
// Generate unique string to use as marker. |
1506
|
22 |
|
$unFound = (string)uniqid('arrayy', true); |
1507
|
|
|
|
1508
|
22 |
|
return $this->get($key, $unFound) !== $unFound; |
1509
|
|
|
} |
1510
|
|
|
|
1511
|
|
|
/** |
1512
|
|
|
* Implodes an array. |
1513
|
|
|
* |
1514
|
|
|
* @param string $glue |
1515
|
|
|
* |
1516
|
|
|
* @return string |
1517
|
|
|
*/ |
1518
|
27 |
|
public function implode($glue = '') |
1519
|
|
|
{ |
1520
|
27 |
|
return implode($glue, $this->array); |
1521
|
|
|
} |
1522
|
|
|
|
1523
|
|
|
/** |
1524
|
|
|
* Given a list and an iterate-function that returns |
1525
|
|
|
* a key for each element in the list (or a property name), |
1526
|
|
|
* returns an object with an index of each item. |
1527
|
|
|
* |
1528
|
|
|
* Just like groupBy, but for when you know your keys are unique. |
1529
|
|
|
* |
1530
|
|
|
* @param mixed $key |
1531
|
|
|
* |
1532
|
|
|
* @return static <p>(Immutable)</p> |
1533
|
|
|
*/ |
1534
|
3 |
|
public function indexBy($key) |
1535
|
|
|
{ |
1536
|
3 |
|
$results = array(); |
1537
|
|
|
|
1538
|
3 |
|
foreach ($this->array as $a) { |
1539
|
3 |
|
if (\array_key_exists($key, $a) === true) { |
1540
|
2 |
|
$results[$a[$key]] = $a; |
1541
|
2 |
|
} |
1542
|
3 |
|
} |
1543
|
|
|
|
1544
|
3 |
|
return static::create($results); |
1545
|
|
|
} |
1546
|
|
|
|
1547
|
|
|
/** |
1548
|
|
|
* alias: for "Arrayy->searchIndex()" |
1549
|
|
|
* |
1550
|
|
|
* @see Arrayy::searchIndex() |
1551
|
|
|
* |
1552
|
|
|
* @param mixed $value <p>The value to search for.</p> |
1553
|
|
|
* |
1554
|
|
|
* @return mixed |
1555
|
|
|
*/ |
1556
|
4 |
|
public function indexOf($value) |
1557
|
|
|
{ |
1558
|
4 |
|
return $this->searchIndex($value); |
1559
|
|
|
} |
1560
|
|
|
|
1561
|
|
|
/** |
1562
|
|
|
* Get everything but the last..$to items. |
1563
|
|
|
* |
1564
|
|
|
* @param int $to |
1565
|
|
|
* |
1566
|
|
|
* @return static <p>(Immutable)</p> |
1567
|
|
|
*/ |
1568
|
12 |
|
public function initial($to = 1) |
1569
|
|
|
{ |
1570
|
12 |
|
$slice = count($this->array) - $to; |
1571
|
|
|
|
1572
|
12 |
|
return $this->firstsImmutable($slice); |
1573
|
|
|
} |
1574
|
|
|
|
1575
|
|
|
/** |
1576
|
|
|
* @param mixed $value |
1577
|
|
|
*/ |
1578
|
405 |
|
protected function internalGetArray(&$value) |
1579
|
|
|
{ |
1580
|
405 |
|
if ($value instanceof self) { |
1581
|
|
|
$value &= $value->getArray(); |
1582
|
405 |
|
} elseif ($value instanceof \JsonSerializable) { |
|
|
|
|
1583
|
|
|
$value &= $value->jsonSerialize(); |
1584
|
|
|
} |
1585
|
405 |
|
} |
1586
|
|
|
|
1587
|
|
|
/** |
1588
|
|
|
* Internal mechanics of remove method. |
1589
|
|
|
* |
1590
|
|
|
* @param string $key |
1591
|
|
|
* |
1592
|
|
|
* @return boolean |
1593
|
|
|
*/ |
1594
|
18 |
|
protected function internalRemove($key) |
1595
|
|
|
{ |
1596
|
18 |
|
$path = explode($this->pathSeparator, (string)$key); |
1597
|
|
|
|
1598
|
|
|
// Crawl though the keys |
1599
|
18 |
|
while (count($path) > 1) { |
1600
|
|
|
$key = \array_shift($path); |
1601
|
|
|
|
1602
|
|
|
if (!$this->has($key)) { |
1603
|
|
|
return false; |
1604
|
|
|
} |
1605
|
|
|
|
1606
|
|
|
$this->array = &$this->array[$key]; |
1607
|
|
|
} |
1608
|
|
|
|
1609
|
18 |
|
$key = \array_shift($path); |
1610
|
|
|
|
1611
|
18 |
|
unset($this->array[$key]); |
1612
|
|
|
|
1613
|
18 |
|
return true; |
1614
|
|
|
} |
1615
|
|
|
|
1616
|
|
|
/** |
1617
|
|
|
* Internal mechanic of set method. |
1618
|
|
|
* |
1619
|
|
|
* @param string $key |
1620
|
|
|
* @param mixed $value |
1621
|
|
|
* |
1622
|
|
|
* @return bool |
1623
|
|
|
*/ |
1624
|
30 |
|
protected function internalSet($key, $value) |
1625
|
|
|
{ |
1626
|
30 |
|
if ($key === null) { |
1627
|
|
|
return false; |
1628
|
|
|
} |
1629
|
|
|
|
1630
|
|
|
// init |
1631
|
30 |
|
$array =& $this->array; |
1632
|
30 |
|
$path = explode($this->pathSeparator, (string)$key); |
1633
|
|
|
|
1634
|
|
|
// Crawl through the keys |
1635
|
30 |
|
while (count($path) > 1) { |
1636
|
3 |
|
$key = \array_shift($path); |
1637
|
|
|
|
1638
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
1639
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
1640
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
1641
|
3 |
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
1642
|
|
|
$array[$key] = static::create(array()); |
1643
|
|
|
} |
1644
|
|
|
|
1645
|
3 |
|
$array =& $array[$key]; |
1646
|
3 |
|
} |
1647
|
|
|
|
1648
|
30 |
|
$array[\array_shift($path)] = $value; |
1649
|
|
|
|
1650
|
30 |
|
return true; |
1651
|
|
|
} |
1652
|
|
|
|
1653
|
|
|
/** |
1654
|
|
|
* Return an array with all elements found in input array. |
1655
|
|
|
* |
1656
|
|
|
* @param array $search |
1657
|
|
|
* |
1658
|
|
|
* @return static <p>(Immutable)</p> |
1659
|
|
|
*/ |
1660
|
2 |
|
public function intersection(array $search) |
1661
|
|
|
{ |
1662
|
2 |
|
return static::create(\array_values(\array_intersect($this->array, $search))); |
1663
|
|
|
} |
1664
|
|
|
|
1665
|
|
|
/** |
1666
|
|
|
* Return a boolean flag which indicates whether the two input arrays have any common elements. |
1667
|
|
|
* |
1668
|
|
|
* @param array $search |
1669
|
|
|
* |
1670
|
|
|
* @return bool |
1671
|
|
|
*/ |
1672
|
1 |
|
public function intersects(array $search) |
1673
|
|
|
{ |
1674
|
1 |
|
return count($this->intersection($search)->array) > 0; |
1675
|
|
|
} |
1676
|
|
|
|
1677
|
|
|
/** |
1678
|
|
|
* Invoke a function on all of an array's values. |
1679
|
|
|
* |
1680
|
|
|
* @param mixed $callable |
1681
|
|
|
* @param mixed $arguments |
1682
|
|
|
* |
1683
|
|
|
* @return static <p>(Immutable)</p> |
1684
|
|
|
*/ |
1685
|
1 |
|
public function invoke($callable, $arguments = array()) |
1686
|
|
|
{ |
1687
|
|
|
// If one argument given for each iteration, create an array for it. |
1688
|
1 |
|
if (!is_array($arguments)) { |
1689
|
1 |
|
$arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray(); |
1690
|
1 |
|
} |
1691
|
|
|
|
1692
|
|
|
// If the callable has arguments, pass them. |
1693
|
1 |
|
if ($arguments) { |
|
|
|
|
1694
|
1 |
|
$array = \array_map($callable, $this->array, $arguments); |
1695
|
1 |
|
} else { |
1696
|
1 |
|
$array = \array_map($callable, $this->array); |
1697
|
|
|
} |
1698
|
|
|
|
1699
|
1 |
|
return static::create($array); |
1700
|
|
|
} |
1701
|
|
|
|
1702
|
|
|
/** |
1703
|
|
|
* Check whether array is associative or not. |
1704
|
|
|
* |
1705
|
|
|
* @return bool <p>Returns true if associative, false otherwise.</p> |
1706
|
|
|
*/ |
1707
|
15 |
|
public function isAssoc() |
1708
|
|
|
{ |
1709
|
15 |
|
if ($this->isEmpty()) { |
1710
|
3 |
|
return false; |
1711
|
|
|
} |
1712
|
|
|
|
1713
|
13 |
|
foreach ($this->keys()->getArray() as $key) { |
1714
|
13 |
|
if (!is_string($key)) { |
1715
|
11 |
|
return false; |
1716
|
|
|
} |
1717
|
3 |
|
} |
1718
|
|
|
|
1719
|
3 |
|
return true; |
1720
|
|
|
} |
1721
|
|
|
|
1722
|
|
|
/** |
1723
|
|
|
* Check whether the array is empty or not. |
1724
|
|
|
* |
1725
|
|
|
* @return bool <p>Returns true if empty, false otherwise.</p> |
1726
|
|
|
*/ |
1727
|
88 |
|
public function isEmpty() |
1728
|
|
|
{ |
1729
|
88 |
|
return !$this->array; |
1730
|
|
|
} |
1731
|
|
|
|
1732
|
|
|
/** |
1733
|
|
|
* Check if the current array is equal to the given "$array" or not. |
1734
|
|
|
* |
1735
|
|
|
* @param array $array |
1736
|
|
|
* |
1737
|
|
|
* @return bool |
1738
|
|
|
*/ |
1739
|
|
|
public function isEqual(array $array) |
1740
|
|
|
{ |
1741
|
|
|
return ($this->array === $array); |
1742
|
|
|
} |
1743
|
|
|
|
1744
|
|
|
/** |
1745
|
|
|
* Check if the current array is a multi-array. |
1746
|
|
|
* |
1747
|
|
|
* @return bool |
1748
|
|
|
*/ |
1749
|
14 |
|
public function isMultiArray() |
1750
|
|
|
{ |
1751
|
14 |
|
return !(count($this->array) === count($this->array, COUNT_RECURSIVE)); |
1752
|
|
|
} |
1753
|
|
|
|
1754
|
|
|
/** |
1755
|
|
|
* Check whether array is numeric or not. |
1756
|
|
|
* |
1757
|
|
|
* @return bool <p>Returns true if numeric, false otherwise.</p> |
1758
|
|
|
*/ |
1759
|
5 |
|
public function isNumeric() |
1760
|
|
|
{ |
1761
|
5 |
|
if ($this->isEmpty()) { |
1762
|
2 |
|
return false; |
1763
|
|
|
} |
1764
|
|
|
|
1765
|
4 |
|
foreach ($this->keys() as $key) { |
1766
|
4 |
|
if (!is_int($key)) { |
1767
|
2 |
|
return false; |
1768
|
|
|
} |
1769
|
3 |
|
} |
1770
|
|
|
|
1771
|
2 |
|
return true; |
1772
|
|
|
} |
1773
|
|
|
|
1774
|
|
|
/** |
1775
|
|
|
* Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not. |
1776
|
|
|
* |
1777
|
|
|
* @return bool |
1778
|
|
|
*/ |
1779
|
1 |
|
public function isSequential() |
1780
|
|
|
{ |
1781
|
1 |
|
return \array_keys($this->array) === range(0, count($this->array) - 1); |
1782
|
|
|
} |
1783
|
|
|
|
1784
|
|
|
/** |
1785
|
|
|
* @return array |
1786
|
|
|
*/ |
1787
|
|
|
public function jsonSerialize() |
1788
|
|
|
{ |
1789
|
|
|
return $this->getArray(); |
1790
|
|
|
} |
1791
|
|
|
|
1792
|
|
|
/** |
1793
|
|
|
* Get all keys from the current array. |
1794
|
|
|
* |
1795
|
|
|
* @return static <p>(Immutable)</p> |
1796
|
|
|
*/ |
1797
|
25 |
|
public function keys() |
1798
|
|
|
{ |
1799
|
25 |
|
return static::create(\array_keys($this->array)); |
1800
|
|
|
} |
1801
|
|
|
|
1802
|
|
|
/** |
1803
|
|
|
* Sort an array by key in reverse order. |
1804
|
|
|
* |
1805
|
|
|
* @param int $sort_flags [optional] <p> |
1806
|
|
|
* You may modify the behavior of the sort using the optional |
1807
|
|
|
* parameter sort_flags, for details |
1808
|
|
|
* see sort. |
1809
|
|
|
* </p> |
1810
|
|
|
* |
1811
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
1812
|
|
|
*/ |
1813
|
4 |
|
public function krsort($sort_flags = null) |
1814
|
|
|
{ |
1815
|
4 |
|
krsort($this->array, $sort_flags); |
1816
|
|
|
|
1817
|
4 |
|
return $this; |
1818
|
|
|
} |
1819
|
|
|
|
1820
|
|
|
/** |
1821
|
|
|
* Get the last value from the current array. |
1822
|
|
|
* |
1823
|
|
|
* @return mixed <p>Return null if there wasn't a element.</p> |
1824
|
|
|
*/ |
1825
|
4 |
|
public function last() |
1826
|
|
|
{ |
1827
|
4 |
|
return $this->pop(); |
1828
|
|
|
} |
1829
|
|
|
|
1830
|
|
|
/** |
1831
|
|
|
* Get the last value(s) from the current array. |
1832
|
|
|
* |
1833
|
|
|
* @param int|null $number |
1834
|
|
|
* |
1835
|
|
|
* @return static <p>(Immutable)</p> |
1836
|
|
|
*/ |
1837
|
13 |
|
public function lastsImmutable($number = null) |
1838
|
|
|
{ |
1839
|
13 |
|
if ($this->isEmpty()) { |
1840
|
1 |
|
return static::create(); |
1841
|
|
|
} |
1842
|
|
|
|
1843
|
12 |
|
if ($number === null) { |
1844
|
8 |
|
$poppedValue = $this->pop(); |
1845
|
|
|
|
1846
|
8 |
|
if ($poppedValue === null) { |
1847
|
1 |
|
$poppedValue = array($poppedValue); |
1848
|
1 |
|
} else { |
1849
|
7 |
|
$poppedValue = (array)$poppedValue; |
1850
|
|
|
} |
1851
|
|
|
|
1852
|
8 |
|
$arrayy = static::create($poppedValue); |
1853
|
8 |
|
} else { |
1854
|
4 |
|
$number = (int)$number; |
1855
|
4 |
|
$arrayy = $this->rest(-$number); |
1856
|
|
|
} |
1857
|
|
|
|
1858
|
12 |
|
return $arrayy; |
1859
|
|
|
} |
1860
|
|
|
|
1861
|
|
|
/** |
1862
|
|
|
* Get the last value(s) from the current array. |
1863
|
|
|
* |
1864
|
|
|
* @param int|null $number |
1865
|
|
|
* |
1866
|
|
|
* @return static <p>(Mutable)</p> |
1867
|
|
|
*/ |
1868
|
13 |
|
public function lastsMutable($number = null) |
1869
|
|
|
{ |
1870
|
13 |
|
if ($this->isEmpty()) { |
1871
|
1 |
|
return $this; |
1872
|
|
|
} |
1873
|
|
|
|
1874
|
12 |
|
if ($number === null) { |
1875
|
8 |
|
$poppedValue = $this->pop(); |
1876
|
|
|
|
1877
|
8 |
|
if ($poppedValue === null) { |
1878
|
1 |
|
$poppedValue = array($poppedValue); |
1879
|
1 |
|
} else { |
1880
|
7 |
|
$poppedValue = (array)$poppedValue; |
1881
|
|
|
} |
1882
|
|
|
|
1883
|
8 |
|
$this->array = static::create($poppedValue)->array; |
1884
|
8 |
|
} else { |
1885
|
4 |
|
$number = (int)$number; |
1886
|
4 |
|
$this->array = $this->rest(-$number)->array; |
1887
|
|
|
} |
1888
|
|
|
|
1889
|
12 |
|
return $this; |
1890
|
|
|
} |
1891
|
|
|
|
1892
|
|
|
/** |
1893
|
|
|
* Count the values from the current array. |
1894
|
|
|
* |
1895
|
|
|
* alias: for "Arrayy->size()" |
1896
|
|
|
* |
1897
|
|
|
* @see Arrayy::size() |
1898
|
|
|
* |
1899
|
|
|
* @return int |
1900
|
|
|
*/ |
1901
|
10 |
|
public function length() |
1902
|
|
|
{ |
1903
|
10 |
|
return $this->size(); |
1904
|
|
|
} |
1905
|
|
|
|
1906
|
|
|
/** |
1907
|
|
|
* Apply the given function to the every element of the array, |
1908
|
|
|
* collecting the results. |
1909
|
|
|
* |
1910
|
|
|
* @param callable $callable |
1911
|
|
|
* |
1912
|
|
|
* @return static <p>(Immutable) Arrayy object with modified elements.</p> |
1913
|
|
|
*/ |
1914
|
4 |
|
public function map($callable) |
1915
|
|
|
{ |
1916
|
4 |
|
$result = \array_map($callable, $this->array); |
1917
|
|
|
|
1918
|
4 |
|
return static::create($result); |
1919
|
|
|
} |
1920
|
|
|
|
1921
|
|
|
/** |
1922
|
|
|
* Check if all items in current array match a truth test. |
1923
|
|
|
* |
1924
|
|
|
* @param \Closure $closure |
1925
|
|
|
* |
1926
|
|
|
* @return bool |
1927
|
|
|
*/ |
1928
|
15 |
View Code Duplication |
public function matches(\Closure $closure) |
|
|
|
|
1929
|
|
|
{ |
1930
|
15 |
|
if (count($this->array) === 0) { |
1931
|
2 |
|
return false; |
1932
|
|
|
} |
1933
|
|
|
|
1934
|
|
|
// init |
1935
|
13 |
|
$array = $this->array; |
1936
|
|
|
|
1937
|
13 |
|
foreach ($array as $key => $value) { |
1938
|
13 |
|
$value = $closure($value, $key); |
1939
|
|
|
|
1940
|
13 |
|
if ($value === false) { |
1941
|
7 |
|
return false; |
1942
|
|
|
} |
1943
|
9 |
|
} |
1944
|
|
|
|
1945
|
7 |
|
return true; |
1946
|
|
|
} |
1947
|
|
|
|
1948
|
|
|
/** |
1949
|
|
|
* Check if any item in the current array matches a truth test. |
1950
|
|
|
* |
1951
|
|
|
* @param \Closure $closure |
1952
|
|
|
* |
1953
|
|
|
* @return bool |
1954
|
|
|
*/ |
1955
|
14 |
View Code Duplication |
public function matchesAny(\Closure $closure) |
|
|
|
|
1956
|
|
|
{ |
1957
|
14 |
|
if (count($this->array) === 0) { |
1958
|
2 |
|
return false; |
1959
|
|
|
} |
1960
|
|
|
|
1961
|
|
|
// init |
1962
|
12 |
|
$array = $this->array; |
1963
|
|
|
|
1964
|
12 |
|
foreach ($array as $key => $value) { |
1965
|
12 |
|
$value = $closure($value, $key); |
1966
|
|
|
|
1967
|
12 |
|
if ($value === true) { |
1968
|
9 |
|
return true; |
1969
|
|
|
} |
1970
|
5 |
|
} |
1971
|
|
|
|
1972
|
4 |
|
return false; |
1973
|
|
|
} |
1974
|
|
|
|
1975
|
|
|
/** |
1976
|
|
|
* Get the max value from an array. |
1977
|
|
|
* |
1978
|
|
|
* @return mixed |
1979
|
|
|
*/ |
1980
|
10 |
|
public function max() |
1981
|
|
|
{ |
1982
|
10 |
|
if ($this->count() === 0) { |
1983
|
1 |
|
return false; |
1984
|
|
|
} |
1985
|
|
|
|
1986
|
9 |
|
return max($this->array); |
1987
|
|
|
} |
1988
|
|
|
|
1989
|
|
|
/** |
1990
|
|
|
* Merge the new $array into the current array. |
1991
|
|
|
* |
1992
|
|
|
* - keep key,value from the current array, also if the index is in the new $array |
1993
|
|
|
* |
1994
|
|
|
* @param array $array |
1995
|
|
|
* @param bool $recursive |
1996
|
|
|
* |
1997
|
|
|
* @return static <p>(Immutable)</p> |
1998
|
|
|
*/ |
1999
|
25 |
View Code Duplication |
public function mergeAppendKeepIndex(array $array = array(), $recursive = false) |
|
|
|
|
2000
|
|
|
{ |
2001
|
25 |
|
if (true === $recursive) { |
2002
|
4 |
|
$result = \array_replace_recursive($this->array, $array); |
2003
|
4 |
|
} else { |
2004
|
21 |
|
$result = \array_replace($this->array, $array); |
2005
|
|
|
} |
2006
|
|
|
|
2007
|
25 |
|
return static::create($result); |
2008
|
|
|
} |
2009
|
|
|
|
2010
|
|
|
/** |
2011
|
|
|
* Merge the new $array into the current array. |
2012
|
|
|
* |
2013
|
|
|
* - replace duplicate assoc-keys from the current array with the key,values from the new $array |
2014
|
|
|
* - create new indexes |
2015
|
|
|
* |
2016
|
|
|
* @param array $array |
2017
|
|
|
* @param bool $recursive |
2018
|
|
|
* |
2019
|
|
|
* @return static <p>(Immutable)</p> |
2020
|
|
|
*/ |
2021
|
16 |
View Code Duplication |
public function mergeAppendNewIndex(array $array = array(), $recursive = false) |
|
|
|
|
2022
|
|
|
{ |
2023
|
16 |
|
if (true === $recursive) { |
2024
|
4 |
|
$result = \array_merge_recursive($this->array, $array); |
2025
|
4 |
|
} else { |
2026
|
12 |
|
$result = \array_merge($this->array, $array); |
2027
|
|
|
} |
2028
|
|
|
|
2029
|
16 |
|
return static::create($result); |
2030
|
|
|
} |
2031
|
|
|
|
2032
|
|
|
/** |
2033
|
|
|
* Merge the the current array into the $array. |
2034
|
|
|
* |
2035
|
|
|
* - use key,value from the new $array, also if the index is in the current array |
2036
|
|
|
* |
2037
|
|
|
* @param array $array |
2038
|
|
|
* @param bool $recursive |
2039
|
|
|
* |
2040
|
|
|
* @return static <p>(Immutable)</p> |
2041
|
|
|
*/ |
2042
|
16 |
View Code Duplication |
public function mergePrependKeepIndex(array $array = array(), $recursive = false) |
|
|
|
|
2043
|
|
|
{ |
2044
|
16 |
|
if (true === $recursive) { |
2045
|
4 |
|
$result = \array_replace_recursive($array, $this->array); |
2046
|
4 |
|
} else { |
2047
|
12 |
|
$result = \array_replace($array, $this->array); |
2048
|
|
|
} |
2049
|
|
|
|
2050
|
16 |
|
return static::create($result); |
2051
|
|
|
} |
2052
|
|
|
|
2053
|
|
|
/** |
2054
|
|
|
* Merge the current array into the new $array. |
2055
|
|
|
* |
2056
|
|
|
* - replace duplicate assoc-keys from new $array with the key,values from the current array |
2057
|
|
|
* - create new indexes |
2058
|
|
|
* |
2059
|
|
|
* @param array $array |
2060
|
|
|
* @param bool $recursive |
2061
|
|
|
* |
2062
|
|
|
* @return static <p>(Immutable)</p> |
2063
|
|
|
*/ |
2064
|
17 |
View Code Duplication |
public function mergePrependNewIndex(array $array = array(), $recursive = false) |
|
|
|
|
2065
|
|
|
{ |
2066
|
17 |
|
if (true === $recursive) { |
2067
|
4 |
|
$result = \array_merge_recursive($array, $this->array); |
2068
|
4 |
|
} else { |
2069
|
13 |
|
$result = \array_merge($array, $this->array); |
2070
|
|
|
} |
2071
|
|
|
|
2072
|
17 |
|
return static::create($result); |
2073
|
|
|
} |
2074
|
|
|
|
2075
|
|
|
/** |
2076
|
|
|
* Get the min value from an array. |
2077
|
|
|
* |
2078
|
|
|
* @return mixed |
2079
|
|
|
*/ |
2080
|
10 |
|
public function min() |
2081
|
|
|
{ |
2082
|
10 |
|
if ($this->count() === 0) { |
2083
|
1 |
|
return false; |
2084
|
|
|
} |
2085
|
|
|
|
2086
|
9 |
|
return min($this->array); |
2087
|
|
|
} |
2088
|
|
|
|
2089
|
|
|
/** |
2090
|
|
|
* Move an array element to a new index. |
2091
|
|
|
* |
2092
|
|
|
* cherry-picked from: http://stackoverflow.com/questions/12624153/move-an-array-element-to-a-new-index-in-php |
2093
|
|
|
* |
2094
|
|
|
* @param int|string $from |
2095
|
|
|
* @param int|string $to |
2096
|
|
|
* |
2097
|
|
|
* @return static <p>(Immutable)</p> |
2098
|
|
|
*/ |
2099
|
1 |
|
public function moveElement($from, $to) |
2100
|
|
|
{ |
2101
|
1 |
|
$array = $this->array; |
2102
|
|
|
|
2103
|
1 |
|
if (is_int($from)) { |
2104
|
1 |
|
$tmp = \array_splice($array, $from, 1); |
2105
|
1 |
|
\array_splice($array, $to, 0, $tmp); |
2106
|
1 |
|
$output = $array; |
2107
|
1 |
|
} elseif (is_string($from)) { |
2108
|
1 |
|
$indexToMove = \array_search($from, \array_keys($array), true); |
2109
|
1 |
|
$itemToMove = $array[$from]; |
2110
|
1 |
|
\array_splice($array, $indexToMove, 1); |
2111
|
1 |
|
$i = 0; |
2112
|
1 |
|
$output = array(); |
2113
|
1 |
|
foreach ($array as $key => $item) { |
2114
|
1 |
|
if ($i == $to) { |
2115
|
1 |
|
$output[$from] = $itemToMove; |
2116
|
1 |
|
} |
2117
|
1 |
|
$output[$key] = $item; |
2118
|
1 |
|
$i++; |
2119
|
1 |
|
} |
2120
|
1 |
|
} else { |
2121
|
|
|
$output = array(); |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
1 |
|
return static::create($output); |
2125
|
|
|
} |
2126
|
|
|
|
2127
|
|
|
/** |
2128
|
|
|
* Get a subset of the items from the given array. |
2129
|
|
|
* |
2130
|
|
|
* @param mixed[] $keys |
2131
|
|
|
* |
2132
|
|
|
* @return static <p>(Immutable)</p> |
2133
|
|
|
*/ |
2134
|
|
|
public function only(array $keys) |
2135
|
|
|
{ |
2136
|
|
|
$array = $this->array; |
2137
|
|
|
|
2138
|
|
|
return static::create(\array_intersect_key($array, \array_flip($keys))); |
2139
|
|
|
} |
2140
|
|
|
|
2141
|
|
|
/** |
2142
|
|
|
* Pad array to the specified size with a given value. |
2143
|
|
|
* |
2144
|
|
|
* @param int $size <p>Size of the result array.</p> |
2145
|
|
|
* @param mixed $value <p>Empty value by default.</p> |
2146
|
|
|
* |
2147
|
|
|
* @return static <p>(Immutable) Arrayy object padded to $size with $value.</p> |
2148
|
|
|
*/ |
2149
|
4 |
|
public function pad($size, $value) |
2150
|
|
|
{ |
2151
|
4 |
|
$result = \array_pad($this->array, $size, $value); |
2152
|
|
|
|
2153
|
4 |
|
return static::create($result); |
2154
|
|
|
} |
2155
|
|
|
|
2156
|
|
|
/** |
2157
|
|
|
* Pop a specified value off the end of the current array. |
2158
|
|
|
* |
2159
|
|
|
* @return mixed <p>(Mutable) The popped element from the current array.</p> |
2160
|
|
|
*/ |
2161
|
16 |
|
public function pop() |
2162
|
|
|
{ |
2163
|
16 |
|
return \array_pop($this->array); |
2164
|
|
|
} |
2165
|
|
|
|
2166
|
|
|
/** |
2167
|
|
|
* Prepend a value to the current array. |
2168
|
|
|
* |
2169
|
|
|
* @param mixed $value |
2170
|
|
|
* @param mixed $key |
2171
|
|
|
* |
2172
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
2173
|
|
|
*/ |
2174
|
8 |
|
public function prepend($value, $key = null) |
2175
|
|
|
{ |
2176
|
8 |
|
if ($key === null) { |
2177
|
8 |
|
\array_unshift($this->array, $value); |
2178
|
8 |
|
} else { |
2179
|
|
|
/** @noinspection AdditionOperationOnArraysInspection */ |
2180
|
1 |
|
$this->array = array($key => $value) + $this->array; |
2181
|
|
|
} |
2182
|
|
|
|
2183
|
8 |
|
return $this; |
2184
|
|
|
} |
2185
|
|
|
|
2186
|
|
|
/** |
2187
|
|
|
* Push one or more values onto the end of array at once. |
2188
|
|
|
* |
2189
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p> |
2190
|
|
|
*/ |
2191
|
4 |
View Code Duplication |
public function push(/* variadic arguments allowed */) |
|
|
|
|
2192
|
|
|
{ |
2193
|
4 |
|
if (func_num_args()) { |
2194
|
4 |
|
$args = \array_merge(array(&$this->array), func_get_args()); |
2195
|
4 |
|
call_user_func_array('array_push', $args); |
2196
|
4 |
|
} |
2197
|
|
|
|
2198
|
4 |
|
return $this; |
2199
|
|
|
} |
2200
|
|
|
|
2201
|
|
|
/** |
2202
|
|
|
* Get a random value from the current array. |
2203
|
|
|
* |
2204
|
|
|
* @param null|int $number <p>How many values you will take?</p> |
2205
|
|
|
* |
2206
|
|
|
* @return static <p>(Immutable)</p> |
2207
|
|
|
*/ |
2208
|
17 |
|
public function randomImmutable($number = null) |
2209
|
|
|
{ |
2210
|
17 |
|
if ($this->count() === 0) { |
2211
|
|
|
return static::create(); |
2212
|
|
|
} |
2213
|
|
|
|
2214
|
17 |
View Code Duplication |
if ($number === null) { |
|
|
|
|
2215
|
14 |
|
$arrayRandValue = array($this->array[\array_rand($this->array)]); |
2216
|
|
|
|
2217
|
14 |
|
return static::create($arrayRandValue); |
2218
|
|
|
} |
2219
|
|
|
|
2220
|
5 |
|
$arrayTmp = $this->array; |
2221
|
5 |
|
shuffle($arrayTmp); |
2222
|
|
|
|
2223
|
5 |
|
return static::create($arrayTmp)->firstsImmutable($number); |
2224
|
|
|
} |
2225
|
|
|
|
2226
|
|
|
/** |
2227
|
|
|
* Pick a random key/index from the keys of this array. |
2228
|
|
|
* |
2229
|
|
|
* @return mixed <p>Get a key/index or null if there wasn't a key/index.</p> |
2230
|
|
|
* |
2231
|
|
|
* @throws \RangeException If array is empty |
2232
|
|
|
*/ |
2233
|
4 |
|
public function randomKey() |
2234
|
|
|
{ |
2235
|
4 |
|
$result = $this->randomKeys(1); |
2236
|
|
|
|
2237
|
4 |
|
if (!isset($result[0])) { |
2238
|
|
|
$result[0] = null; |
2239
|
|
|
} |
2240
|
|
|
|
2241
|
4 |
|
return $result[0]; |
2242
|
|
|
} |
2243
|
|
|
|
2244
|
|
|
/** |
2245
|
|
|
* Pick a given number of random keys/indexes out of this array. |
2246
|
|
|
* |
2247
|
|
|
* @param int $number <p>The number of keys/indexes (should be <= $this->count())</p> |
2248
|
|
|
* |
2249
|
|
|
* @return static <p>(Immutable)</p> |
2250
|
|
|
* |
2251
|
|
|
* @throws \RangeException If array is empty |
2252
|
|
|
*/ |
2253
|
14 |
|
public function randomKeys($number) |
2254
|
|
|
{ |
2255
|
14 |
|
$number = (int)$number; |
2256
|
14 |
|
$count = $this->count(); |
2257
|
|
|
|
2258
|
14 |
|
if ($number === 0 || $number > $count) { |
2259
|
3 |
|
throw new \RangeException( |
2260
|
3 |
|
sprintf( |
2261
|
3 |
|
'Number of requested keys (%s) must be equal or lower than number of elements in this array (%s)', |
2262
|
3 |
|
$number, |
2263
|
|
|
$count |
2264
|
3 |
|
) |
2265
|
3 |
|
); |
2266
|
|
|
} |
2267
|
|
|
|
2268
|
11 |
|
$result = (array)\array_rand($this->array, $number); |
2269
|
|
|
|
2270
|
11 |
|
return static::create($result); |
2271
|
|
|
} |
2272
|
|
|
|
2273
|
|
|
/** |
2274
|
|
|
* Get a random value from the current array. |
2275
|
|
|
* |
2276
|
|
|
* @param null|int $number <p>How many values you will take?</p> |
2277
|
|
|
* |
2278
|
|
|
* @return static <p>(Mutable)</p> |
2279
|
|
|
*/ |
2280
|
17 |
|
public function randomMutable($number = null) |
2281
|
|
|
{ |
2282
|
17 |
|
if ($this->count() === 0) { |
2283
|
|
|
return static::create(); |
2284
|
|
|
} |
2285
|
|
|
|
2286
|
17 |
View Code Duplication |
if ($number === null) { |
|
|
|
|
2287
|
7 |
|
$arrayRandValue = array($this->array[\array_rand($this->array)]); |
2288
|
7 |
|
$this->array = $arrayRandValue; |
2289
|
|
|
|
2290
|
7 |
|
return $this; |
2291
|
|
|
} |
2292
|
|
|
|
2293
|
11 |
|
shuffle($this->array); |
2294
|
|
|
|
2295
|
11 |
|
return $this->firstsMutable($number); |
2296
|
|
|
} |
2297
|
|
|
|
2298
|
|
|
/** |
2299
|
|
|
* Pick a random value from the values of this array. |
2300
|
|
|
* |
2301
|
|
|
* @return mixed <p>Get a random value or null if there wasn't a value.</p> |
2302
|
|
|
*/ |
2303
|
4 |
|
public function randomValue() |
2304
|
|
|
{ |
2305
|
4 |
|
$result = $this->randomImmutable(); |
2306
|
|
|
|
2307
|
4 |
|
if (!isset($result[0])) { |
2308
|
|
|
$result[0] = null; |
2309
|
|
|
} |
2310
|
|
|
|
2311
|
4 |
|
return $result[0]; |
2312
|
|
|
} |
2313
|
|
|
|
2314
|
|
|
/** |
2315
|
|
|
* Pick a given number of random values out of this array. |
2316
|
|
|
* |
2317
|
|
|
* @param int $number |
2318
|
|
|
* |
2319
|
|
|
* @return static <p>(Mutable)</p> |
2320
|
|
|
*/ |
2321
|
7 |
|
public function randomValues($number) |
2322
|
|
|
{ |
2323
|
7 |
|
$number = (int)$number; |
2324
|
|
|
|
2325
|
7 |
|
return $this->randomMutable($number); |
2326
|
|
|
} |
2327
|
|
|
|
2328
|
|
|
/** |
2329
|
|
|
* Get a random value from an array, with the ability to skew the results. |
2330
|
|
|
* |
2331
|
|
|
* Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
2332
|
|
|
* |
2333
|
|
|
* @param array $array |
2334
|
|
|
* @param null|int $number <p>How many values you will take?</p> |
2335
|
|
|
* |
2336
|
|
|
* @return static <p>(Immutable)</p> |
2337
|
|
|
*/ |
2338
|
9 |
|
public function randomWeighted(array $array, $number = null) |
2339
|
|
|
{ |
2340
|
9 |
|
$options = array(); |
2341
|
9 |
|
foreach ($array as $option => $weight) { |
2342
|
9 |
|
if ($this->searchIndex($option) !== false) { |
2343
|
2 |
|
for ($i = 0; $i < $weight; ++$i) { |
2344
|
1 |
|
$options[] = $option; |
2345
|
1 |
|
} |
2346
|
2 |
|
} |
2347
|
9 |
|
} |
2348
|
|
|
|
2349
|
9 |
|
return $this->mergeAppendKeepIndex($options)->randomImmutable($number); |
2350
|
|
|
} |
2351
|
|
|
|
2352
|
|
|
/** |
2353
|
|
|
* Reduce the current array via callable e.g. anonymous-function. |
2354
|
|
|
* |
2355
|
|
|
* @param mixed $callable |
2356
|
|
|
* @param array $init |
2357
|
|
|
* |
2358
|
|
|
* @return static <p>(Immutable)</p> |
2359
|
|
|
*/ |
2360
|
4 |
|
public function reduce($callable, array $init = array()) |
2361
|
|
|
{ |
2362
|
4 |
|
$result = \array_reduce($this->array, $callable, $init); |
2363
|
|
|
|
2364
|
4 |
|
if ($result === null) { |
2365
|
|
|
$this->array = array(); |
2366
|
|
|
} else { |
2367
|
4 |
|
$this->array = (array)$result; |
2368
|
|
|
} |
2369
|
|
|
|
2370
|
4 |
|
return static::create($this->array); |
2371
|
|
|
} |
2372
|
|
|
|
2373
|
|
|
/** |
2374
|
|
|
* Create a numerically re-indexed Arrayy object. |
2375
|
|
|
* |
2376
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p> |
2377
|
|
|
*/ |
2378
|
9 |
|
public function reindex() |
2379
|
|
|
{ |
2380
|
9 |
|
$this->array = \array_values($this->array); |
2381
|
|
|
|
2382
|
9 |
|
return $this; |
2383
|
|
|
} |
2384
|
|
|
|
2385
|
|
|
/** |
2386
|
|
|
* Return all items that fail the truth test. |
2387
|
|
|
* |
2388
|
|
|
* @param \Closure $closure |
2389
|
|
|
* |
2390
|
|
|
* @return static <p>(Immutable)</p> |
2391
|
|
|
*/ |
2392
|
1 |
View Code Duplication |
public function reject(\Closure $closure) |
|
|
|
|
2393
|
|
|
{ |
2394
|
1 |
|
$filtered = array(); |
2395
|
|
|
|
2396
|
1 |
|
foreach ($this->array as $key => $value) { |
2397
|
1 |
|
if (!$closure($value, $key)) { |
2398
|
1 |
|
$filtered[$key] = $value; |
2399
|
1 |
|
} |
2400
|
1 |
|
} |
2401
|
|
|
|
2402
|
1 |
|
return static::create($filtered); |
2403
|
|
|
} |
2404
|
|
|
|
2405
|
|
|
/** |
2406
|
|
|
* Remove a value from the current array (optional using dot-notation). |
2407
|
|
|
* |
2408
|
|
|
* @param mixed $key |
2409
|
|
|
* |
2410
|
|
|
* @return static <p>(Immutable)</p> |
2411
|
|
|
*/ |
2412
|
18 |
|
public function remove($key) |
2413
|
|
|
{ |
2414
|
|
|
// Recursive call |
2415
|
18 |
|
if (is_array($key)) { |
2416
|
|
|
foreach ($key as $k) { |
2417
|
|
|
$this->internalRemove($k); |
2418
|
|
|
} |
2419
|
|
|
|
2420
|
|
|
return static::create($this->array); |
2421
|
|
|
} |
2422
|
|
|
|
2423
|
18 |
|
$this->internalRemove($key); |
2424
|
|
|
|
2425
|
18 |
|
return static::create($this->array); |
2426
|
|
|
} |
2427
|
|
|
|
2428
|
|
|
/** |
2429
|
|
|
* Remove the first value from the current array. |
2430
|
|
|
* |
2431
|
|
|
* @return static <p>(Immutable)</p> |
2432
|
|
|
*/ |
2433
|
7 |
|
public function removeFirst() |
2434
|
|
|
{ |
2435
|
7 |
|
$tmpArray = $this->array; |
2436
|
7 |
|
\array_shift($tmpArray); |
2437
|
|
|
|
2438
|
7 |
|
return static::create($tmpArray); |
2439
|
|
|
} |
2440
|
|
|
|
2441
|
|
|
/** |
2442
|
|
|
* Remove the last value from the current array. |
2443
|
|
|
* |
2444
|
|
|
* @return static <p>(Immutable)</p> |
2445
|
|
|
*/ |
2446
|
7 |
|
public function removeLast() |
2447
|
|
|
{ |
2448
|
7 |
|
$tmpArray = $this->array; |
2449
|
7 |
|
\array_pop($tmpArray); |
2450
|
|
|
|
2451
|
7 |
|
return static::create($tmpArray); |
2452
|
|
|
} |
2453
|
|
|
|
2454
|
|
|
/** |
2455
|
|
|
* Removes a particular value from an array (numeric or associative). |
2456
|
|
|
* |
2457
|
|
|
* @param mixed $value |
2458
|
|
|
* |
2459
|
|
|
* @return static <p>(Immutable)</p> |
2460
|
|
|
*/ |
2461
|
7 |
|
public function removeValue($value) |
2462
|
|
|
{ |
2463
|
7 |
|
$isNumericArray = true; |
2464
|
7 |
|
foreach ($this->array as $key => $item) { |
2465
|
6 |
|
if ($item === $value) { |
2466
|
6 |
|
if (!is_int($key)) { |
2467
|
|
|
$isNumericArray = false; |
2468
|
|
|
} |
2469
|
6 |
|
unset($this->array[$key]); |
2470
|
6 |
|
} |
2471
|
7 |
|
} |
2472
|
|
|
|
2473
|
7 |
|
if ($isNumericArray) { |
2474
|
7 |
|
$this->array = \array_values($this->array); |
2475
|
7 |
|
} |
2476
|
|
|
|
2477
|
7 |
|
return static::create($this->array); |
2478
|
|
|
} |
2479
|
|
|
|
2480
|
|
|
/** |
2481
|
|
|
* Generate array of repeated arrays. |
2482
|
|
|
* |
2483
|
|
|
* @param int $times <p>How many times has to be repeated.</p> |
2484
|
|
|
* |
2485
|
|
|
* @return Arrayy |
2486
|
|
|
*/ |
2487
|
1 |
|
public function repeat($times) |
2488
|
|
|
{ |
2489
|
1 |
|
if ($times === 0) { |
2490
|
1 |
|
return new static(); |
2491
|
|
|
} |
2492
|
|
|
|
2493
|
1 |
|
return static::create(\array_fill(0, (int)$times, $this->array)); |
2494
|
|
|
} |
2495
|
|
|
|
2496
|
|
|
/** |
2497
|
|
|
* Replace a key with a new key/value pair. |
2498
|
|
|
* |
2499
|
|
|
* @param $replace |
2500
|
|
|
* @param $key |
2501
|
|
|
* @param $value |
2502
|
|
|
* |
2503
|
|
|
* @return static <p>(Immutable)</p> |
2504
|
|
|
*/ |
2505
|
2 |
|
public function replace($replace, $key, $value) |
2506
|
|
|
{ |
2507
|
2 |
|
$this->remove($replace); |
2508
|
|
|
|
2509
|
2 |
|
return $this->set($key, $value); |
2510
|
|
|
} |
2511
|
|
|
|
2512
|
|
|
/** |
2513
|
|
|
* Create an array using the current array as values and the other array as keys. |
2514
|
|
|
* |
2515
|
|
|
* @param array $keys <p>An array of keys.</p> |
2516
|
|
|
* |
2517
|
|
|
* @return static <p>(Immutable) Arrayy object with keys from the other array.</p> |
2518
|
|
|
*/ |
2519
|
2 |
|
public function replaceAllKeys(array $keys) |
2520
|
|
|
{ |
2521
|
2 |
|
$result = \array_combine($keys, $this->array); |
2522
|
|
|
|
2523
|
2 |
|
return static::create($result); |
2524
|
|
|
} |
2525
|
|
|
|
2526
|
|
|
/** |
2527
|
|
|
* Create an array using the current array as keys and the other array as values. |
2528
|
|
|
* |
2529
|
|
|
* @param array $array <p>An array o values.</p> |
2530
|
|
|
* |
2531
|
|
|
* @return static <p>(Immutable) Arrayy object with values from the other array.</p> |
2532
|
|
|
*/ |
2533
|
2 |
|
public function replaceAllValues(array $array) |
2534
|
|
|
{ |
2535
|
2 |
|
$result = \array_combine($this->array, $array); |
2536
|
|
|
|
2537
|
2 |
|
return static::create($result); |
2538
|
|
|
} |
2539
|
|
|
|
2540
|
|
|
/** |
2541
|
|
|
* Replace the keys in an array with another set. |
2542
|
|
|
* |
2543
|
|
|
* @param array $keys <p>An array of keys matching the array's size</p> |
2544
|
|
|
* |
2545
|
|
|
* @return static <p>(Immutable)</p> |
2546
|
|
|
*/ |
2547
|
1 |
|
public function replaceKeys(array $keys) |
2548
|
|
|
{ |
2549
|
1 |
|
$values = \array_values($this->array); |
2550
|
1 |
|
$result = \array_combine($keys, $values); |
2551
|
|
|
|
2552
|
1 |
|
return static::create($result); |
2553
|
|
|
} |
2554
|
|
|
|
2555
|
|
|
/** |
2556
|
|
|
* Replace the first matched value in an array. |
2557
|
|
|
* |
2558
|
|
|
* @param mixed $search |
2559
|
|
|
* @param mixed $replacement |
2560
|
|
|
* |
2561
|
|
|
* @return static <p>(Immutable)</p> |
2562
|
|
|
*/ |
2563
|
3 |
|
public function replaceOneValue($search, $replacement = '') |
2564
|
|
|
{ |
2565
|
3 |
|
$array = $this->array; |
2566
|
3 |
|
$key = \array_search($search, $array, true); |
2567
|
|
|
|
2568
|
3 |
|
if ($key !== false) { |
2569
|
3 |
|
$array[$key] = $replacement; |
2570
|
3 |
|
} |
2571
|
|
|
|
2572
|
3 |
|
return static::create($array); |
2573
|
|
|
} |
2574
|
|
|
|
2575
|
|
|
/** |
2576
|
|
|
* Replace values in the current array. |
2577
|
|
|
* |
2578
|
|
|
* @param string $search <p>The string to replace.</p> |
2579
|
|
|
* @param string $replacement <p>What to replace it with.</p> |
2580
|
|
|
* |
2581
|
|
|
* @return static <p>(Immutable)</p> |
2582
|
|
|
*/ |
2583
|
1 |
|
public function replaceValues($search, $replacement = '') |
2584
|
|
|
{ |
2585
|
1 |
|
$array = $this->each( |
2586
|
|
|
function ($value) use ($search, $replacement) { |
2587
|
1 |
|
return UTF8::str_replace($search, $replacement, $value); |
2588
|
|
|
} |
2589
|
1 |
|
); |
2590
|
|
|
|
2591
|
1 |
|
return $array; |
2592
|
|
|
} |
2593
|
|
|
|
2594
|
|
|
/** |
2595
|
|
|
* Get the last elements from index $from until the end of this array. |
2596
|
|
|
* |
2597
|
|
|
* @param int $from |
2598
|
|
|
* |
2599
|
|
|
* @return static <p>(Immutable)</p> |
2600
|
|
|
*/ |
2601
|
15 |
|
public function rest($from = 1) |
2602
|
|
|
{ |
2603
|
15 |
|
$tmpArray = $this->array; |
2604
|
|
|
|
2605
|
15 |
|
return static::create(\array_splice($tmpArray, $from)); |
2606
|
|
|
} |
2607
|
|
|
|
2608
|
|
|
/** |
2609
|
|
|
* Return the array in the reverse order. |
2610
|
|
|
* |
2611
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2612
|
|
|
*/ |
2613
|
8 |
|
public function reverse() |
2614
|
|
|
{ |
2615
|
8 |
|
$this->array = \array_reverse($this->array); |
2616
|
|
|
|
2617
|
8 |
|
return $this; |
2618
|
|
|
} |
2619
|
|
|
|
2620
|
|
|
/** |
2621
|
|
|
* Sort an array in reverse order. |
2622
|
|
|
* |
2623
|
|
|
* @param int $sort_flags [optional] <p> |
2624
|
|
|
* You may modify the behavior of the sort using the optional |
2625
|
|
|
* parameter sort_flags, for details |
2626
|
|
|
* see sort. |
2627
|
|
|
* </p> |
2628
|
|
|
* |
2629
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2630
|
|
|
*/ |
2631
|
4 |
|
public function rsort($sort_flags = null) |
2632
|
|
|
{ |
2633
|
4 |
|
rsort($this->array, $sort_flags); |
2634
|
|
|
|
2635
|
4 |
|
return $this; |
2636
|
|
|
} |
2637
|
|
|
|
2638
|
|
|
/** |
2639
|
|
|
* Search for the first index of the current array via $value. |
2640
|
|
|
* |
2641
|
|
|
* @param mixed $value |
2642
|
|
|
* |
2643
|
|
|
* @return int|float|string |
2644
|
|
|
*/ |
2645
|
20 |
|
public function searchIndex($value) |
2646
|
|
|
{ |
2647
|
20 |
|
return \array_search($value, $this->array, true); |
2648
|
|
|
} |
2649
|
|
|
|
2650
|
|
|
/** |
2651
|
|
|
* Search for the value of the current array via $index. |
2652
|
|
|
* |
2653
|
|
|
* @param mixed $index |
2654
|
|
|
* |
2655
|
|
|
* @return static <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p> |
2656
|
|
|
*/ |
2657
|
9 |
|
public function searchValue($index) |
2658
|
|
|
{ |
2659
|
|
|
// init |
2660
|
9 |
|
$return = array(); |
2661
|
|
|
|
2662
|
9 |
|
if ($this->isEmpty()) { |
2663
|
|
|
return static::create(); |
2664
|
|
|
} |
2665
|
|
|
|
2666
|
|
|
// php cast "bool"-index into "int"-index |
2667
|
9 |
|
if ((bool)$index === $index) { |
2668
|
1 |
|
$index = (int)$index; |
2669
|
1 |
|
} |
2670
|
|
|
|
2671
|
9 |
|
if (\array_key_exists($index, $this->array) === true) { |
2672
|
7 |
|
$return = array($this->array[$index]); |
2673
|
7 |
|
} |
2674
|
|
|
|
2675
|
|
|
|
2676
|
9 |
|
return static::create($return); |
2677
|
|
|
} |
2678
|
|
|
|
2679
|
|
|
/** |
2680
|
|
|
* Set a value for the current array (optional using dot-notation). |
2681
|
|
|
* |
2682
|
|
|
* @param string $key <p>The key to set.</p> |
2683
|
|
|
* @param mixed $value <p>Its value.</p> |
2684
|
|
|
* |
2685
|
|
|
* @return static <p>(Immutable)</p> |
2686
|
|
|
*/ |
2687
|
17 |
|
public function set($key, $value) |
2688
|
|
|
{ |
2689
|
17 |
|
$this->internalSet($key, $value); |
2690
|
|
|
|
2691
|
17 |
|
return static::create($this->array); |
2692
|
|
|
} |
2693
|
|
|
|
2694
|
|
|
/** |
2695
|
|
|
* Get a value from a array and set it if it was not. |
2696
|
|
|
* |
2697
|
|
|
* WARNING: this method only set the value, if the $key is not already set |
2698
|
|
|
* |
2699
|
|
|
* @param string $key <p>The key</p> |
2700
|
|
|
* @param mixed $fallback <p>The default value to set if it isn't.</p> |
2701
|
|
|
* |
2702
|
|
|
* @return mixed <p>(Mutable)</p> |
2703
|
|
|
*/ |
2704
|
11 |
|
public function setAndGet($key, $fallback = null) |
2705
|
|
|
{ |
2706
|
|
|
// If the key doesn't exist, set it. |
2707
|
11 |
|
if (!$this->has($key)) { |
2708
|
4 |
|
$this->array = $this->set($key, $fallback)->getArray(); |
2709
|
4 |
|
} |
2710
|
|
|
|
2711
|
11 |
|
return $this->get($key); |
2712
|
|
|
} |
2713
|
|
|
|
2714
|
|
|
/** |
2715
|
|
|
* Shifts a specified value off the beginning of array. |
2716
|
|
|
* |
2717
|
|
|
* @return mixed <p>(Mutable) A shifted element from the current array.</p> |
2718
|
|
|
*/ |
2719
|
4 |
|
public function shift() |
2720
|
|
|
{ |
2721
|
4 |
|
return \array_shift($this->array); |
2722
|
|
|
} |
2723
|
|
|
|
2724
|
|
|
/** |
2725
|
|
|
* Shuffle the current array. |
2726
|
|
|
* |
2727
|
|
|
* @return static <p>(Immutable)</p> |
2728
|
|
|
*/ |
2729
|
1 |
|
public function shuffle() |
2730
|
|
|
{ |
2731
|
1 |
|
$array = $this->array; |
2732
|
|
|
|
2733
|
1 |
|
shuffle($array); |
2734
|
|
|
|
2735
|
1 |
|
return static::create($array); |
2736
|
|
|
} |
2737
|
|
|
|
2738
|
|
|
/** |
2739
|
|
|
* Get the size of an array. |
2740
|
|
|
* |
2741
|
|
|
* @return int |
2742
|
|
|
*/ |
2743
|
93 |
|
public function size() |
2744
|
|
|
{ |
2745
|
93 |
|
return count($this->array); |
2746
|
|
|
} |
2747
|
|
|
|
2748
|
|
|
/** |
2749
|
|
|
* Extract a slice of the array. |
2750
|
|
|
* |
2751
|
|
|
* @param int $offset <p>Slice begin index.</p> |
2752
|
|
|
* @param int|null $length <p>Length of the slice.</p> |
2753
|
|
|
* @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
2754
|
|
|
* |
2755
|
|
|
* @return static <p>A slice of the original array with length $length.</p> |
2756
|
|
|
*/ |
2757
|
4 |
|
public function slice($offset, $length = null, $preserveKeys = false) |
2758
|
|
|
{ |
2759
|
4 |
|
$result = \array_slice($this->array, $offset, $length, $preserveKeys); |
2760
|
|
|
|
2761
|
4 |
|
return static::create($result); |
2762
|
|
|
} |
2763
|
|
|
|
2764
|
|
|
/** |
2765
|
|
|
* Sort the current array and optional you can keep the keys. |
2766
|
|
|
* |
2767
|
|
|
* @param integer $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2768
|
|
|
* @param integer $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
2769
|
|
|
* <strong>SORT_NATURAL</strong></p> |
2770
|
|
|
* @param bool $keepKeys |
2771
|
|
|
* |
2772
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2773
|
|
|
*/ |
2774
|
19 |
|
public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
2775
|
|
|
{ |
2776
|
19 |
|
$this->sorting($this->array, $direction, $strategy, $keepKeys); |
2777
|
|
|
|
2778
|
19 |
|
return $this; |
2779
|
|
|
} |
2780
|
|
|
|
2781
|
|
|
/** |
2782
|
|
|
* Sort the current array by key. |
2783
|
|
|
* |
2784
|
|
|
* @link http://php.net/manual/en/function.ksort.php |
2785
|
|
|
* @link http://php.net/manual/en/function.krsort.php |
2786
|
|
|
* |
2787
|
|
|
* @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2788
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
2789
|
|
|
* <strong>SORT_NATURAL</strong></p> |
2790
|
|
|
* |
2791
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2792
|
|
|
*/ |
2793
|
18 |
|
public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR) |
2794
|
|
|
{ |
2795
|
18 |
|
$this->sorterKeys($this->array, $direction, $strategy); |
2796
|
|
|
|
2797
|
18 |
|
return $this; |
2798
|
|
|
} |
2799
|
|
|
|
2800
|
|
|
/** |
2801
|
|
|
* Sort the current array by value. |
2802
|
|
|
* |
2803
|
|
|
* @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2804
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
2805
|
|
|
* |
2806
|
|
|
* @return static <p>(Mutable)</p> |
2807
|
|
|
*/ |
2808
|
1 |
|
public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
2809
|
|
|
{ |
2810
|
1 |
|
return $this->sort($direction, $strategy, true); |
2811
|
|
|
} |
2812
|
|
|
|
2813
|
|
|
/** |
2814
|
|
|
* Sort the current array by value. |
2815
|
|
|
* |
2816
|
|
|
* @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2817
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
2818
|
|
|
* |
2819
|
|
|
* @return static <p>(Mutable)</p> |
2820
|
|
|
*/ |
2821
|
1 |
|
public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
2822
|
|
|
{ |
2823
|
1 |
|
return $this->sort($direction, $strategy, false); |
2824
|
|
|
} |
2825
|
|
|
|
2826
|
|
|
/** |
2827
|
|
|
* Sort a array by value, by a closure or by a property. |
2828
|
|
|
* |
2829
|
|
|
* - If the sorter is null, the array is sorted naturally. |
2830
|
|
|
* - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
2831
|
|
|
* |
2832
|
|
|
* @param null $sorter |
2833
|
|
|
* @param string|int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2834
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
2835
|
|
|
* <strong>SORT_NATURAL</strong></p> |
2836
|
|
|
* |
2837
|
|
|
* @return static <p>(Immutable)</p> |
2838
|
|
|
*/ |
2839
|
1 |
|
public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
2840
|
|
|
{ |
2841
|
1 |
|
$array = (array)$this->array; |
2842
|
1 |
|
$direction = $this->getDirection($direction); |
2843
|
|
|
|
2844
|
|
|
// Transform all values into their results. |
2845
|
1 |
|
if ($sorter) { |
2846
|
1 |
|
$arrayy = static::create($array); |
2847
|
|
|
|
2848
|
1 |
|
$that = $this; |
2849
|
1 |
|
$results = $arrayy->each( |
2850
|
|
|
function ($value) use ($sorter, $that) { |
2851
|
1 |
|
return is_callable($sorter) ? $sorter($value) : $that->get($sorter, null, $value); |
2852
|
|
|
} |
2853
|
1 |
|
); |
2854
|
|
|
|
2855
|
1 |
|
$results = $results->getArray(); |
2856
|
1 |
|
} else { |
2857
|
1 |
|
$results = $array; |
2858
|
|
|
} |
2859
|
|
|
|
2860
|
|
|
// Sort by the results and replace by original values |
2861
|
1 |
|
\array_multisort($results, $direction, $strategy, $array); |
2862
|
|
|
|
2863
|
1 |
|
return static::create($array); |
2864
|
|
|
} |
2865
|
|
|
|
2866
|
|
|
/** |
2867
|
|
|
* sorting keys |
2868
|
|
|
* |
2869
|
|
|
* @param array $elements |
2870
|
|
|
* @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2871
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
2872
|
|
|
* |
2873
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2874
|
|
|
*/ |
2875
|
18 |
|
protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
2876
|
|
|
{ |
2877
|
18 |
|
$direction = $this->getDirection($direction); |
2878
|
|
|
|
2879
|
|
|
switch ($direction) { |
2880
|
18 |
|
case 'desc': |
2881
|
18 |
|
case SORT_DESC: |
2882
|
6 |
|
krsort($elements, $strategy); |
2883
|
6 |
|
break; |
2884
|
13 |
|
case 'asc': |
2885
|
13 |
|
case SORT_ASC: |
2886
|
13 |
|
default: |
2887
|
13 |
|
ksort($elements, $strategy); |
2888
|
13 |
|
} |
2889
|
|
|
|
2890
|
18 |
|
return $this; |
2891
|
|
|
} |
2892
|
|
|
|
2893
|
|
|
/** |
2894
|
|
|
* @param array &$elements |
2895
|
|
|
* @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
2896
|
|
|
* @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
2897
|
|
|
* <strong>SORT_NATURAL</strong></p> |
2898
|
|
|
* @param bool $keepKeys |
2899
|
|
|
* |
2900
|
|
|
* @return static <p>(Mutable) Return this Arrayy object.</p> |
2901
|
|
|
*/ |
2902
|
19 |
|
protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
2903
|
|
|
{ |
2904
|
19 |
|
$direction = $this->getDirection($direction); |
2905
|
|
|
|
2906
|
19 |
|
if (!$strategy) { |
2907
|
19 |
|
$strategy = SORT_REGULAR; |
2908
|
19 |
|
} |
2909
|
|
|
|
2910
|
|
|
switch ($direction) { |
2911
|
19 |
|
case 'desc': |
2912
|
19 |
|
case SORT_DESC: |
2913
|
9 |
|
if ($keepKeys) { |
2914
|
5 |
|
arsort($elements, $strategy); |
2915
|
5 |
|
} else { |
2916
|
4 |
|
rsort($elements, $strategy); |
2917
|
|
|
} |
2918
|
9 |
|
break; |
2919
|
10 |
|
case 'asc': |
2920
|
10 |
|
case SORT_ASC: |
2921
|
10 |
|
default: |
2922
|
10 |
|
if ($keepKeys) { |
2923
|
4 |
|
asort($elements, $strategy); |
2924
|
4 |
|
} else { |
2925
|
6 |
|
sort($elements, $strategy); |
2926
|
|
|
} |
2927
|
10 |
|
} |
2928
|
|
|
|
2929
|
19 |
|
return $this; |
2930
|
|
|
} |
2931
|
|
|
|
2932
|
|
|
/** |
2933
|
|
|
* Split an array in the given amount of pieces. |
2934
|
|
|
* |
2935
|
|
|
* @param int $numberOfPieces |
2936
|
|
|
* @param bool $keepKeys |
2937
|
|
|
* |
2938
|
|
|
* @return static <p>(Immutable)</p> |
2939
|
|
|
*/ |
2940
|
1 |
|
public function split($numberOfPieces = 2, $keepKeys = false) |
2941
|
|
|
{ |
2942
|
1 |
|
$arrayCount = $this->count(); |
2943
|
|
|
|
2944
|
1 |
|
if ($arrayCount === 0) { |
2945
|
1 |
|
$result = array(); |
2946
|
1 |
|
} else { |
2947
|
1 |
|
$numberOfPieces = (int)$numberOfPieces; |
2948
|
1 |
|
$splitSize = (int)ceil($arrayCount / $numberOfPieces); |
2949
|
1 |
|
$result = \array_chunk($this->array, $splitSize, $keepKeys); |
2950
|
|
|
} |
2951
|
|
|
|
2952
|
1 |
|
return static::create($result); |
2953
|
|
|
} |
2954
|
|
|
|
2955
|
|
|
/** |
2956
|
|
|
* Stripe all empty items. |
2957
|
|
|
* |
2958
|
|
|
* @return static <p>(Immutable)</p> |
2959
|
|
|
*/ |
2960
|
1 |
|
public function stripEmpty() |
2961
|
|
|
{ |
2962
|
1 |
|
return $this->filter( |
2963
|
|
|
function ($item) { |
2964
|
1 |
|
if ($item === null) { |
2965
|
1 |
|
return false; |
2966
|
|
|
} |
2967
|
|
|
|
2968
|
1 |
|
return (bool)trim((string)$item); |
2969
|
|
|
} |
2970
|
1 |
|
); |
2971
|
|
|
} |
2972
|
|
|
|
2973
|
|
|
/** |
2974
|
|
|
* Swap two values between positions by key. |
2975
|
|
|
* |
2976
|
|
|
* @param string|int $swapA <p>a key in the array</p> |
2977
|
|
|
* @param string|int $swapB <p>a key in the array</p> |
2978
|
|
|
* |
2979
|
|
|
* @return static <p>(Immutable)</p> |
2980
|
|
|
*/ |
2981
|
1 |
|
public function swap($swapA, $swapB) |
2982
|
|
|
{ |
2983
|
1 |
|
$array = $this->array; |
2984
|
|
|
|
2985
|
1 |
|
list($array[$swapA], $array[$swapB]) = array($array[$swapB], $array[$swapA]); |
2986
|
|
|
|
2987
|
1 |
|
return static::create($array); |
2988
|
|
|
} |
2989
|
|
|
|
2990
|
|
|
/** |
2991
|
|
|
* alias: for "Arrayy->getArray()" |
2992
|
|
|
* |
2993
|
|
|
* @see Arrayy::getArray() |
2994
|
|
|
*/ |
2995
|
142 |
|
public function toArray() |
2996
|
|
|
{ |
2997
|
142 |
|
return $this->getArray(); |
2998
|
|
|
} |
2999
|
|
|
|
3000
|
|
|
/** |
3001
|
|
|
* Convert the current array to JSON. |
3002
|
|
|
* |
3003
|
|
|
* @param null|int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p> |
3004
|
|
|
* @param int $depth [optional] <p>Set the maximum depth. Must be greater than zero.</p> |
3005
|
|
|
* |
3006
|
|
|
* @return string |
3007
|
|
|
*/ |
3008
|
6 |
|
public function toJson($options = null, $depth = 512) |
3009
|
|
|
{ |
3010
|
6 |
|
return UTF8::json_encode($this->array, $options, $depth); |
3011
|
|
|
} |
3012
|
|
|
|
3013
|
|
|
/** |
3014
|
|
|
* Implodes array to a string with specified separator. |
3015
|
|
|
* |
3016
|
|
|
* @param string $separator [optional] <p>The element's separator.</p> |
3017
|
|
|
* |
3018
|
|
|
* @return string <p>The string representation of array, separated by ",".</p> |
3019
|
|
|
*/ |
3020
|
19 |
|
public function toString($separator = ',') |
3021
|
|
|
{ |
3022
|
19 |
|
return $this->implode($separator); |
3023
|
|
|
} |
3024
|
|
|
|
3025
|
|
|
/** |
3026
|
|
|
* Return a duplicate free copy of the current array. |
3027
|
|
|
* |
3028
|
|
|
* @return static <p>(Mutable)</p> |
3029
|
|
|
*/ |
3030
|
9 |
|
public function unique() |
3031
|
|
|
{ |
3032
|
9 |
|
$this->array = \array_reduce( |
|
|
|
|
3033
|
9 |
|
$this->array, |
3034
|
|
|
function ($resultArray, $value) { |
3035
|
8 |
|
if (!in_array($value, $resultArray, true)) { |
3036
|
8 |
|
$resultArray[] = $value; |
3037
|
8 |
|
} |
3038
|
|
|
|
3039
|
8 |
|
return $resultArray; |
3040
|
9 |
|
}, |
3041
|
9 |
|
array() |
3042
|
9 |
|
); |
3043
|
|
|
|
3044
|
9 |
View Code Duplication |
if ($this->array === null) { |
|
|
|
|
3045
|
|
|
$this->array = array(); |
3046
|
|
|
} else { |
3047
|
9 |
|
$this->array = (array)$this->array; |
3048
|
|
|
} |
3049
|
|
|
|
3050
|
9 |
|
return $this; |
3051
|
|
|
} |
3052
|
|
|
|
3053
|
|
|
/** |
3054
|
|
|
* Return a duplicate free copy of the current array. (with the old keys) |
3055
|
|
|
* |
3056
|
|
|
* @return static <p>(Mutable)</p> |
3057
|
|
|
*/ |
3058
|
9 |
|
public function uniqueKeepIndex() |
3059
|
|
|
{ |
3060
|
|
|
// init |
3061
|
9 |
|
$array = $this->array; |
3062
|
|
|
|
3063
|
9 |
|
$this->array = \array_reduce( |
|
|
|
|
3064
|
9 |
|
\array_keys($array), |
3065
|
9 |
|
function ($resultArray, $key) use ($array) { |
3066
|
8 |
|
if (!in_array($array[$key], $resultArray, true)) { |
3067
|
8 |
|
$resultArray[$key] = $array[$key]; |
3068
|
8 |
|
} |
3069
|
|
|
|
3070
|
8 |
|
return $resultArray; |
3071
|
9 |
|
}, |
3072
|
9 |
|
array() |
3073
|
9 |
|
); |
3074
|
|
|
|
3075
|
9 |
View Code Duplication |
if ($this->array === null) { |
|
|
|
|
3076
|
|
|
$this->array = array(); |
3077
|
|
|
} else { |
3078
|
9 |
|
$this->array = (array)$this->array; |
3079
|
|
|
} |
3080
|
|
|
|
3081
|
9 |
|
return $this; |
3082
|
|
|
} |
3083
|
|
|
|
3084
|
|
|
/** |
3085
|
|
|
* alias: for "Arrayy->unique()" |
3086
|
|
|
* |
3087
|
|
|
* @see Arrayy::unique() |
3088
|
|
|
* |
3089
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
3090
|
|
|
*/ |
3091
|
9 |
|
public function uniqueNewIndex() |
3092
|
|
|
{ |
3093
|
9 |
|
return $this->unique(); |
3094
|
|
|
} |
3095
|
|
|
|
3096
|
|
|
/** |
3097
|
|
|
* Prepends one or more values to the beginning of array at once. |
3098
|
|
|
* |
3099
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p> |
3100
|
|
|
*/ |
3101
|
4 |
View Code Duplication |
public function unshift(/* variadic arguments allowed */) |
|
|
|
|
3102
|
|
|
{ |
3103
|
4 |
|
if (func_num_args()) { |
3104
|
4 |
|
$args = \array_merge(array(&$this->array), func_get_args()); |
3105
|
4 |
|
call_user_func_array('array_unshift', $args); |
3106
|
4 |
|
} |
3107
|
|
|
|
3108
|
4 |
|
return $this; |
3109
|
|
|
} |
3110
|
|
|
|
3111
|
|
|
/** |
3112
|
|
|
* Get all values from a array. |
3113
|
|
|
* |
3114
|
|
|
* @return static <p>(Immutable)</p> |
3115
|
|
|
*/ |
3116
|
2 |
|
public function values() |
3117
|
|
|
{ |
3118
|
2 |
|
return static::create(\array_values((array)$this->array)); |
3119
|
|
|
} |
3120
|
|
|
|
3121
|
|
|
/** |
3122
|
|
|
* Apply the given function to every element in the array, discarding the results. |
3123
|
|
|
* |
3124
|
|
|
* @param callable $callable |
3125
|
|
|
* @param bool $recursive <p>Whether array will be walked recursively or no</p> |
3126
|
|
|
* |
3127
|
|
|
* @return static <p>(Mutable) Return this Arrayy object, with modified elements.</p> |
3128
|
|
|
*/ |
3129
|
9 |
|
public function walk($callable, $recursive = false) |
3130
|
|
|
{ |
3131
|
9 |
|
if (true === $recursive) { |
3132
|
4 |
|
\array_walk_recursive($this->array, $callable); |
3133
|
4 |
|
} else { |
3134
|
5 |
|
\array_walk($this->array, $callable); |
3135
|
|
|
} |
3136
|
|
|
|
3137
|
9 |
|
return $this; |
3138
|
|
|
} |
3139
|
|
|
} |
3140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.