Completed
Push — master ( 89cece...f05f62 )
by Lars
05:49
created

Arrayy::rsort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 765
  public function __construct($array = array(), $iteratorClass = '\\Arrayy\\ArrayyIterator')
40
  {
41 765
    $array = $this->fallbackForArray($array);
42 763
    $this->array = $array;
43
44 763
    $this->setIteratorClass($iteratorClass);
45 763
  }
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 1
  public function &__get($key)
55
  {
56 1
    $return = $this->get($key);
57
58 1
    if (is_array($return)) {
59
      return static::create($return);
60
    }
61
62 1
    return $return;
63
  }
64
65
  /**
66
   * Call object as function.
67
   *
68
   * @param mixed $key
69
   *
70
   * @return mixed
71
   */
72
  public function __invoke($key = null)
73
  {
74
    if ($key !== null) {
75
      if (isset($this->array[$key])) {
76
        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
  public function asort($sort_flags = null)
168
  {
169
    asort($this->array, $sort_flags);
170
171
    return $this;
172
  }
173
174
  /**
175 93
   * Count the values from the current array.
176
   *
177 93
   * alias: for "Arrayy->size()"
178
   *
179
   * @see Arrayy::size()
180
   *
181
   * @return int
182
   */
183
  public function count()
184
  {
185
    return $this->size();
186
  }
187 1
188
  /**
189 1
   * Exchange the array for another one.
190
   *
191 1
   * @param array|Arrayy $data
192
   *
193
   * @return array
194
   */
195
  public function exchangeArray($data)
196
  {
197
    $this->array = $this->fallbackForArray($data);
198
199 1
    return $this->array;
200
  }
201 1
202
  /**
203
   * Creates a copy of the ArrayyObject.
204
   *
205
   * @return array
206
   */
207
  public function getArrayCopy()
208
  {
209 20
    return $this->array;
210
  }
211 20
212
  /**
213 20
   * 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
  public function getIterator()
218
  {
219
    $iterator = $this->getIteratorClass();
220
221 20
    return new $iterator($this->array);
222
  }
223 20
224
  /**
225
   * Gets the iterator classname for the ArrayObject.
226
   *
227
   * @return string
228
   */
229
  public function getIteratorClass()
230
  {
231
    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
  public function ksort($sort_flags = null)
246
  {
247
    ksort($this->array, $sort_flags);
248
249
    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 39
264
  /**
265 39
   * Sort entries using a "natural order" algorithm
266 4
   *
267
   * @return static <p>(Mutable) Return this Arrayy object.</p>
268
   */
269
  public function natsort()
270 35
  {
271 1
    natsort($this->array);
272 1
273
    return $this;
274 35
  }
275
276
  /**
277
   * Whether or not an offset exists.
278 35
   *
279
   * @param int|float|string $offset
280
   *
281 12
   * @return bool
282 12
   */
283 12
  public function offsetExists($offset)
284 35
  {
285 33
    if ($this->isEmpty()) {
286
      return false;
287
    }
288 3
289
    // php cast "bool"-index into "int"-index
290 3
    if ((bool)$offset === $offset) {
291
      $offset = (int)$offset;
292 3
    }
293 3
294 3
    $tmpReturn = \array_key_exists($offset, $this->array);
295 3
296
    if (
297 3
        $tmpReturn === true
298 3
        ||
299
        (
300 3
            $tmpReturn === false
301 3
            &&
302 3
            strpos((string)$offset, $this->pathSeparator) === false
303 3
        )
304
    ) {
305 3
      return $tmpReturn;
306
    }
307
308
    $offsetExists = false;
309
310
    if (strpos((string)$offset, $this->pathSeparator) !== false) {
311
312
      $offsetExists = false;
313
      $explodedPath = explode($this->pathSeparator, (string)$offset);
314
      $lastOffset = \array_pop($explodedPath);
315 25
      $containerPath = implode($this->pathSeparator, $explodedPath);
316
317 25
      $this->callAtPath(
318
          $containerPath,
319 25
          function ($container) use ($lastOffset, &$offsetExists) {
320
            $offsetExists = \array_key_exists($lastOffset, $container);
321
          }
322
      );
323
    }
324
325
    return $offsetExists;
326
  }
327
328 17
  /**
329
   * Returns the value at specified offset.
330 17
   *
331 4
   * @param mixed $offset
332 4
   *
333 13
   * @return mixed <p>Will return null if the offset did not exists.</p>
334
   */
335 17
  public function &offsetGet($offset)
336
  {
337
    $return = $this->offsetExists($offset) ? $this->get($offset) : null;
338
339
    return $return;
340
  }
341
342 7
  /**
343
   * Assigns a value to the specified offset.
344 7
   *
345 1
   * @param mixed $offset
346
   * @param mixed $value
347
   */
348 6
  public function offsetSet($offset, $value)
349 4
  {
350
    if ($offset === null) {
351 4
      $this->array[] = $value;
352
    } else {
353
      $this->internalSet($offset, $value);
354 3
    }
355
  }
356 2
357 2
  /**
358
   * Unset an offset.
359 2
   *
360 2
   * @param mixed $offset
361
   */
362 2
  public function offsetUnset($offset)
363 2
  {
364 2
    if ($this->isEmpty()) {
365
      return;
366 2
    }
367 3
368
    if (\array_key_exists($offset, $this->array)) {
369
      unset($this->array[$offset]);
370
371
      return;
372
    }
373
374 1
    if (strpos((string)$offset, $this->pathSeparator) !== false) {
375
376 1
      $path = explode($this->pathSeparator, (string)$offset);
377
      $pathToUnset = \array_pop($path);
378
379
      $this->callAtPath(
380
          implode($this->pathSeparator, $path),
381
          function (&$offset) use ($pathToUnset) {
382
            unset($offset[$pathToUnset]);
383
          }
384
      );
385
386
    }
387
  }
388 763
389
  /**
390 763
   * Serialize the current "Arrayy"-object.
391 763
   *
392
   * @return string
393 763
   */
394
  public function serialize()
395
  {
396
    return parent::serialize();
397
  }
398
399
  /**
400
   * Sets the iterator classname for the current "Arrayy"-object.
401
   *
402
   * @param  string $class
403
   *
404
   * @return void
405
   *
406
   * @throws \InvalidArgumentException
407
   */
408
  public function setIteratorClass($class)
409
  {
410
    if (class_exists($class)) {
411
      $this->iteratorClass = $class;
412
413
      return;
414
    }
415
416
    if (strpos($class, '\\') === 0) {
417
      $class = '\\' . $class;
418
      if (class_exists($class)) {
419
        $this->iteratorClass = $class;
420
421
        return;
422
      }
423
    }
424
425
    throw new \InvalidArgumentException('The iterator class does not exist: ' . $class);
426
  }
427
428
  /**
429 1
   * Sort the entries with a user-defined comparison function and maintain key association.
430
   *
431 1
   * @param callable $function
432 1
   *
433 1
   * @return static <p>(Mutable) Return this Arrayy object.</p>
434 1
   *
435
   * @throws \InvalidArgumentException
436
   */
437 View Code Duplication
  public function uasort($function)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
438
  {
439
    if (!is_callable($function)) {
440
      throw new \InvalidArgumentException(
441
          'Passed function must be callable'
442
      );
443 1
    }
444
445 1
    uasort($this->array, $function);
446
447 1
    return $this;
448
  }
449
450
  /**
451
   * Sort the entries by keys using a user-defined comparison function.
452
   *
453
   * @param callable $function
454
   *
455
   * @return static <p>(Mutable) Return this Arrayy object.</p>
456
   *
457 2
   * @throws \InvalidArgumentException
458
   */
459 2
  public function uksort($function)
460
  {
461 2
    return $this->customSortKeys($function);
462 2
  }
463 2
464
  /**
465 2
   * Unserialize an string and return this object.
466
   *
467
   * @param string $string
468
   *
469
   * @return static <p>(Mutable)</p>
470
   */
471
  public function unserialize($string)
472
  {
473
    parent::unserialize($string);
474
475 10
    return $this;
476
  }
477 10
478
  /**
479 10
   * Sort an array in reverse order and maintain index association.
480 2
   *
481
   * @return static <p>(Mutable) Return this Arrayy object.</p>
482
   */
483 8
  public function arsort()
484 3
  {
485 3
    arsort($this->array);
486
487 8
    return $this;
488
  }
489
490
  /**
491
   * Iterate over the current array and execute a callback for each loop.
492
   *
493
   * @param \Closure $closure
494
   *
495 4
   * @return static <p>(Immutable)</p>
496
   */
497 4 View Code Duplication
  public function at(\Closure $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
498 4
  {
499 4
    $array = $this->array;
500
501 4
    foreach ($array as $key => $value) {
502 4
      $closure($value, $key);
503
    }
504 4
505
    return static::create($array);
506
  }
507
508 4
  /**
509 1
   * Returns the average value of the current array.
510 1
   *
511 1
   * @param int $decimals <p>The number of decimal-numbers to return.</p>
512 1
   *
513 1
   * @return int|double <p>The average value.</p>
514 1
   */
515 4
  public function average($decimals = 0)
516
  {
517 4
    $count = $this->count();
518
519
    if (!$count) {
520
      return 0;
521
    }
522
523
    if (!is_int($decimals)) {
524
      $decimals = 0;
525
    }
526
527 1
    return round(\array_sum($this->array) / $count, $decimals);
528
  }
529 1
530
  /**
531
   * @param mixed      $path
532
   * @param callable   $callable
533
   * @param null|array $currentOffset
534
   */
535
  protected function callAtPath($path, $callable, &$currentOffset = null)
536
  {
537
    if ($currentOffset === null) {
538
      $currentOffset = &$this->array;
539
    }
540
541 1
    $explodedPath = explode($this->pathSeparator, $path);
542
    $nextPath = \array_shift($explodedPath);
543 1
544
    if (!isset($currentOffset[$nextPath])) {
545 1
      return;
546
    }
547
548
    if (!empty($explodedPath)) {
549
      $this->callAtPath(
550
          implode($this->pathSeparator, $explodedPath),
551
          $callable,
552
          $currentOffset[$nextPath]
553
      );
554
    } else {
555
      $callable($currentOffset[$nextPath]);
556 4
    }
557
  }
558 4
559
  /**
560 4
   * Changes all keys in an array.
561
   *
562
   * @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br />
563
   *                  or <strong>CASE_LOWER</strong> (default)</p>
564
   *
565
   * @return static <p>(Immutable)</p>
566
   */
567
  public function changeKeyCase($case = CASE_LOWER)
568 8
  {
569
    return static::create(UTF8::array_change_key_case($this->array, $case));
0 ignored issues
show
Bug introduced by
The method array_change_key_case() does not seem to exist on object<voku\helper\UTF8>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
570 8
  }
571
572 7
  /**
573
   * Change the path separator of the array wrapper.
574 8
   *
575
   * By default, the separator is: "."
576
   *
577
   * @param string $separator <p>Separator to set.</p>
578
   *
579
   * @return static <p>Mutable</p>
580
   */
581
  public function changeSeparator($separator)
582 4
  {
583
    $this->pathSeparator = $separator;
584 4
585
    return $this;
586 4
  }
587
588
  /**
589
   * Create a chunked version of the current array.
590
   *
591
   * @param int  $size         <p>Size of each chunk.</p>
592
   * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p>
593
   *
594
   * @return static <p>(Immutable) A new array of chunks from the original array.</p>
595
   */
596 13
  public function chunk($size, $preserveKeys = false)
597
  {
598 13
    $result = \array_chunk($this->array, $size, $preserveKeys);
599
600
    return static::create($result);
601
  }
602
603
  /**
604
   * Clean all falsy values from the current array.
605
   *
606
   * @return static <p>(Immutable)</p>
607
   */
608 13
  public function clean()
609
  {
610 13
    return $this->filter(
611 13
        function ($value) {
612 13
          return (bool)$value;
613
        }
614 13
    );
615 13
  }
616 13
617 13
  /**
618 13
   * WARNING!!! -> Clear the current array.
619
   *
620 13
   * @return static <p>(Mutable) Return this Arrayy object, with an empty array.</p>
621
   */
622
  public function clear()
623
  {
624
    $this->array = array();
625
626
    return $this;
627
  }
628
629
  /**
630 4
   * Check if an item is in the current array.
631
   *
632 4
   * @param string|int|float $value
633
   *
634
   * @return bool
635
   */
636
  public function contains($value)
637
  {
638
    return in_array($value, $this->array, true);
639
  }
640
641
  /**
642 1
   * Check if an (case-insensitive) string is in the current array.
643
   *
644 1
   * @param string $value
645
   *
646
   * @return bool
647
   */
648
  public function containsCaseInsensitive($value)
649
  {
650
    return in_array(
651
        UTF8::strtolower($value),
652
        \array_map(
653
            array(
654
                new UTF8(),
655
                'strtolower',
656 9
            ),
657
            $this->array
658 9
        ),
659
        true
660
    );
661
  }
662
663
  /**
664
   * Check if the given key/index exists in the array.
665
   *
666
   * @param string|int|float $key <p>key/index to search for</p>
667
   *
668 1
   * @return bool <p>Returns true if the given key/index exists in the array, false otherwise.</p>
669
   */
670 1
  public function containsKey($key)
671
  {
672
    return $this->offsetExists($key);
673
  }
674
675
  /**
676
   * Check if all given needles are present in the array as key/index.
677
   *
678
   * @param array $needles
679
   *
680 479
   * @return bool <p>Returns true if the given keys/indexes exists in the array, false otherwise.</p>
681
   */
682 479
  public function containsKeys(array $needles)
683
  {
684
    return count(\array_intersect($needles, $this->keys()->getArray())) === count($needles);
685
  }
686
687
  /**
688
   * alias: for "Arrayy->contains()"
689
   *
690
   * @see Arrayy::contains()
691
   *
692 1
   * @param string|int|float $value
693
   *
694 1
   * @return bool
695
   */
696 1
  public function containsValue($value)
697
  {
698 1
    return $this->contains($value);
699
  }
700
701
  /**
702
   * Check if all given needles are present in the array.
703
   *
704
   * @param array $needles
705
   *
706
   * @return bool <p>Returns true if the given values exists in the array, false otherwise.</p>
707
   */
708 5
  public function containsValues(array $needles)
709
  {
710 5
    return count(\array_intersect($needles, $this->array)) === count($needles);
711
  }
712 5
713
  /**
714
   * Creates an Arrayy object.
715
   *
716
   * @param array $array
717
   *
718
   * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p>
719
   */
720
  public static function create($array = array())
721
  {
722 4
    return new static($array);
723
  }
724 4
725 4
  /**
726
   * WARNING: Creates an Arrayy object by reference.
727 3
   *
728 4
   * @param array $array
729
   *
730 4
   * @return static <p>(Mutable) Return this Arrayy object.</p>
731
   */
732
  public function createByReference(&$array = array())
733
  {
734
    $array = $this->fallbackForArray($array);
735
736
    $this->array = &$array;
737
738
    return $this;
739
  }
740
741
  /**
742
   * Create an new Arrayy object via JSON.
743 8
   *
744
   * @param string $json
745 8
   *
746 1
   * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p>
747
   */
748 1
  public static function createFromJson($json)
749 1
  {
750 1
    $array = UTF8::json_decode($json, true);
751
752 1
    return static::create($array);
753 7
  }
754
755
  /**
756
   * Create an new instance filled with values from an object that have implemented ArrayAccess.
757 8
   *
758
   * @param \ArrayAccess $object <p>Object that implements ArrayAccess</p>
759
   *
760
   * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p>
761 8
   */
762 8
  public static function createFromObject(\ArrayAccess $object)
763 8
  {
764 8
    $array = new static();
765 8
    foreach ($object as $key => $value) {
766
      /** @noinspection OffsetOperationsInspection */
767 8
      $array[$key] = $value;
768
    }
769
770
    return $array;
771
  }
772
773
  /**
774
   * Create an new Arrayy object via string.
775
   *
776
   * @param string      $str       <p>The input string.</p>
777
   * @param string|null $delimiter <p>The boundary string.</p>
778
   * @param string|null $regEx     <p>Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be
779 1
   *                               used.</p>
780
   *
781 1
   * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p>
782
   */
783
  public static function createFromString($str, $delimiter, $regEx = null)
784
  {
785
    if ($regEx) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $regEx of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
786
      preg_match_all($regEx, $str, $array);
787
788
      if (!empty($array)) {
789
        $array = $array[0];
790
      }
791
792
    } else {
793 5
      $array = explode($delimiter, $str);
794
    }
795 5
796
    // trim all string in the array
797 5
    \array_walk(
798
        $array,
799
        function (&$val) {
800
          /** @noinspection ReferenceMismatchInspection */
801
          if (is_string($val)) {
802
            $val = trim($val);
803
          }
804
        }
805
    );
806
807
    return static::create($array);
808
  }
809 4
810
  /**
811 4
   * Create an new instance containing a range of elements.
812
   *
813 4
   * @param mixed $low  <p>First value of the sequence.</p>
814
   * @param mixed $high <p>The sequence is ended upon reaching the end value.</p>
815
   * @param int   $step <p>Used as the increment between elements in the sequence.</p>
816
   *
817
   * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p>
818
   */
819
  public static function createWithRange($low, $high, $step = 1)
820
  {
821
    return static::create(range($low, $high, $step));
822
  }
823 12
824
  /**
825 12
   * Custom sort by index via "uksort".
826
   *
827 12
   * @link http://php.net/manual/en/function.uksort.php
828
   *
829
   * @param callable $function
830
   *
831
   * @return static <p>(Mutable) Return this Arrayy object.</p>
832
   *
833
   * @throws \InvalidArgumentException
834
   */
835 View Code Duplication
  public function customSortKeys($function)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
836
  {
837
    if (!is_callable($function)) {
838 1
      throw new \InvalidArgumentException(
839
          'Passed function must be callable'
840 1
      );
841
    }
842
843
    uksort($this->array, $function);
844 1
845 1
    return $this;
846 1
  }
847 1
848 1
  /**
849 1
   * Custom sort by value via "usort".
850
   *
851
   * @link http://php.net/manual/en/function.usort.php
852 1
   *
853 1
   * @param callable $function
854 1
   *
855 1
   * @return static <p>(Mutable) Return this Arrayy object.</p>
856 1
   *
857 1
   * @throws \InvalidArgumentException
858 1
   */
859 1 View Code Duplication
  public function customSortValues($function)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
860 1
  {
861 1
    if (!is_callable($function)) {
862 1
      throw new \InvalidArgumentException(
863
          'Passed function must be callable'
864 1
      );
865 1
    }
866
867 1
    usort($this->array, $function);
868
869 1
    return $this;
870
  }
871
872
  /**
873
   * Return values that are only in the current array.
874
   *
875
   * @param array $array
876
   *
877
   * @return static <p>(Immutable)</p>
878
   */
879 8
  public function diff(array $array = array())
880
  {
881 8
    $result = \array_diff($this->array, $array);
882
883 8
    return static::create($result);
884
  }
885
886
  /**
887
   * Return values that are only in the current multi-dimensional array.
888
   *
889
   * @param array      $array
890
   * @param null|array $helperVariableForRecursion <p>(only for internal usage)</p>
891 1
   *
892
   * @return static <p>(Immutable)</p>
893 1
   */
894
  public function diffRecursive(array $array = array(), $helperVariableForRecursion = null)
895 1
  {
896 1
    $result = array();
897
898 1
    if (
899
        $helperVariableForRecursion !== null
900
        &&
901
        is_array($helperVariableForRecursion)
902
    ) {
903
      $arrayForTheLoop = $helperVariableForRecursion;
904
    } else {
905
      $arrayForTheLoop = $this->array;
906
    }
907
908 4
    foreach ($arrayForTheLoop as $key => $value) {
909
      if (\array_key_exists($key, $array)) {
910 4
        if (is_array($value)) {
911
          $recursiveDiff = $this->diffRecursive($array[$key], $value);
912 4
          if (!empty($recursiveDiff)) {
913 4
            $result[$key] = $recursiveDiff;
914 4
          }
915
        } else {
916 4
          if ($value != $array[$key]) {
917
            $result[$key] = $value;
918
          }
919
        }
920
      } else {
921
        $result[$key] = $value;
922
      }
923
    }
924
925
    return static::create($result);
926 4
  }
927
928 4
  /**
929 4
   * Return values that are only in the new $array.
930 3
   *
931 1
   * @param array $array
932 1
   *
933
   * @return static <p>(Immutable)</p>
934 4
   */
935
  public function diffReverse(array $array = array())
936 4
  {
937
    $result = \array_diff($array, $this->array);
938
939
    return static::create($result);
940
  }
941
942
  /**
943
   * Divide an array into two arrays. One with keys and the other with values.
944
   *
945
   * @return static <p>(Immutable)</p>
946
   */
947
  public function divide()
948
  {
949
    return static::create(
950
        array(
951
            $this->keys(),
952
            $this->values(),
953
        )
954
    );
955
  }
956 765
957
  /**
958 765
   * Iterate over the current array and modify the array's value.
959 762
   *
960
   * @param \Closure $closure
961
   *
962 11
   * @return static <p>(Immutable)</p>
963 1
   */
964 View Code Duplication
  public function each(\Closure $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
965
  {
966 10
    $array = $this->array;
967 6
968
    foreach ($array as $key => $value) {
969
      $array[$key] = $closure($value, $key);
970 9
    }
971
972 9
    return static::create($array);
973
  }
974
975
  /**
976
   * Check if a value is in the current array using a closure.
977 9
   *
978
   * @param \Closure $closure
979
   *
980
   * @return bool <p>Returns true if the given value is found, false otherwise.</p>
981 9
   */
982
  public function exists(\Closure $closure)
983
  {
984
    $isExists = false;
985
    foreach ($this->array as $key => $value) {
986
      if ($closure($value, $key)) {
987 9
        $isExists = true;
988
        break;
989 2
      }
990 9
    }
991 7
992
    return $isExists;
993
  }
994 2
995
  /**
996 2
   * create a fallback for array
997
   *
998
   * 1. use the current array, if it's a array
999
   * 2. call "getArray()" on object, if there is a "Arrayy"-object
1000
   * 3. fallback to empty array, if there is nothing
1001
   * 4. call "createFromObject()" on object, if there is a "\ArrayAccess"-object
1002
   * 5. call "__toArray()" on object, if the method exists
1003
   * 6. cast a string or object with "__toString()" into an array
1004
   * 7. throw a "InvalidArgumentException"-Exception
1005
   *
1006 9
   * @param $array
1007
   *
1008 9
   * @return array
1009 1
   *
1010
   * @throws \InvalidArgumentException
1011
   */
1012 9
  protected function fallbackForArray(&$array)
1013
  {
1014 9
    if (is_array($array)) {
1015
      return $array;
1016
    }
1017
1018
    if ($array instanceof self) {
1019
      return $array->getArray();
1020
    }
1021
1022
    if (!$array) {
1023
      return array();
1024
    }
1025
1026
    $isObject = is_object($array);
1027
1028
    if ($isObject && $array instanceof \ArrayAccess) {
1029
      /** @noinspection ReferenceMismatchInspection */
1030
      return static::createFromObject($array)->getArray();
1031
    }
1032
1033
    if ($isObject && $array instanceof \ArrayObject) {
1034
      return $array->getArrayCopy();
1035
    }
1036
1037
    if ($isObject && method_exists($array, '__toArray')) {
1038
      return (array)$array->__toArray();
1039 1
    }
1040
1041 1
    /** @noinspection ReferenceMismatchInspection */
1042 1
    if (
1043 1
        is_string($array)
1044
        ||
1045
        ($isObject && method_exists($array, '__toString'))
1046
    ) {
1047 1
      return array((string)$array);
1048 1
    }
1049
1050
    throw new \InvalidArgumentException(
1051 1
        'Passed value should be a array'
1052
    );
1053
  }
1054 1
1055
  /**
1056
   * Find all items in an array that pass the truth test.
1057 1
   *
1058
   * @param \Closure|null $closure
1059 1
   *
1060 1
   * @return static <p>(Immutable)</p>
1061
   */
1062
  public function filter($closure = null)
1063 1
  {
1064
    if (!$closure) {
1065
      return $this->clean();
1066 1
    }
1067
1068
    $array = \array_filter($this->array, $closure);
1069 1
1070
    return static::create($array);
1071 1
  }
1072 1
1073
  /**
1074
   * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property
1075 1
   * within that.
1076
   *
1077
   * @param string $property
1078 1
   * @param string $value
1079
   * @param string $comparisonOp
1080
   *                            <p>
1081 1
   *                            'eq' (equals),<br />
1082 1
   *                            'gt' (greater),<br />
1083
   *                            'gte' || 'ge' (greater or equals),<br />
1084 1
   *                            'lt' (less),<br />
1085 1
   *                            'lte' || 'le' (less or equals),<br />
1086 1
   *                            'ne' (not equals),<br />
1087
   *                            'contains',<br />
1088 1
   *                            'notContains',<br />
1089 1
   *                            'newer' (via strtotime),<br />
1090 1
   *                            'older' (via strtotime),<br />
1091 1
   *                            </p>
1092
   *
1093 1
   * @return static <p>(Immutable)</p>
1094 1
   */
1095 1
  public function filterBy($property, $value, $comparisonOp = null)
1096
  {
1097 1
    if (!$comparisonOp) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $comparisonOp of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1098
      $comparisonOp = is_array($value) ? 'contains' : 'eq';
1099 1
    }
1100 1
1101
    $ops = array(
1102 1
        'eq'          => function ($item, $prop, $value) {
1103
          return $item[$prop] === $value;
1104
        },
1105
        'gt'          => function ($item, $prop, $value) {
1106
          return $item[$prop] > $value;
1107
        },
1108
        'ge'          => function ($item, $prop, $value) {
1109
          return $item[$prop] >= $value;
1110
        },
1111
        'gte'         => function ($item, $prop, $value) {
1112
          return $item[$prop] >= $value;
1113 8
        },
1114
        'lt'          => function ($item, $prop, $value) {
1115 8
          return $item[$prop] < $value;
1116 6
        },
1117 5
        'le'          => function ($item, $prop, $value) {
1118
          return $item[$prop] <= $value;
1119 5
        },
1120
        'lte'         => function ($item, $prop, $value) {
1121 3
          return $item[$prop] <= $value;
1122
        },
1123
        'ne'          => function ($item, $prop, $value) {
1124
          return $item[$prop] !== $value;
1125
        },
1126
        'contains'    => function ($item, $prop, $value) {
1127
          return in_array($item[$prop], (array)$value, true);
1128
        },
1129
        'notContains' => function ($item, $prop, $value) {
1130
          return !in_array($item[$prop], (array)$value, true);
1131
        },
1132
        'newer'       => function ($item, $prop, $value) {
1133
          return strtotime($item[$prop]) > strtotime($value);
1134
        },
1135
        'older'       => function ($item, $prop, $value) {
1136
          return strtotime($item[$prop]) < strtotime($value);
1137
        },
1138
    );
1139
1140
    $result = \array_values(
1141
        \array_filter(
1142
            (array)$this->array,
1143 13
            function ($item) use (
1144
                $property,
1145 13
                $value,
1146 13
                $ops,
1147
                $comparisonOp
1148 13
            ) {
1149 3
              $item = (array)$item;
1150
              $itemArrayy = new Arrayy($item);
1151
              $item[$property] = $itemArrayy->get($property, array());
1152 10
1153
              return $ops[$comparisonOp]($item, $property, $value);
1154
            }
1155
        )
1156
    );
1157
1158
    return static::create($result);
1159
  }
1160
1161
  /**
1162 28
   * Find the first item in an array that passes the truth test,
1163
   *  otherwise return false
1164 28
   *
1165 7
   * @param \Closure $closure
1166 7
   *
1167 7
   * @return mixed|false <p>Return false if we did not find the value.</p>
1168 21
   */
1169 21
  public function find(\Closure $closure)
1170 21
  {
1171
    foreach ($this->array as $key => $value) {
1172
      if ($closure($value, $key)) {
1173 28
        return $value;
1174
      }
1175
    }
1176
1177
    return false;
1178
  }
1179
1180
  /**
1181
   * find by ...
1182
   *
1183 26
   * @param string $property
1184
   * @param string $value
1185 26
   * @param string $comparisonOp
1186 11
   *
1187 11
   * @return static <p>(Immutable)</p>
1188 15
   */
1189 15
  public function findBy($property, $value, $comparisonOp = 'eq')
1190
  {
1191
    return $this->filterBy($property, $value, $comparisonOp);
1192 26
  }
1193
1194
  /**
1195
   * Get the first value from the current array.
1196
   *
1197
   * @return mixed <p>Return null if there wasn't a element.</p>
1198
   */
1199
  public function first()
1200 1
  {
1201
    $tmpArray = $this->array;
1202 1
    $result = \array_shift($tmpArray);
1203
1204 1
    if ($result === null) {
1205
      return null;
1206
    }
1207
1208
    return $result;
1209
  }
1210
1211
  /**
1212
   * Get the first value(s) from the current array.
1213
   *
1214
   * @param int|null $number <p>How many values you will take?</p>
1215
   *
1216
   * @return static <p>(Immutable)</p>
1217 59
   */
1218
  public function firstsImmutable($number = null)
1219
  {
1220
    if ($number === null) {
1221 59
      $arrayTmp = $this->array;
1222 3
      $array = (array)\array_shift($arrayTmp);
1223 59
    } else {
1224 3
      $number = (int)$number;
1225 3
      $arrayTmp = $this->array;
1226 57
      $array = \array_splice($arrayTmp, 0, $number, true);
1227
    }
1228
1229 59
    return static::create($array);
1230 1
  }
1231
1232
  /**
1233
   * Get the first value(s) from the current array.
1234 59
   *
1235 2
   * @param int|null $number <p>How many values you will take?</p>
1236 2
   *
1237
   * @return static <p>(Mutable)</p>
1238 59
   */
1239 49
  public function firstsMutable($number = null)
1240 5
  {
1241
    if ($number === null) {
1242
      $this->array = (array)\array_shift($this->array);
1243 46
    } else {
1244
      $number = (int)$number;
1245
      $this->array = \array_splice($this->array, 0, $number, true);
1246
    }
1247 20
1248 20
    return $this;
1249 19
  }
1250
1251
  /**
1252 5
   * Exchanges all keys with their associated values in an array.
1253 5
   *
1254
   * @return static <p>(Immutable)</p>
1255 5
   */
1256
  public function flip()
1257
  {
1258
    $result = \array_flip($this->array);
1259 5
1260
    return static::create($result);
1261
  }
1262
1263
  /**
1264
   * Get a value from an array (optional using dot-notation).
1265
   *
1266
   * @param string $key      <p>The key to look for.</p>
1267 489
   * @param mixed  $fallback <p>Value to fallback to.</p>
1268
   * @param array  $array    <p>The array to get from, if it's set to "null" we use the current array from the
1269 489
   *                         class.</p>
1270
   *
1271 489
   * @return mixed
1272
   */
1273
  public function get($key, $fallback = null, $array = null)
1274
  {
1275
    if (
1276
        $array !== null
1277
        &&
1278
        is_array($array)
1279
    ) {
1280
      $usedArray = $array;
1281
    } else {
1282
      $usedArray = $this->array;
1283
    }
1284
1285
    if ($key === null) {
1286 1
      return static::create($usedArray);
1287
    }
1288 1
1289
    // php cast "bool"-index into "int"-index
1290 1
    if ((bool)$key === $key) {
1291
      $key = (int)$key;
1292
    }
1293
1294
    if (\array_key_exists($key, $usedArray) === true) {
1295
      if (is_array($usedArray[$key])) {
1296
        return static::create($usedArray[$key]);
1297
      }
1298
1299
      return $usedArray[$key];
1300 38
    }
1301
1302 38
    // Crawl through array, get key according to object or not
1303 10
    foreach (explode($this->pathSeparator, (string)$key) as $segment) {
1304
      if (!isset($usedArray[$segment])) {
1305 10
        return $fallback instanceof \Closure ? $fallback() : $fallback;
1306 2
      }
1307 2
1308 8
      $usedArray = $usedArray[$segment];
1309
    }
1310 10
1311
    if (is_array($usedArray)) {
1312
      return static::create($usedArray);
1313
    }
1314 38
1315
    return $usedArray;
1316 38
  }
1317
1318
  /**
1319
   * Get the current array from the "Arrayy"-object.
1320 38
   *
1321
   * @return array
1322
   */
1323
  public function getArray()
1324
  {
1325
    \array_map(array('self', 'internalGetArray'), $this->array);
1326
1327
    return $this->array;
1328
  }
1329
1330 1
  /**
1331
   * Returns the values from a single column of the input array, identified by
1332 1
   * the $columnKey, can be used to extract data-columns from multi-arrays.
1333
   *
1334
   * Info: Optionally, you may provide an $indexKey to index the values in the returned
1335
   * array by the values from the $indexKey column in the input array.
1336
   *
1337
   * @param mixed $columnKey
1338
   * @param mixed $indexKey
1339
   *
1340
   * @return static <p>(Immutable)</p>
1341
   */
1342 3
  public function getColumn($columnKey = null, $indexKey = null)
1343
  {
1344 3
    $result = \array_column($this->array, $columnKey, $indexKey);
1345
1346
    return static::create($result);
1347
  }
1348
1349
  /**
1350
   * Get correct PHP constant for direction.
1351
   *
1352
   * @param int|string $direction
1353
   *
1354 3
   * @return int
1355
   */
1356 3
  protected function getDirection($direction)
1357
  {
1358
    if (is_string($direction)) {
1359
      $direction = strtolower($direction);
1360
1361
      if ($direction === 'desc') {
1362
        $direction = SORT_DESC;
1363
      } else {
1364
        $direction = SORT_ASC;
1365
      }
1366
    }
1367
1368 9
    if (
1369
        $direction !== SORT_DESC
1370 9
        &&
1371
        $direction !== SORT_ASC
1372
    ) {
1373
      $direction = SORT_ASC;
1374
    }
1375
1376
    return $direction;
1377
  }
1378
1379
  /**
1380 3
   * alias: for "Arrayy->keys()"
1381
   *
1382 3
   * @see Arrayy::keys()
1383
   *
1384
   * @return static <p>(Immutable)</p>
1385
   */
1386
  public function getKeys()
1387
  {
1388
    return $this->keys();
1389
  }
1390
1391
  /**
1392
   * alias: for "Arrayy->randomImmutable()"
1393
   *
1394 6
   * @see Arrayy::randomImmutable()
1395
   *
1396 6
   * @return static <p>(Immutable)</p>
1397
   */
1398
  public function getRandom()
1399
  {
1400
    return $this->randomImmutable();
1401
  }
1402
1403
  /**
1404
   * alias: for "Arrayy->randomKey()"
1405
   *
1406
   * @see Arrayy::randomKey()
1407 3
   *
1408
   * @return mixed <p>Get a key/index or null if there wasn't a key/index.</p>
1409 3
   */
1410 3
  public function getRandomKey()
1411
  {
1412
    return $this->randomKey();
1413 3
  }
1414
1415 3
  /**
1416 3
   * alias: for "Arrayy->randomKeys()"
1417
   *
1418 3
   * @see Arrayy::randomKeys()
1419
   *
1420
   * @param int $number
1421
   *
1422 3
   * @return static <p>(Immutable)</p>
1423 3
   */
1424 3
  public function getRandomKeys($number)
1425
  {
1426
    return $this->randomKeys($number);
1427 3
  }
1428 2
1429 1
  /**
1430 1
   * alias: for "Arrayy->randomValue()"
1431 1
   *
1432 1
   * @see Arrayy::randomValue()
1433 1
   *
1434
   * @return mixed <p>get a random value or null if there wasn't a value.</p>
1435 2
   */
1436
  public function getRandomValue()
1437 3
  {
1438
    return $this->randomValue();
1439 3
  }
1440
1441
  /**
1442
   * alias: for "Arrayy->randomValues()"
1443
   *
1444
   * @see Arrayy::randomValues()
1445
   *
1446
   * @param int $number
1447
   *
1448
   * @return static <p>(Immutable)</p>
1449 22
   */
1450
  public function getRandomValues($number)
1451
  {
1452 22
    return $this->randomValues($number);
1453
  }
1454 22
1455
  /**
1456
   * Group values from a array according to the results of a closure.
1457
   *
1458
   * @param string $grouper <p>A callable function name.</p>
1459
   * @param bool   $saveKeys
1460
   *
1461
   * @return static <p>(Immutable)</p>
1462
   */
1463
  public function group($grouper, $saveKeys = false)
1464 27
  {
1465
    $array = (array)$this->array;
1466 27
    $result = array();
1467
1468
    // Iterate over values, group by property/results from closure
1469
    foreach ($array as $key => $value) {
1470
1471
      $groupKey = is_callable($grouper) ? $grouper($value, $key) : $this->get($grouper, null, $value);
1472
      $newValue = $this->get($groupKey, null, $result);
1473
1474
      if ($groupKey instanceof self) {
1475
        $groupKey = $groupKey->getArray();
1476
      }
1477
1478
      if ($newValue instanceof self) {
1479
        $newValue = $newValue->getArray();
1480 3
      }
1481
1482 3
      // Add to results
1483
      if ($groupKey !== null) {
1484 3
        if ($saveKeys) {
1485 3
          $result[$groupKey] = $newValue;
1486 2
          $result[$groupKey][$key] = $value;
1487 2
        } else {
1488 3
          $result[$groupKey] = $newValue;
1489
          $result[$groupKey][] = $value;
1490 3
        }
1491
      }
1492
1493
    }
1494
1495
    return static::create($result);
1496
  }
1497
1498
  /**
1499
   * Check if an array has a given key.
1500
   *
1501
   * @param mixed $key
1502 4
   *
1503
   * @return bool
1504 4
   */
1505
  public function has($key)
1506
  {
1507
    // Generate unique string to use as marker.
1508
    $unFound = (string)uniqid('arrayy', true);
1509
1510
    return $this->get($key, $unFound) !== $unFound;
1511
  }
1512
1513
  /**
1514 12
   * Implodes an array.
1515
   *
1516 12
   * @param string $glue
1517
   *
1518 12
   * @return string
1519
   */
1520
  public function implode($glue = '')
1521
  {
1522
    return implode($glue, $this->array);
1523
  }
1524 404
1525
  /**
1526 404
   * Given a list and an iterate-function that returns
1527
   * a key for each element in the list (or a property name),
1528 404
   * returns an object with an index of each item.
1529
   *
1530
   * Just like groupBy, but for when you know your keys are unique.
1531 404
   *
1532
   * @param mixed $key
1533
   *
1534
   * @return static <p>(Immutable)</p>
1535
   */
1536
  public function indexBy($key)
1537
  {
1538
    $results = array();
1539
1540 18
    foreach ($this->array as $a) {
1541
      if (\array_key_exists($key, $a) === true) {
1542 18
        $results[$a[$key]] = $a;
1543
      }
1544
    }
1545 18
1546
    return static::create($results);
1547
  }
1548
1549
  /**
1550
   * alias: for "Arrayy->searchIndex()"
1551
   *
1552
   * @see Arrayy::searchIndex()
1553
   *
1554
   * @param mixed $value <p>The value to search for.</p>
1555 18
   *
1556
   * @return mixed
1557 18
   */
1558
  public function indexOf($value)
1559 18
  {
1560
    return $this->searchIndex($value);
1561
  }
1562
1563
  /**
1564
   * Get everything but the last..$to items.
1565
   *
1566
   * @param int $to
1567
   *
1568
   * @return static <p>(Immutable)</p>
1569
   */
1570 30
  public function initial($to = 1)
1571
  {
1572 30
    $slice = count($this->array) - $to;
1573
1574
    return $this->firstsImmutable($slice);
1575
  }
1576
1577 30
  /**
1578 30
   * @param mixed $value
1579
   */
1580
  protected function internalGetArray(&$value)
1581 30
  {
1582 3
    if ($value instanceof self) {
1583
      $value &= $value->getArray();
1584
    } elseif ($value instanceof \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1585
      $value &= $value->jsonSerialize();
1586
    }
1587 3
  }
1588
1589
  /**
1590
   * Internal mechanics of remove method.
1591 3
   *
1592 3
   * @param string $key
1593
   *
1594 30
   * @return boolean
1595
   */
1596 30
  protected function internalRemove($key)
1597
  {
1598
    $path = explode($this->pathSeparator, (string)$key);
1599
1600
    // Crawl though the keys
1601
    while (count($path) > 1) {
1602
      $key = \array_shift($path);
1603
1604
      if (!$this->has($key)) {
1605
        return false;
1606 2
      }
1607
1608 2
      $this->array = &$this->array[$key];
1609
    }
1610
1611
    $key = \array_shift($path);
1612
1613
    unset($this->array[$key]);
1614
1615
    return true;
1616
  }
1617
1618 1
  /**
1619
   * Internal mechanic of set method.
1620 1
   *
1621
   * @param string $key
1622
   * @param mixed  $value
1623
   *
1624
   * @return bool
1625
   */
1626
  protected function internalSet($key, $value)
1627
  {
1628
    if ($key === null) {
1629
      return false;
1630
    }
1631 1
1632
    // init
1633
    $array =& $this->array;
1634 1
    $path = explode($this->pathSeparator, (string)$key);
1635 1
1636 1
    // Crawl through the keys
1637
    while (count($path) > 1) {
1638
      $key = \array_shift($path);
1639 1
1640 1
      // If the key doesn't exist at this depth, we will just create an empty array
1641 1
      // to hold the next value, allowing us to create the arrays to hold final
1642 1
      // values at the correct depth. Then we'll keep digging into the array.
1643
      if (!isset($array[$key]) || !is_array($array[$key])) {
1644
        $array[$key] = static::create(array());
1645 1
      }
1646
1647
      $array =& $array[$key];
1648
    }
1649
1650
    $array[\array_shift($path)] = $value;
1651
1652
    return true;
1653 15
  }
1654
1655 15
  /**
1656 3
   * Return an array with all elements found in input array.
1657
   *
1658
   * @param array $search
1659 13
   *
1660 13
   * @return static <p>(Immutable)</p>
1661 11
   */
1662
  public function intersection(array $search)
1663 3
  {
1664
    return static::create(\array_values(\array_intersect($this->array, $search)));
1665 3
  }
1666
1667
  /**
1668
   * Return a boolean flag which indicates whether the two input arrays have any common elements.
1669
   *
1670
   * @param array $search
1671
   *
1672
   * @return bool
1673 87
   */
1674
  public function intersects(array $search)
1675 87
  {
1676
    return count($this->intersection($search)->array) > 0;
1677
  }
1678
1679
  /**
1680
   * Invoke a function on all of an array's values.
1681
   *
1682
   * @param mixed $callable
1683
   * @param mixed $arguments
1684
   *
1685
   * @return static <p>(Immutable)</p>
1686
   */
1687
  public function invoke($callable, $arguments = array())
1688
  {
1689
    // If one argument given for each iteration, create an array for it.
1690
    if (!is_array($arguments)) {
1691
      $arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray();
1692
    }
1693
1694
    // If the callable has arguments, pass them.
1695 14
    if ($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1696
      $array = \array_map($callable, $this->array, $arguments);
1697 14
    } else {
1698
      $array = \array_map($callable, $this->array);
1699
    }
1700
1701
    return static::create($array);
1702
  }
1703
1704
  /**
1705 5
   * Check whether array is associative or not.
1706
   *
1707 5
   * @return bool <p>Returns true if associative, false otherwise.</p>
1708 2
   */
1709
  public function isAssoc()
1710
  {
1711 4
    if ($this->isEmpty()) {
1712 4
      return false;
1713 2
    }
1714
1715 3
    foreach ($this->keys()->getArray() as $key) {
1716
      if (!is_string($key)) {
1717 2
        return false;
1718
      }
1719
    }
1720
1721
    return true;
1722
  }
1723
1724
  /**
1725 1
   * Check whether the array is empty or not.
1726
   *
1727 1
   * @return bool <p>Returns true if empty, false otherwise.</p>
1728
   */
1729
  public function isEmpty()
1730
  {
1731
    return !$this->array;
1732
  }
1733
1734
  /**
1735
   * Check if the current array is equal to the given "$array" or not.
1736
   *
1737
   * @param array $array
1738
   *
1739
   * @return bool
1740
   */
1741
  public function isEqual(array $array)
1742
  {
1743 25
    return ($this->array === $array);
1744
  }
1745 25
1746
  /**
1747
   * Check if the current array is a multi-array.
1748
   *
1749
   * @return bool
1750
   */
1751
  public function isMultiArray()
1752
  {
1753 4
    return !(count($this->array) === count($this->array, COUNT_RECURSIVE));
1754
  }
1755 4
1756
  /**
1757
   * Check whether array is numeric or not.
1758
   *
1759
   * @return bool <p>Returns true if numeric, false otherwise.</p>
1760
   */
1761
  public function isNumeric()
1762
  {
1763
    if ($this->isEmpty()) {
1764
      return false;
1765 13
    }
1766
1767 13
    foreach ($this->keys() as $key) {
1768 1
      if (!is_int($key)) {
1769
        return false;
1770
      }
1771 12
    }
1772 8
1773
    return true;
1774 8
  }
1775 1
1776 1
  /**
1777 7
   * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not.
1778
   *
1779
   * @return bool
1780 8
   */
1781 8
  public function isSequential()
1782 4
  {
1783 4
    return \array_keys($this->array) === range(0, count($this->array) - 1);
1784
  }
1785
1786 12
  /**
1787
   * @return array
1788
   */
1789
  public function jsonSerialize()
1790
  {
1791
    return $this->getArray();
1792
  }
1793
1794
  /**
1795
   * Get all keys from the current array.
1796 13
   *
1797
   * @return static <p>(Immutable)</p>
1798 13
   */
1799 1
  public function keys()
1800
  {
1801
    return static::create(\array_keys($this->array));
1802 12
  }
1803 8
1804
  /**
1805 8
   * Sort an array by key in reverse order.
1806 1
   *
1807 1
   * @param int $sort_flags [optional] <p>
1808 7
   *                        You may modify the behavior of the sort using the optional
1809
   *                        parameter sort_flags, for details
1810
   *                        see sort.
1811 8
   *                        </p>
1812 8
   *
1813 4
   * @return static <p>(Mutable) Return this Arrayy object.</p>
1814 4
   */
1815
  public function krsort($sort_flags = null)
1816
  {
1817 12
    krsort($this->array, $sort_flags);
1818
1819
    return $this;
1820
  }
1821
1822
  /**
1823
   * Get the last value from the current array.
1824
   *
1825
   * @return mixed <p>Return null if there wasn't a element.</p>
1826
   */
1827
  public function last()
1828
  {
1829 10
    return $this->pop();
1830
  }
1831 10
1832
  /**
1833
   * Get the last value(s) from the current array.
1834
   *
1835
   * @param int|null $number
1836
   *
1837
   * @return static <p>(Immutable)</p>
1838
   */
1839
  public function lastsImmutable($number = null)
1840
  {
1841
    if ($this->isEmpty()) {
1842 4
      return static::create();
1843
    }
1844 4
1845
    if ($number === null) {
1846 4
      $poppedValue = $this->pop();
1847
1848
      if ($poppedValue === null) {
1849
        $poppedValue = array($poppedValue);
1850
      } else {
1851
        $poppedValue = (array)$poppedValue;
1852
      }
1853
1854
      $arrayy = static::create($poppedValue);
1855
    } else {
1856 15
      $number = (int)$number;
1857
      $arrayy = $this->rest(-$number);
1858 15
    }
1859 2
1860
    return $arrayy;
1861
  }
1862
1863 13
  /**
1864
   * Get the last value(s) from the current array.
1865 13
   *
1866 13
   * @param int|null $number
1867
   *
1868 13
   * @return static <p>(Mutable)</p>
1869 7
   */
1870
  public function lastsMutable($number = null)
1871 9
  {
1872
    if ($this->isEmpty()) {
1873 7
      return $this;
1874
    }
1875
1876
    if ($number === null) {
1877
      $poppedValue = $this->pop();
1878
1879
      if ($poppedValue === null) {
1880
        $poppedValue = array($poppedValue);
1881
      } else {
1882
        $poppedValue = (array)$poppedValue;
1883 14
      }
1884
1885 14
      $this->array = static::create($poppedValue)->array;
1886 2
    } else {
1887
      $number = (int)$number;
1888
      $this->array = $this->rest(-$number)->array;
1889
    }
1890 12
1891
    return $this;
1892 12
  }
1893 12
1894
  /**
1895 12
   * Count the values from the current array.
1896 9
   *
1897
   * alias: for "Arrayy->size()"
1898 5
   *
1899
   * @see Arrayy::size()
1900 4
   *
1901
   * @return int
1902
   */
1903
  public function length()
1904
  {
1905
    return $this->size();
1906
  }
1907
1908 10
  /**
1909
   * Apply the given function to the every element of the array,
1910 10
   * collecting the results.
1911 1
   *
1912
   * @param callable $callable
1913
   *
1914 9
   * @return static <p>(Immutable) Arrayy object with modified elements.</p>
1915
   */
1916
  public function map($callable)
1917
  {
1918
    $result = \array_map($callable, $this->array);
1919
1920
    return static::create($result);
1921
  }
1922
1923
  /**
1924
   * Check if all items in current array match a truth test.
1925
   *
1926
   * @param \Closure $closure
1927 25
   *
1928
   * @return bool
1929 25
   */
1930 4 View Code Duplication
  public function matches(\Closure $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1931 4
  {
1932 21
    if (count($this->array) === 0) {
1933
      return false;
1934
    }
1935 25
1936
    // init
1937
    $array = $this->array;
1938
1939
    foreach ($array as $key => $value) {
1940
      $value = $closure($value, $key);
1941
1942
      if ($value === false) {
1943
        return false;
1944
      }
1945
    }
1946
1947
    return true;
1948
  }
1949 16
1950
  /**
1951 16
   * Check if any item in the current array matches a truth test.
1952 4
   *
1953 4
   * @param \Closure $closure
1954 12
   *
1955
   * @return bool
1956
   */
1957 16 View Code Duplication
  public function matchesAny(\Closure $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1958
  {
1959
    if (count($this->array) === 0) {
1960
      return false;
1961
    }
1962
1963
    // init
1964
    $array = $this->array;
1965
1966
    foreach ($array as $key => $value) {
1967
      $value = $closure($value, $key);
1968
1969
      if ($value === true) {
1970 16
        return true;
1971
      }
1972 16
    }
1973 4
1974 4
    return false;
1975 12
  }
1976
1977
  /**
1978 16
   * Get the max value from an array.
1979
   *
1980
   * @return mixed
1981
   */
1982
  public function max()
1983
  {
1984
    if ($this->count() === 0) {
1985
      return false;
1986
    }
1987
1988
    return max($this->array);
1989
  }
1990
1991
  /**
1992 16
   * Merge the new $array into the current array.
1993
   *
1994 16
   * - keep key,value from the current array, also if the index is in the new $array
1995 4
   *
1996 4
   * @param array $array
1997 12
   * @param bool  $recursive
1998
   *
1999
   * @return static <p>(Immutable)</p>
2000 16
   */
2001 View Code Duplication
  public function mergeAppendKeepIndex(array $array = array(), $recursive = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2002
  {
2003
    if (true === $recursive) {
2004
      $result = \array_replace_recursive($this->array, $array);
2005
    } else {
2006
      $result = \array_replace($this->array, $array);
2007
    }
2008 10
2009
    return static::create($result);
2010 10
  }
2011 1
2012
  /**
2013
   * Merge the new $array into the current array.
2014 9
   *
2015
   * - replace duplicate assoc-keys from the current array with the key,values from the new $array
2016
   * - create new indexes
2017
   *
2018
   * @param array $array
2019
   * @param bool  $recursive
2020
   *
2021
   * @return static <p>(Immutable)</p>
2022
   */
2023 View Code Duplication
  public function mergeAppendNewIndex(array $array = array(), $recursive = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2024
  {
2025
    if (true === $recursive) {
2026
      $result = \array_merge_recursive($this->array, $array);
2027 1
    } else {
2028
      $result = \array_merge($this->array, $array);
2029 1
    }
2030
2031 1
    return static::create($result);
2032 1
  }
2033 1
2034 1
  /**
2035 1
   * Merge the the current array into the $array.
2036 1
   *
2037 1
   * - use key,value from the new $array, also if the index is in the current array
2038 1
   *
2039 1
   * @param array $array
2040 1
   * @param bool  $recursive
2041 1
   *
2042 1
   * @return static <p>(Immutable)</p>
2043 1
   */
2044 1 View Code Duplication
  public function mergePrependKeepIndex(array $array = array(), $recursive = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2045 1
  {
2046 1
    if (true === $recursive) {
2047 1
      $result = \array_replace_recursive($array, $this->array);
2048 1
    } else {
2049
      $result = \array_replace($array, $this->array);
2050
    }
2051
2052 1
    return static::create($result);
2053
  }
2054
2055
  /**
2056
   * Merge the current array into the new $array.
2057
   *
2058
   * - replace duplicate assoc-keys from new $array with the key,values from the current array
2059
   * - create new indexes
2060
   *
2061
   * @param array $array
2062
   * @param bool  $recursive
2063
   *
2064
   * @return static <p>(Immutable)</p>
2065
   */
2066 View Code Duplication
  public function mergePrependNewIndex(array $array = array(), $recursive = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2067
  {
2068
    if (true === $recursive) {
2069
      $result = \array_merge_recursive($array, $this->array);
2070
    } else {
2071
      $result = \array_merge($array, $this->array);
2072
    }
2073
2074
    return static::create($result);
2075
  }
2076
2077 4
  /**
2078
   * Get the min value from an array.
2079 4
   *
2080
   * @return mixed
2081 4
   */
2082
  public function min()
2083
  {
2084
    if ($this->count() === 0) {
2085
      return false;
2086
    }
2087
2088
    return min($this->array);
2089 16
  }
2090
2091 16
  /**
2092
   * Move an array element to a new index.
2093
   *
2094
   * cherry-picked from: http://stackoverflow.com/questions/12624153/move-an-array-element-to-a-new-index-in-php
2095
   *
2096
   * @param int|string $from
2097
   * @param int|string $to
2098
   *
2099
   * @return static <p>(Immutable)</p>
2100
   */
2101
  public function moveElement($from, $to)
2102 8
  {
2103
    $array = $this->array;
2104 8
2105 8
    if (is_int($from)) {
2106 8
      $tmp = \array_splice($array, $from, 1);
2107
      \array_splice($array, $to, 0, $tmp);
2108 1
      $output = $array;
2109
    } elseif (is_string($from)) {
2110
      $indexToMove = \array_search($from, \array_keys($array), true);
2111 8
      $itemToMove = $array[$from];
2112
      \array_splice($array, $indexToMove, 1);
2113
      $i = 0;
2114
      $output = array();
2115
      foreach ($array as $key => $item) {
2116
        if ($i == $to) {
2117
          $output[$from] = $itemToMove;
2118
        }
2119 4
        $output[$key] = $item;
2120
        $i++;
2121 4
      }
2122 4
    } else {
2123 4
      $output = array();
2124 4
    }
2125
2126 4
    return static::create($output);
2127
  }
2128
2129
  /**
2130
   * Get a subset of the items from the given array.
2131
   *
2132
   * @param mixed[] $keys
2133
   *
2134
   * @return static <p>(Immutable)</p>
2135
   */
2136 17
  public function only(array $keys)
2137
  {
2138 17
    $array = $this->array;
2139
2140
    return static::create(\array_intersect_key($array, \array_flip($keys)));
2141
  }
2142 17
2143 14
  /**
2144
   * Pad array to the specified size with a given value.
2145 14
   *
2146
   * @param int   $size  <p>Size of the result array.</p>
2147
   * @param mixed $value <p>Empty value by default.</p>
2148 5
   *
2149 5
   * @return static <p>(Immutable) Arrayy object padded to $size with $value.</p>
2150
   */
2151 5
  public function pad($size, $value)
2152
  {
2153
    $result = \array_pad($this->array, $size, $value);
2154
2155
    return static::create($result);
2156
  }
2157
2158
  /**
2159
   * Pop a specified value off the end of the current array.
2160
   *
2161 4
   * @return mixed <p>(Mutable) The popped element from the current array.</p>
2162
   */
2163 4
  public function pop()
2164
  {
2165 4
    return \array_pop($this->array);
2166
  }
2167
2168
  /**
2169 4
   * Prepend a value to the current array.
2170
   *
2171
   * @param mixed $value
2172
   * @param mixed $key
2173
   *
2174
   * @return static <p>(Mutable) Return this Arrayy object, with the prepended value.</p>
2175
   */
2176
  public function prepend($value, $key = null)
2177
  {
2178
    if ($key === null) {
2179
      \array_unshift($this->array, $value);
2180
    } else {
2181 14
      /** @noinspection AdditionOperationOnArraysInspection */
2182
      $this->array = array($key => $value) + $this->array;
2183 14
    }
2184 14
2185
    return $this;
2186 14
  }
2187 3
2188 3
  /**
2189 3
   * Push one or more values onto the end of array at once.
2190 3
   *
2191
   * @return static <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p>
2192 3
   */
2193 3 View Code Duplication
  public function push(/* variadic arguments allowed */)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2194
  {
2195
    if (func_num_args()) {
2196 11
      $args = \array_merge(array(&$this->array), func_get_args());
2197
      call_user_func_array('array_push', $args);
2198 11
    }
2199
2200
    return $this;
2201
  }
2202
2203
  /**
2204
   * Get a random value from the current array.
2205
   *
2206
   * @param null|int $number <p>How many values you will take?</p>
2207
   *
2208 17
   * @return static <p>(Immutable)</p>
2209
   */
2210 17
  public function randomImmutable($number = null)
2211
  {
2212
    if ($this->count() === 0) {
2213
      return static::create();
2214 17
    }
2215 7
2216 7 View Code Duplication
    if ($number === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
2217
      $arrayRandValue = array($this->array[\array_rand($this->array)]);
2218 7
2219
      return static::create($arrayRandValue);
2220
    }
2221 11
2222
    $arrayTmp = $this->array;
2223 11
    shuffle($arrayTmp);
2224
2225
    return static::create($arrayTmp)->firstsImmutable($number);
2226
  }
2227
2228
  /**
2229
   * Pick a random key/index from the keys of this array.
2230
   *
2231 4
   * @return mixed <p>Get a key/index or null if there wasn't a key/index.</p>
2232
   *
2233 4
   * @throws \RangeException If array is empty
2234
   */
2235 4
  public function randomKey()
2236
  {
2237
    $result = $this->randomKeys(1);
2238
2239 4
    if (!isset($result[0])) {
2240
      $result[0] = null;
2241
    }
2242
2243
    return $result[0];
2244
  }
2245
2246
  /**
2247
   * Pick a given number of random keys/indexes out of this array.
2248
   *
2249 7
   * @param int $number <p>The number of keys/indexes (should be <= $this->count())</p>
2250
   *
2251 7
   * @return static <p>(Immutable)</p>
2252
   *
2253 7
   * @throws \RangeException If array is empty
2254
   */
2255
  public function randomKeys($number)
2256
  {
2257
    $number = (int)$number;
2258
    $count = $this->count();
2259
2260
    if ($number === 0 || $number > $count) {
2261
      throw new \RangeException(
2262
          sprintf(
2263
              'Number of requested keys (%s) must be equal or lower than number of elements in this array (%s)',
2264
              $number,
2265
              $count
2266 9
          )
2267
      );
2268 9
    }
2269 9
2270 9
    $result = (array)\array_rand($this->array, $number);
2271 2
2272 1
    return static::create($result);
2273 1
  }
2274 2
2275 9
  /**
2276
   * Get a random value from the current array.
2277 9
   *
2278
   * @param null|int $number <p>How many values you will take?</p>
2279
   *
2280
   * @return static <p>(Mutable)</p>
2281
   */
2282
  public function randomMutable($number = null)
2283
  {
2284
    if ($this->count() === 0) {
2285
      return static::create();
2286
    }
2287
2288 3 View Code Duplication
    if ($number === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
2289
      $arrayRandValue = array($this->array[\array_rand($this->array)]);
2290 3
      $this->array = $arrayRandValue;
2291
2292 3
      return $this;
2293
    }
2294
2295 3
    shuffle($this->array);
2296
2297
    return $this->firstsMutable($number);
2298 3
  }
2299
2300
  /**
2301
   * Pick a random value from the values of this array.
2302
   *
2303
   * @return mixed <p>Get a random value or null if there wasn't a value.</p>
2304
   */
2305
  public function randomValue()
2306 9
  {
2307
    $result = $this->randomImmutable();
2308 9
2309
    if (!isset($result[0])) {
2310 9
      $result[0] = null;
2311
    }
2312
2313
    return $result[0];
2314
  }
2315
2316
  /**
2317
   * Pick a given number of random values out of this array.
2318
   *
2319
   * @param int $number
2320 1
   *
2321
   * @return static <p>(Mutable)</p>
2322 1
   */
2323
  public function randomValues($number)
2324 1
  {
2325 1
    $number = (int)$number;
2326 1
2327 1
    return $this->randomMutable($number);
2328 1
  }
2329
2330 1
  /**
2331
   * Get a random value from an array, with the ability to skew the results.
2332
   *
2333
   * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
2334
   *
2335
   * @param array    $array
2336
   * @param null|int $number <p>How many values you will take?</p>
2337
   *
2338
   * @return static <p>(Immutable)</p>
2339
   */
2340 18
  public function randomWeighted(array $array, $number = null)
2341
  {
2342
    $options = array();
2343 18
    foreach ($array as $option => $weight) {
2344
      if ($this->searchIndex($option) !== false) {
2345
        for ($i = 0; $i < $weight; ++$i) {
2346
          $options[] = $option;
2347
        }
2348
      }
2349
    }
2350
2351 18
    return $this->mergeAppendKeepIndex($options)->randomImmutable($number);
2352
  }
2353 18
2354
  /**
2355
   * Reduce the current array via callable e.g. anonymous-function.
2356
   *
2357
   * @param mixed $callable
2358
   * @param array $init
2359
   *
2360
   * @return static <p>(Immutable)</p>
2361 7
   */
2362
  public function reduce($callable, array $init = array())
2363 7
  {
2364 7
    $result = \array_reduce($this->array, $callable, $init);
2365
2366 7
    if ($result === null) {
2367
      $this->array = array();
2368
    } else {
2369
      $this->array = (array)$result;
2370
    }
2371
2372
    return static::create($this->array);
2373
  }
2374 7
2375
  /**
2376 7
   * Create a numerically re-indexed Arrayy object.
2377 7
   *
2378
   * @return static <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p>
2379 7
   */
2380
  public function reindex()
2381
  {
2382
    $this->array = \array_values($this->array);
2383
2384
    return $this;
2385
  }
2386
2387
  /**
2388
   * Return all items that fail the truth test.
2389 7
   *
2390
   * @param \Closure $closure
2391 7
   *
2392 7
   * @return static <p>(Immutable)</p>
2393 6
   */
2394 6 View Code Duplication
  public function reject(\Closure $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2395
  {
2396
    $filtered = array();
2397 6
2398 6
    foreach ($this->array as $key => $value) {
2399 7
      if (!$closure($value, $key)) {
2400
        $filtered[$key] = $value;
2401 7
      }
2402 7
    }
2403 7
2404
    return static::create($filtered);
2405 7
  }
2406
2407
  /**
2408
   * Remove a value from the current array (optional using dot-notation).
2409
   *
2410
   * @param mixed $key
2411
   *
2412
   * @return static <p>(Immutable)</p>
2413
   */
2414
  public function remove($key)
2415 1
  {
2416
    // Recursive call
2417 1
    if (is_array($key)) {
2418 1
      foreach ($key as $k) {
2419
        $this->internalRemove($k);
2420
      }
2421 1
2422
      return static::create($this->array);
2423
    }
2424
2425
    $this->internalRemove($key);
2426
2427
    return static::create($this->array);
2428
  }
2429
2430
  /**
2431
   * Remove the first value from the current array.
2432
   *
2433 2
   * @return static <p>(Immutable)</p>
2434
   */
2435 2
  public function removeFirst()
2436
  {
2437 2
    $tmpArray = $this->array;
2438
    \array_shift($tmpArray);
2439
2440
    return static::create($tmpArray);
2441
  }
2442
2443
  /**
2444
   * Remove the last value from the current array.
2445
   *
2446
   * @return static <p>(Immutable)</p>
2447 2
   */
2448
  public function removeLast()
2449 2
  {
2450
    $tmpArray = $this->array;
2451 2
    \array_pop($tmpArray);
2452
2453
    return static::create($tmpArray);
2454
  }
2455
2456
  /**
2457
   * Removes a particular value from an array (numeric or associative).
2458
   *
2459
   * @param mixed $value
2460
   *
2461 2
   * @return static <p>(Immutable)</p>
2462
   */
2463 2
  public function removeValue($value)
2464
  {
2465 2
    $isNumericArray = true;
2466
    foreach ($this->array as $key => $item) {
2467
      if ($item === $value) {
2468
        if (!is_int($key)) {
2469
          $isNumericArray = false;
2470
        }
2471
        unset($this->array[$key]);
2472
      }
2473
    }
2474
2475 1
    if ($isNumericArray) {
2476
      $this->array = \array_values($this->array);
2477 1
    }
2478 1
2479
    return static::create($this->array);
2480 1
  }
2481
2482
  /**
2483
   * Generate array of repeated arrays.
2484
   *
2485
   * @param int $times <p>How many times has to be repeated.</p>
2486
   *
2487
   * @return Arrayy
2488
   */
2489
  public function repeat($times)
2490
  {
2491 3
    if ($times === 0) {
2492
      return new static();
2493 3
    }
2494 3
2495
    return static::create(\array_fill(0, (int)$times, $this->array));
2496 3
  }
2497 3
2498 3
  /**
2499
   * Replace a key with a new key/value pair.
2500 3
   *
2501
   * @param $replace
2502
   * @param $key
2503
   * @param $value
2504
   *
2505
   * @return static <p>(Immutable)</p>
2506
   */
2507
  public function replace($replace, $key, $value)
2508
  {
2509
    $this->remove($replace);
2510
2511 1
    return $this->set($key, $value);
2512
  }
2513 1
2514
  /**
2515 1
   * Create an array using the current array as values and the other array as keys.
2516
   *
2517 1
   * @param array $keys <p>An array of keys.</p>
2518
   *
2519 1
   * @return static <p>(Immutable) Arrayy object with keys from the other array.</p>
2520
   */
2521
  public function replaceAllKeys(array $keys)
2522
  {
2523
    $result = \array_combine($keys, $this->array);
2524
2525
    return static::create($result);
2526
  }
2527
2528
  /**
2529 15
   * Create an array using the current array as keys and the other array as values.
2530
   *
2531 15
   * @param array $array <p>An array o values.</p>
2532
   *
2533 15
   * @return static <p>(Immutable) Arrayy object with values from the other array.</p>
2534
   */
2535
  public function replaceAllValues(array $array)
2536
  {
2537
    $result = \array_combine($this->array, $array);
2538
2539
    return static::create($result);
2540
  }
2541 7
2542
  /**
2543 7
   * Replace the keys in an array with another set.
2544
   *
2545 7
   * @param array $keys <p>An array of keys matching the array's size</p>
2546
   *
2547
   * @return static <p>(Immutable)</p>
2548
   */
2549
  public function replaceKeys(array $keys)
2550
  {
2551
    $values = \array_values($this->array);
2552
    $result = \array_combine($keys, $values);
2553
2554
    return static::create($result);
2555 20
  }
2556
2557 20
  /**
2558
   * Replace the first matched value in an array.
2559
   *
2560
   * @param mixed $search
2561
   * @param mixed $replacement
2562
   *
2563
   * @return static <p>(Immutable)</p>
2564
   */
2565
  public function replaceOneValue($search, $replacement = '')
2566
  {
2567 9
    $array = $this->array;
2568
    $key = \array_search($search, $array, true);
2569
2570 9
    if ($key !== false) {
2571
      $array[$key] = $replacement;
2572 9
    }
2573
2574
    return static::create($array);
2575
  }
2576
2577 9
  /**
2578 1
   * Replace values in the current array.
2579 1
   *
2580
   * @param string $search      <p>The string to replace.</p>
2581 9
   * @param string $replacement <p>What to replace it with.</p>
2582 7
   *
2583 7
   * @return static <p>(Immutable)</p>
2584
   */
2585
  public function replaceValues($search, $replacement = '')
2586 9
  {
2587
    $array = $this->each(
2588
        function ($value) use ($search, $replacement) {
2589
          return UTF8::str_replace($search, $replacement, $value);
2590
        }
2591
    );
2592
2593
    return $array;
2594
  }
2595
2596
  /**
2597 17
   * Get the last elements from index $from until the end of this array.
2598
   *
2599 17
   * @param int $from
2600
   *
2601 17
   * @return static <p>(Immutable)</p>
2602
   */
2603
  public function rest($from = 1)
2604
  {
2605
    $tmpArray = $this->array;
2606
2607
    return static::create(\array_splice($tmpArray, $from));
2608
  }
2609
2610
  /**
2611
   * Return the array in the reverse order.
2612
   *
2613
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2614 11
   */
2615
  public function reverse()
2616
  {
2617 11
    $this->array = \array_reverse($this->array);
2618 4
2619 4
    return $this;
2620
  }
2621 11
2622
  /**
2623
   * Sort an array in reverse order.
2624
   *
2625
   * @param int $sort_flags [optional] <p>
2626
   *                        You may modify the behavior of the sort using the optional
2627
   *                        parameter sort_flags, for details
2628
   *                        see sort.
2629 4
   *                        </p>
2630
   *
2631 4
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2632
   */
2633
  public function rsort($sort_flags = null)
2634
  {
2635
    rsort($this->array, $sort_flags);
2636
2637
    return $this;
2638
  }
2639 1
2640
  /**
2641 1
   * Search for the first index of the current array via $value.
2642
   *
2643 1
   * @param mixed $value
2644
   *
2645 1
   * @return int|float|string
2646
   */
2647
  public function searchIndex($value)
2648
  {
2649
    return \array_search($value, $this->array, true);
2650
  }
2651
2652
  /**
2653 93
   * Search for the value of the current array via $index.
2654
   *
2655 93
   * @param mixed $index
2656
   *
2657
   * @return static <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p>
2658
   */
2659
  public function searchValue($index)
2660
  {
2661
    // init
2662
    $return = array();
2663
2664
    if ($this->isEmpty()) {
2665
      return static::create();
2666
    }
2667 4
2668
    // php cast "bool"-index into "int"-index
2669 4
    if ((bool)$index === $index) {
2670
      $index = (int)$index;
2671 4
    }
2672
2673
    if (\array_key_exists($index, $this->array) === true) {
2674
      $return = array($this->array[$index]);
2675
    }
2676
2677
2678
    return static::create($return);
2679
  }
2680
2681
  /**
2682
   * Set a value for the current array (optional using dot-notation).
2683 19
   *
2684
   * @param string $key   <p>The key to set.</p>
2685 19
   * @param mixed  $value <p>Its value.</p>
2686
   *
2687 19
   * @return static <p>(Immutable)</p>
2688
   */
2689
  public function set($key, $value)
2690
  {
2691
    $this->internalSet($key, $value);
2692
2693
    return static::create($this->array);
2694
  }
2695
2696
  /**
2697
   * Get a value from a array and set it if it was not.
2698
   *
2699
   * WARNING: this method only set the value, if the $key is not already set
2700
   *
2701
   * @param string $key      <p>The key</p>
2702 18
   * @param mixed  $fallback <p>The default value to set if it isn't.</p>
2703
   *
2704 18
   * @return mixed <p>(Mutable)</p>
2705
   */
2706 18
  public function setAndGet($key, $fallback = null)
2707
  {
2708
    // If the key doesn't exist, set it.
2709
    if (!$this->has($key)) {
2710
      $this->array = $this->set($key, $fallback)->getArray();
2711
    }
2712
2713
    return $this->get($key);
2714
  }
2715
2716
  /**
2717 1
   * Shifts a specified value off the beginning of array.
2718
   *
2719 1
   * @return mixed <p>(Mutable) A shifted element from the current array.</p>
2720
   */
2721
  public function shift()
2722
  {
2723
    return \array_shift($this->array);
2724
  }
2725
2726
  /**
2727
   * Shuffle the current array.
2728
   *
2729
   * @return static <p>(Immutable)</p>
2730 1
   */
2731
  public function shuffle()
2732 1
  {
2733
    $array = $this->array;
2734
2735
    shuffle($array);
2736
2737
    return static::create($array);
2738
  }
2739
2740
  /**
2741
   * Get the size of an array.
2742
   *
2743
   * @return int
2744
   */
2745
  public function size()
2746
  {
2747
    return count($this->array);
2748 1
  }
2749
2750 1
  /**
2751 1
   * Extract a slice of the array.
2752
   *
2753
   * @param int      $offset       <p>Slice begin index.</p>
2754 1
   * @param int|null $length       <p>Length of the slice.</p>
2755 1
   * @param bool     $preserveKeys <p>Whether array keys are preserved or no.</p>
2756
   *
2757 1
   * @return static <p>A slice of the original array with length $length.</p>
2758 1
   */
2759
  public function slice($offset, $length = null, $preserveKeys = false)
2760 1
  {
2761
    $result = \array_slice($this->array, $offset, $length, $preserveKeys);
2762 1
2763
    return static::create($result);
2764 1
  }
2765 1
2766 1
  /**
2767
   * Sort the current array and optional you can keep the keys.
2768
   *
2769
   * @param integer $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2770 1
   * @param integer $strategy  <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or
2771
   *                           <strong>SORT_NATURAL</strong></p>
2772 1
   * @param bool    $keepKeys
2773
   *
2774
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2775
   */
2776
  public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2777
  {
2778
    $this->sorting($this->array, $direction, $strategy, $keepKeys);
2779
2780
    return $this;
2781
  }
2782
2783
  /**
2784 18
   * Sort the current array by key.
2785
   *
2786 18
   * @link http://php.net/manual/en/function.ksort.php
2787
   * @link http://php.net/manual/en/function.krsort.php
2788
   *
2789 18
   * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2790 18
   * @param int        $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or
2791 6
   *                              <strong>SORT_NATURAL</strong></p>
2792 6
   *
2793 13
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2794 13
   */
2795 13
  public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR)
2796 13
  {
2797 13
    $this->sorterKeys($this->array, $direction, $strategy);
2798 18
2799
    return $this;
2800
  }
2801
2802
  /**
2803
   * Sort the current array by value.
2804
   *
2805
   * @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2806
   * @param int $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p>
2807
   *
2808
   * @return static <p>(Mutable)</p>
2809 19
   */
2810
  public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2811 19
  {
2812
    return $this->sort($direction, $strategy, true);
2813 19
  }
2814 19
2815 19
  /**
2816
   * Sort the current array by value.
2817
   *
2818 19
   * @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2819 19
   * @param int $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p>
2820 9
   *
2821 5
   * @return static <p>(Mutable)</p>
2822 5
   */
2823 4
  public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2824
  {
2825 9
    return $this->sort($direction, $strategy, false);
2826 10
  }
2827 10
2828 10
  /**
2829 10
   * Sort a array by value, by a closure or by a property.
2830 4
   *
2831 4
   * - If the sorter is null, the array is sorted naturally.
2832 6
   * - Associative (string) keys will be maintained, but numeric keys will be re-indexed.
2833
   *
2834 10
   * @param null       $sorter
2835 19
   * @param string|int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2836
   * @param int        $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or
2837
   *                              <strong>SORT_NATURAL</strong></p>
2838
   *
2839
   * @return static <p>(Immutable)</p>
2840
   */
2841
  public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2842
  {
2843
    $array = (array)$this->array;
2844
    $direction = $this->getDirection($direction);
2845 1
2846
    // Transform all values into their results.
2847 1
    if ($sorter) {
2848
      $arrayy = static::create($array);
2849 1
2850 1
      $that = $this;
2851 1
      $results = $arrayy->each(
2852 1
          function ($value) use ($sorter, $that) {
2853 1
            return is_callable($sorter) ? $sorter($value) : $that->get($sorter, null, $value);
2854 1
          }
2855
      );
2856
2857 1
      $results = $results->getArray();
2858
    } else {
2859
      $results = $array;
2860
    }
2861
2862
    // Sort by the results and replace by original values
2863
    \array_multisort($results, $direction, $strategy, $array);
2864
2865 1
    return static::create($array);
2866
  }
2867 1
2868
  /**
2869 1
   * sorting keys
2870 1
   *
2871
   * @param array $elements
2872
   * @param int   $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2873 1
   * @param int   $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p>
2874
   *
2875 1
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2876
   */
2877
  protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2878
  {
2879
    $direction = $this->getDirection($direction);
2880
2881
    switch ($direction) {
2882
      case 'desc':
2883
      case SORT_DESC:
2884
        krsort($elements, $strategy);
2885
        break;
2886 1
      case 'asc':
2887
      case SORT_ASC:
2888 1
      default:
2889
        ksort($elements, $strategy);
2890 1
    }
2891
2892 1
    return $this;
2893
  }
2894
2895
  /**
2896
   * @param array      &$elements
2897
   * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p>
2898
   * @param int        $strategy  <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or
2899
   *                              <strong>SORT_NATURAL</strong></p>
2900 142
   * @param bool       $keepKeys
2901
   *
2902 142
   * @return static <p>(Mutable) Return this Arrayy object.</p>
2903
   */
2904
  protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2905
  {
2906
    $direction = $this->getDirection($direction);
2907
2908
    if (!$strategy) {
2909
      $strategy = SORT_REGULAR;
2910
    }
2911
2912
    switch ($direction) {
2913 6
      case 'desc':
2914
      case SORT_DESC:
2915 6
        if ($keepKeys) {
2916
          arsort($elements, $strategy);
2917
        } else {
2918
          rsort($elements, $strategy);
2919
        }
2920
        break;
2921
      case 'asc':
2922
      case SORT_ASC:
2923
      default:
2924
        if ($keepKeys) {
2925 19
          asort($elements, $strategy);
2926
        } else {
2927 19
          sort($elements, $strategy);
2928
        }
2929
    }
2930
2931
    return $this;
2932
  }
2933
2934
  /**
2935 9
   * Split an array in the given amount of pieces.
2936
   *
2937 9
   * @param int  $numberOfPieces
2938 9
   * @param bool $keepKeys
2939
   *
2940 8
   * @return static <p>(Immutable)</p>
2941 8
   */
2942 8
  public function split($numberOfPieces = 2, $keepKeys = false)
2943
  {
2944 8
    $arrayCount = $this->count();
2945 9
2946 9
    if ($arrayCount === 0) {
2947 9
      $result = array();
2948
    } else {
2949 9
      $numberOfPieces = (int)$numberOfPieces;
2950
      $splitSize = (int)ceil($arrayCount / $numberOfPieces);
2951
      $result = \array_chunk($this->array, $splitSize, $keepKeys);
2952 9
    }
2953
2954
    return static::create($result);
2955 9
  }
2956
2957
  /**
2958
   * Stripe all empty items.
2959
   *
2960
   * @return static <p>(Immutable)</p>
2961
   */
2962
  public function stripEmpty()
2963 9
  {
2964
    return $this->filter(
2965
        function ($item) {
2966 9
          if ($item === null) {
2967
            return false;
2968 9
          }
2969 9
2970 9
          return (bool)trim((string)$item);
2971 8
        }
2972 8
    );
2973 8
  }
2974
2975 8
  /**
2976 9
   * Swap two values between positions by key.
2977 9
   *
2978 9
   * @param string|int $swapA <p>a key in the array</p>
2979
   * @param string|int $swapB <p>a key in the array</p>
2980 9
   *
2981
   * @return static <p>(Immutable)</p>
2982
   */
2983 9
  public function swap($swapA, $swapB)
2984
  {
2985
    $array = $this->array;
2986 9
2987
    list($array[$swapA], $array[$swapB]) = array($array[$swapB], $array[$swapA]);
2988
2989
    return static::create($array);
2990
  }
2991
2992
  /**
2993
   * alias: for "Arrayy->getArray()"
2994
   *
2995
   * @see Arrayy::getArray()
2996 9
   */
2997
  public function toArray()
2998 9
  {
2999
    return $this->getArray();
3000
  }
3001
3002
  /**
3003
   * Convert the current array to JSON.
3004
   *
3005
   * @param null|int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p>
3006 4
   * @param int      $depth   [optional] <p>Set the maximum depth. Must be greater than zero.</p>
3007
   *
3008 4
   * @return string
3009 4
   */
3010 4
  public function toJson($options = null, $depth = 512)
3011 4
  {
3012
    return UTF8::json_encode($this->array, $options, $depth);
3013 4
  }
3014
3015
  /**
3016
   * Implodes array to a string with specified separator.
3017
   *
3018
   * @param string $separator [optional] <p>The element's separator.</p>
3019
   *
3020
   * @return string <p>The string representation of array, separated by ",".</p>
3021 2
   */
3022
  public function toString($separator = ',')
3023 2
  {
3024
    return $this->implode($separator);
3025
  }
3026
3027
  /**
3028
   * Return a duplicate free copy of the current array.
3029
   *
3030
   * @return static <p>(Mutable)</p>
3031
   */
3032
  public function unique()
3033
  {
3034 9
    $this->array = \array_reduce(
0 ignored issues
show
Documentation Bug introduced by
It seems like \array_reduce($this->arr...esultArray; }, array()) of type * is incompatible with the declared type array of property $array.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
3035
        $this->array,
3036 9
        function ($resultArray, $value) {
3037 4
          if (!in_array($value, $resultArray, true)) {
3038 4
            $resultArray[] = $value;
3039 5
          }
3040
3041
          return $resultArray;
3042 9
        },
3043
        array()
3044
    );
3045
3046 View Code Duplication
    if ($this->array === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
3047
      $this->array = array();
3048
    } else {
3049
      $this->array = (array)$this->array;
3050
    }
3051
3052
    return $this;
3053
  }
3054
3055
  /**
3056
   * Return a duplicate free copy of the current array. (with the old keys)
3057
   *
3058
   * @return static <p>(Mutable)</p>
3059
   */
3060
  public function uniqueKeepIndex()
3061
  {
3062
    // init
3063
    $array = $this->array;
3064
3065
    $this->array = \array_reduce(
0 ignored issues
show
Documentation Bug introduced by
It seems like \array_reduce(\array_key...esultArray; }, array()) of type * is incompatible with the declared type array of property $array.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
3066
        \array_keys($array),
3067
        function ($resultArray, $key) use ($array) {
3068
          if (!in_array($array[$key], $resultArray, true)) {
3069
            $resultArray[$key] = $array[$key];
3070
          }
3071
3072
          return $resultArray;
3073
        },
3074
        array()
3075
    );
3076
3077 View Code Duplication
    if ($this->array === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
3078
      $this->array = array();
3079
    } else {
3080
      $this->array = (array)$this->array;
3081
    }
3082
3083
    return $this;
3084
  }
3085
3086
  /**
3087
   * alias: for "Arrayy->unique()"
3088
   *
3089
   * @see Arrayy::unique()
3090
   *
3091
   * @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p>
3092
   */
3093
  public function uniqueNewIndex()
3094
  {
3095
    return $this->unique();
3096
  }
3097
3098
  /**
3099
   * Prepends one or more values to the beginning of array at once.
3100
   *
3101
   * @return static <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p>
3102
   */
3103 View Code Duplication
  public function unshift(/* variadic arguments allowed */)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
3104
  {
3105
    if (func_num_args()) {
3106
      $args = \array_merge(array(&$this->array), func_get_args());
3107
      call_user_func_array('array_unshift', $args);
3108
    }
3109
3110
    return $this;
3111
  }
3112
3113
  /**
3114
   * Get all values from a array.
3115
   *
3116
   * @return static <p>(Immutable)</p>
3117
   */
3118
  public function values()
3119
  {
3120
    return static::create(\array_values((array)$this->array));
3121
  }
3122
3123
  /**
3124
   * Apply the given function to every element in the array, discarding the results.
3125
   *
3126
   * @param callable $callable
3127
   * @param bool     $recursive <p>Whether array will be walked recursively or no</p>
3128
   *
3129
   * @return static <p>(Mutable) Return this Arrayy object, with modified elements.</p>
3130
   */
3131
  public function walk($callable, $recursive = false)
3132
  {
3133
    if (true === $recursive) {
3134
      \array_walk_recursive($this->array, $callable);
3135
    } else {
3136
      \array_walk($this->array, $callable);
3137
    }
3138
3139
    return $this;
3140
  }
3141
}
3142