Completed
Push — master ( f4d837...8744d3 )
by Lars
03:14
created

Arrayy::containsCaseInsensitive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace Arrayy;
4
5
use ArrayAccess;
6
use Closure;
7
use voku\helper\UTF8;
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
16
{
17
  /**
18
   * @var array
19
   */
20
  protected $array = array();
21
22
  /** @noinspection MagicMethodsValidityInspection */
23
  /**
24
   * Initializes
25
   *
26
   * @param array $array
27
   */
28 718
  public function __construct($array = array())
29
  {
30 718
    $array = $this->fallbackForArray($array);
31
32 716
    $this->array = $array;
0 ignored issues
show
Documentation Bug introduced by
It seems like $array can also be of type object<Arrayy\Arrayy>. However, the property $array is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
33 716
  }
34
35
  /**
36
   * Get a value by key.
37
   *
38
   * @param $key
39
   *
40
   * @return mixed Get a Value from the current array.
41
   */
42
  public function &__get($key)
43
  {
44
    return $this->array[$key];
45
  }
46
47
  /**
48
   * Call object as function.
49
   *
50
   * @param mixed $key
51
   *
52
   * @return mixed
53
   */
54
  public function __invoke($key = null)
55
  {
56
    if ($key !== null) {
57
      if (isset($this->array[$key])) {
58
        return $this->array[$key];
59
      } else {
60
        return false;
61
      }
62
    }
63
64
    return (array)$this->array;
65
  }
66
67
  /**
68
   * Whether or not an element exists by key.
69
   *
70
   * @param mixed $key
71
   *
72
   * @return bool True is the key/index exists, otherwise false.
73
   */
74
  public function __isset($key)
75
  {
76
    return isset($this->array[$key]);
77
  }
78
79
  /**
80
   * Assigns a value to the specified element.
81
   *
82
   * @param mixed $key
83
   * @param mixed $value
84
   */
85
  public function __set($key, $value)
86
  {
87
    $this->array[$key] = $value;
88
  }
89
90
  /**
91
   * magic to string
92
   *
93
   * @return string
94
   */
95 15
  public function __toString()
96
  {
97 15
    return $this->toString();
98
  }
99
100
  /**
101
   * Unset element by key.
102
   *
103
   * @param mixed $key
104
   */
105
  public function __unset($key)
106
  {
107
    unset($this->array[$key]);
108
  }
109
110
  /**
111
   * alias: for "Arrayy->append()"
112
   *
113
   * @param mixed $value
114
   *
115
   * @return self (Mutable) Return this Arrayy object, with the appended values.
116
   */
117 1
  public function add($value)
118
  {
119 1
    return $this->append($value);
120
  }
121
122
  /**
123
   * Append a value to the current array.
124
   *
125
   * @param mixed $value
126
   *
127
   * @return self (Mutable) Return this Arrayy object, with the appended values.
128
   */
129 9
  public function append($value)
130
  {
131 9
    $this->array[] = $value;
132
133 9
    return $this;
134
  }
135
136
  /**
137
   * Count the values from the current array.
138
   *
139
   * INFO: only a alias for "$arrayy->size()"
140
   *
141
   * @return int
142
   */
143 110
  public function count()
144
  {
145 110
    return $this->size();
146
  }
147
148
  /**
149
   * Returns a new ArrayIterator, thus implementing the IteratorAggregate interface.
150
   *
151
   * @return \ArrayIterator An iterator for the values in the array.
152
   */
153 17
  public function getIterator()
154
  {
155 17
    return new \ArrayIterator($this->array);
156
  }
157
158
  /**
159
   * Whether or not an offset exists.
160
   *
161
   * @param mixed $offset
162
   *
163
   * @return bool
164
   */
165 31
  public function offsetExists($offset)
166
  {
167 31
    return isset($this->array[$offset]);
168
  }
169
170
  /**
171
   * Returns the value at specified offset.
172
   *
173
   * @param mixed $offset
174
   *
175
   * @return mixed return null if the offset did not exists
176
   */
177 22
  public function offsetGet($offset)
178
  {
179 22
    return $this->offsetExists($offset) ? $this->array[$offset] : null;
180
  }
181
182
  /**
183
   * Assigns a value to the specified offset.
184
   *
185
   * @param mixed $offset
186
   * @param mixed $value
187
   */
188 13
  public function offsetSet($offset, $value)
189
  {
190 13
    if (null === $offset) {
191 4
      $this->array[] = $value;
192
    } else {
193 9
      $this->array[$offset] = $value;
194
    }
195 13
  }
196
197
  /**
198
   * Unset an offset.
199
   *
200
   * @param mixed $offset
201
   */
202 5
  public function offsetUnset($offset)
203
  {
204 5
    if ($this->offsetExists($offset)) {
205 3
      unset($this->array[$offset]);
206
    }
207 5
  }
208
209
  /**
210
   * Serialize the current array.
211
   *
212
   * @return string
213
   */
214 2
  public function serialize()
215
  {
216 2
    return serialize($this->array);
217
  }
218
219
  /**
220
   * Unserialize an string and return this object.
221
   *
222
   * @param string $string
223
   *
224
   * @return self (Mutable)
225
   */
226 1
  public function unserialize($string)
227
  {
228 1
    $this->array = unserialize($string);
229
230 1
    return $this;
231
  }
232
233
  /**
234
   * Iterate over the current array and execute a callback for each loop.
235
   *
236
   * @param \Closure $closure
237
   *
238
   * @return Arrayy (Immutable)
239
   */
240 2 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...
241
  {
242 2
    $array = $this->array;
243
244 2
    foreach ($array as $key => $value) {
245 2
      $closure($value, $key);
246
    }
247
248 2
    return static::create($array);
249
  }
250
251
  /**
252
   * Returns the average value of the current array.
253
   *
254
   * @param int $decimals The number of decimals to return
255
   *
256
   * @return int|double The average value
257
   */
258 10
  public function average($decimals = null)
259
  {
260 10
    $count = $this->count();
261
262 10
    if (!$count) {
263 2
      return 0;
264
    }
265
266 8
    if (!is_int($decimals)) {
267 3
      $decimals = null;
268
    }
269
270 8
    return round(array_sum($this->array) / $count, $decimals);
271
  }
272
273
  /**
274
   * Create a chunked version of the current array.
275
   *
276
   * @param int  $size         Size of each chunk
277
   * @param bool $preserveKeys Whether array keys are preserved or no
278
   *
279
   * @return Arrayy (Immutable) A new array of chunks from the original array.
280
   */
281 4
  public function chunk($size, $preserveKeys = false)
282
  {
283 4
    $result = array_chunk($this->array, $size, $preserveKeys);
284
285 4
    return static::create($result);
286
  }
287
288
  /**
289
   * Clean all falsy values from the current array.
290
   *
291
   * @return Arrayy (Immutable)
292
   */
293 8
  public function clean()
294
  {
295 8
    return $this->filter(
296
        function ($value) {
297 7
          return (bool)$value;
298 8
        }
299
    );
300
  }
301
302
  /**
303
   * WARNING!!! -> Clear the current array.
304
   *
305
   * @return self (Mutable) Return this Arrayy object, with an empty array.
306
   */
307 4
  public function clear()
308
  {
309 4
    $this->array = array();
310
311 4
    return $this;
312
  }
313
314
  /**
315
   * Check if an item is in the current array.
316
   *
317
   * @param mixed $value
318
   *
319
   * @return bool
320
   */
321 13
  public function contains($value)
322
  {
323 13
    return in_array($value, $this->array, true);
324
  }
325
326
  /**
327
   * Check if an (case-insensitive) string is in the current array.
328
   *
329
   * @param string $value
330
   *
331
   * @return bool
332
   */
333 13
  public function containsCaseInsensitive($value)
334
  {
335 13
    return in_array(
336 13
        UTF8::strtolower($value),
337
        array_map(
338
            array(
339 13
                new UTF8(),
340 13
                'strtolower',
341
            ),
342 13
            $this->array
343
        ),
344 13
        true
345
    );
346
  }
347
348
  /**
349
   * Check if the given key/index exists in the array.
350
   *
351
   * @param mixed $key Key/index to search for
352
   *
353
   * @return bool Returns true if the given key/index exists in the array, false otherwise
354
   */
355 4
  public function containsKey($key)
356
  {
357 4
    return array_key_exists($key, $this->array);
358
  }
359
360
  /** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
361
  /**
362
   * Creates an Arrayy object.
363
   *
364
   * @param array $array
365
   *
366
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
367
   */
368 445
  public static function create($array = array())
369
  {
370 445
    return new static($array);
371
  }
372
373
  /** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
374
  /**
375
   * WARNING: Creates an Arrayy object by reference.
376
   *
377
   * @param array $array
378
   *
379
   * @return self (Mutable) Return this Arrayy object.
380
   */
381
  public function createByReference(&$array = array())
382
  {
383
    $array = $this->fallbackForArray($array);
384
385
    $this->array = &$array;
386
387
    return $this;
388
  }
389
390
  /**
391
   * Create an new Arrayy object via JSON.
392
   *
393
   * @param string $json
394
   *
395
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
396
   */
397 5
  public static function createFromJson($json)
398
  {
399 5
    $array = UTF8::json_decode($json, true);
400
401 5
    return static::create($array);
402
  }
403
404
  /**
405
   * Create an new instance filled with values from an object that have implemented ArrayAccess.
406
   *
407
   * @param ArrayAccess $object Object that implements ArrayAccess
408
   *
409
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
410
   */
411 4
  public static function createFromObject(ArrayAccess $object)
412
  {
413 4
    $array = new static();
414 4
    foreach ($object as $key => $value) {
415
      /** @noinspection OffsetOperationsInspection */
416 3
      $array[$key] = $value;
417
    }
418
419 4
    return $array;
420
  }
421
422
  /**
423
   * Create an new instance containing a range of elements.
424
   *
425
   * @param mixed $low  First value of the sequence
426
   * @param mixed $high The sequence is ended upon reaching the end value
427
   * @param int   $step Used as the increment between elements in the sequence
428
   *
429
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
430
   */
431 1
  public static function createWithRange($low, $high, $step = 1)
432
  {
433 1
    return static::create(range($low, $high, $step));
434
  }
435
436
  /**
437
   * Create an new Arrayy object via string.
438
   *
439
   * @param string      $str       The input string.
440
   * @param string|null $delimiter The boundary string.
441
   * @param string|null $regEx     Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be used.
442
   *
443
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
444
   */
445 8
  public static function createFromString($str, $delimiter, $regEx = null)
446
  {
447 8
    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...
448 1
      preg_match_all($regEx, $str, $array);
449
450 1
      if (count($array) > 0) {
451 1
        $array = $array[0];
452
      }
453
454
    } else {
455 7
      $array = explode($delimiter, $str);
456
    }
457
458
    // trim all string in the array
459 8
    array_walk(
460
        $array,
461
        function (&$val) {
462
          /** @noinspection ReferenceMismatchInspection */
463 8
          if (is_string($val)) {
464 8
            $val = trim($val);
465
          }
466 8
        }
467
    );
468
469 8
    return static::create($array);
470
  }
471
472
  /**
473
   * Custom sort by index via "uksort".
474
   *
475
   * @link http://php.net/manual/en/function.uksort.php
476
   *
477
   * @param callable $function
478
   *
479
   * @return self (Mutable) Return this Arrayy object.
480
   */
481 5
  public function customSortKeys($function)
482
  {
483 5
    uksort($this->array, $function);
484
485 5
    return $this;
486
  }
487
488
  /**
489
   * Custom sort by value via "usort".
490
   *
491
   * @link http://php.net/manual/en/function.usort.php
492
   *
493
   * @param callable $function
494
   *
495
   * @return self (Mutable) Return this Arrayy object.
496
   */
497 4
  public function customSortValues($function)
498
  {
499 4
    usort($this->array, $function);
500
501 4
    return $this;
502
  }
503
504
  /**
505
   * Return values that are only in the current array.
506
   *
507
   * @param array $array
508
   *
509
   * @return Arrayy (Immutable)
510
   */
511 12
  public function diff(array $array = array())
512
  {
513 12
    $result = array_diff($this->array, $array);
514
515 12
    return static::create($result);
516
  }
517
518
  /**
519
   * Return values that are only in the new $array.
520
   *
521
   * @param array $array
522
   *
523
   * @return Arrayy (Immutable)
524
   */
525 8
  public function diffReverse(array $array = array())
526
  {
527 8
    $result = array_diff($array, $this->array);
528
529 8
    return static::create($result);
530
  }
531
532
  /**
533
   * Return values that are only in the current multi-dimensional array.
534
   *
535
   * @param array      $array
536
   * @param null|array $helperVariableForRecursion (only for internal usage)
537
   *
538
   * @return Arrayy (Immutable)
539
   */
540 1
  public function diffRecursive(array $array = array(), $helperVariableForRecursion = null)
541
  {
542 1
    $result = array();
543
544 1
    if ($helperVariableForRecursion !== null && is_array($helperVariableForRecursion) === true) {
545 1
      $arrayForTheLoop = $helperVariableForRecursion;
546
    } else {
547 1
      $arrayForTheLoop = $this->array;
548
    }
549
550 1
    foreach ($arrayForTheLoop as $key => $value) {
551 1
      if (array_key_exists($key, $array)) {
552 1
        if (is_array($value)) {
553 1
          $recursiveDiff = $this->diffRecursive($array[$key], $value);
554 1
          if (count($recursiveDiff)) {
555 1
            $result[$key] = $recursiveDiff;
556
          }
557
        } else {
558 1
          if ($value != $array[$key]) {
559 1
            $result[$key] = $value;
560
          }
561
        }
562
      } else {
563 1
        $result[$key] = $value;
564
      }
565
    }
566
567 1
    return static::create($result);
568
  }
569
570
  /**
571
   * Iterate over the current array and modify the array's value.
572
   *
573
   * @param \Closure $closure
574
   *
575
   * @return Arrayy (Immutable)
576
   */
577 22 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...
578
  {
579 22
    $array = $this->array;
580
581 22
    foreach ($array as $key => &$value) {
582 18
      $value = $closure($value, $key);
583
    }
584
585 22
    return static::create($array);
586
  }
587
588
  /**
589
   * Check if a value is in the current array using a closure.
590
   *
591
   * @param \Closure $closure
592
   *
593
   * @return bool Returns true if the given value is found, false otherwise
594
   */
595 4
  public function exists(\Closure $closure)
596
  {
597 4
    $isExists = false;
598 4
    foreach ($this->array as $key => $value) {
599 3
      if ($closure($value, $key)) {
600 1
        $isExists = true;
601 3
        break;
602
      }
603
    }
604
605 4
    return $isExists;
606
  }
607
608
  /**
609
   * create a fallback for array
610
   *
611
   * 1. use the current array, if it's a array
612
   * 2. call "getArray()" on object, if there is a "Arrayy"-object
613
   * 3. fallback to empty array, if there is nothing
614
   * 4. call "createFromObject()" on object, if there is a "ArrayAccess"-object
615
   * 5. call "__toArray()" on object, if the method exists
616
   * 6. cast a string or object with "__toString()" into an array
617
   * 7. throw a "InvalidArgumentException"-Exception
618
   *
619
   * @param $array
620
   *
621
   * @return array
622
   *
623
   * @throws \InvalidArgumentException
624
   */
625 718
  protected function fallbackForArray(&$array)
626
  {
627 718
    if (is_array($array)) {
628 715
      return $array;
629
    }
630
631 9
    if ($array instanceof self) {
632
      return $array->getArray();
633
    }
634
635 9
    if (!$array) {
636 6
      return array();
637
    }
638
    
639 8
    if ($array instanceof ArrayAccess) {
640
      /** @noinspection ReferenceMismatchInspection */
641
      return self::createFromObject($array);
642
    }
643
644 8
    if (is_object($array) && method_exists($array, '__toArray')) {
645
      return (array)$array->__toArray();
646
    }
647
648
    /** @noinspection ReferenceMismatchInspection */
649
    if (
650 8
        is_string($array)
651
        ||
652 8
        (is_object($array) && method_exists($array, '__toString'))
653
    ) {
654 6
      return (array)$array;
655
    }
656
657 2
    throw new \InvalidArgumentException(
658 2
        'Passed value should be a array'
659
    );
660
  }
661
662
  /**
663
   * Find all items in an array that pass the truth test.
664
   *
665
   * @param \Closure|null $closure
666
   *
667
   * @return Arrayy (Immutable)
668
   */
669 8
  public function filter($closure = null)
670
  {
671 8
    if (!$closure) {
672 1
      return $this->clean();
673
    }
674
675 8
    $array = array_filter($this->array, $closure);
676
677 8
    return static::create($array);
678
  }
679
680
  /**
681
   * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property
682
   * within that.
683
   *
684
   * @param        $property
685
   * @param        $value
686
   * @param string $comparisonOp
687
   *                            'eq' (equals),<br />
688
   *                            'gt' (greater),<br />
689
   *                            'gte' || 'ge' (greater or equals),<br />
690
   *                            'lt' (less),<br />
691
   *                            'lte' || 'le' (less or equals),<br />
692
   *                            'ne' (not equals),<br />
693
   *                            'contains',<br />
694
   *                            'notContains',<br />
695
   *                            'newer' (via strtotime),<br />
696
   *                            'older' (via strtotime),<br />
697
   *
698
   * @return Arrayy (Immutable)
699
   */
700 1
  public function filterBy($property, $value, $comparisonOp = null)
701
  {
702 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...
703 1
      $comparisonOp = is_array($value) ? 'contains' : 'eq';
704
    }
705
706
    $ops = array(
707
        'eq'          => function ($item, $prop, $value) {
708 1
          return $item[$prop] === $value;
709 1
        },
710
        'gt'          => function ($item, $prop, $value) {
711
          return $item[$prop] > $value;
712 1
        },
713
        'ge'         => function ($item, $prop, $value) {
714
          return $item[$prop] >= $value;
715 1
        },
716
        'gte'         => function ($item, $prop, $value) {
717
          return $item[$prop] >= $value;
718 1
        },
719
        'lt'          => function ($item, $prop, $value) {
720 1
          return $item[$prop] < $value;
721 1
        },
722
        'le'         => function ($item, $prop, $value) {
723
          return $item[$prop] <= $value;
724 1
        },
725
        'lte'         => function ($item, $prop, $value) {
726
          return $item[$prop] <= $value;
727 1
        },
728
        'ne'          => function ($item, $prop, $value) {
729
          return $item[$prop] !== $value;
730 1
        },
731
        'contains'    => function ($item, $prop, $value) {
732 1
          return in_array($item[$prop], (array)$value, true);
733 1
        },
734
        'notContains' => function ($item, $prop, $value) {
735
          return !in_array($item[$prop], (array)$value, true);
736 1
        },
737
        'newer'       => function ($item, $prop, $value) {
738
          return strtotime($item[$prop]) > strtotime($value);
739 1
        },
740
        'older'       => function ($item, $prop, $value) {
741
          return strtotime($item[$prop]) < strtotime($value);
742 1
        },
743
    );
744
745 1
    $result = array_values(
746
        array_filter(
747 1
            (array)$this->array,
748
            function ($item) use (
749 1
                $property,
750 1
                $value,
751 1
                $ops,
752 1
                $comparisonOp
753
            ) {
754 1
              $item = (array)$item;
755 1
              $itemArrayy = new Arrayy($item);
756 1
              $item[$property] = $itemArrayy->get($property, array());
757
758 1
              return $ops[$comparisonOp]($item, $property, $value);
759 1
            }
760
        )
761
    );
762
763 1
    return static::create($result);
764
  }
765
766
  /**
767
   * Find the first item in an array that passes the truth test,
768
   *  otherwise return false
769
   *
770
   * @param \Closure $closure
771
   *
772
   * @return mixed|false false if we did not find the value
773
   */
774 8
  public function find(\Closure $closure)
775
  {
776 8
    foreach ($this->array as $key => $value) {
777 6
      if ($closure($value, $key)) {
778 6
        return $value;
779
      }
780
    }
781
782 3
    return false;
783
  }
784
785
  /**
786
   * find by ...
787
   *
788
   * @param        $property
789
   * @param        $value
790
   * @param string $comparisonOp
791
   *
792
   * @return Arrayy (Immutable)
793
   */
794
  public function findBy($property, $value, $comparisonOp = 'eq')
795
  {
796
    return $this->filterBy($property, $value, $comparisonOp);
797
  }
798
799
  /**
800
   * Get the first value from the current array.
801
   *
802
   * @return mixed Return null if there wasn't a element.
803
   */
804 13
  public function first()
805
  {
806 13
    $result = array_shift($this->array);
807
808 13
    if (null === $result) {
809 3
      return null;
810
    } else {
811 10
      return $result;
812
    }
813
  }
814
815
  /**
816
   * Get the first value(s) from the current array.
817
   *
818
   * @param int|null $number how many values you will take?
819
   *
820
   * @return self (Mutable)
821
   */
822 26
  public function firstsMutable($number = null)
823
  {
824 26
    if ($number === null) {
825 11
      $this->array = (array)array_shift($this->array);
826
    } else {
827 15
      $number = (int)$number;
828 15
      $this->array = array_splice($this->array, 0, $number, true);
829
    }
830
831 26
    return $this;
832
  }
833
834
  /**
835
   * Get the first value(s) from the current array.
836
   *
837
   * @param int|null $number how many values you will take?
838
   *
839
   * @return Arrayy (Immutable)
840
   */
841 28
  public function firstsImmutable($number = null)
842
  {
843 28
    if ($number === null) {
844 7
      $array = (array)array_shift($this->array);
845
    } else {
846 21
      $number = (int)$number;
847 21
      $array = array_splice($this->array, 0, $number, true);
848
    }
849
850 28
    return static::create($array);
851
  }
852
853
  /**
854
   * Exchanges all keys with their associated values in an array.
855
   *
856
   * @return Arrayy (Immutable)
857
   */
858 1
  public function flip()
859
  {
860 1
    $result = array_flip($this->array);
861
862 1
    return static::create($result);
863
  }
864
865
  /**
866
   * Get a value from an array (optional using dot-notation).
867
   *
868
   * @param string $key     The key to look for.
869
   * @param mixed  $default Default value to fallback to.
870
   * @param array  $array   The array to get from, if it's set to "null" we use the current array from the class.
871
   *
872
   * @return mixed
873
   */
874 34
  public function get($key, $default = null, $array = null)
875
  {
876 34
    if (is_array($array) === true) {
877 3
      $usedArray = $array;
878
    } else {
879 32
      $usedArray = $this->array;
880
    }
881
882 34
    if (null === $key) {
883 1
      return $usedArray;
884
    }
885
886 34
    if (isset($usedArray[$key])) {
887 23
      return $usedArray[$key];
888
    }
889
890
    // Crawl through array, get key according to object or not
891 19
    foreach (explode('.', $key) as $segment) {
892 19
      if (!isset($usedArray[$segment])) {
893 17
        return $default instanceof Closure ? $default() : $default;
894
      }
895
896 2
      $usedArray = $usedArray[$segment];
897
    }
898
899 2
    return $usedArray;
900
  }
901
902
  /**
903
   * Get the current array from the "Arrayy"-object.
904
   *
905
   * @return array
906
   */
907 476
  public function getArray()
908
  {
909 476
    return $this->array;
910
  }
911
912
  /**
913
   * Returns the values from a single column of the input array, identified by
914
   * the $columnKey, can be used to extract data-columns from multi-arrays.
915
   *
916
   * Info: Optionally, you may provide an $indexKey to index the values in the returned
917
   * array by the values from the $indexKey column in the input array.
918
   *
919
   * @param mixed $columnKey
920
   * @param mixed $indexKey
921
   *
922
   * @return Arrayy (Immutable)
923
   */
924 1
  public function getColumn($columnKey = null, $indexKey = null)
925
  {
926 1
    $result = array_column($this->array, $columnKey, $indexKey);
927
928 1
    return static::create($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by array_column($this->array, $columnKey, $indexKey) on line 926 can also be of type false or null; however, Arrayy\Arrayy::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
929
  }
930
931
  /**
932
   * Get correct PHP constant for direction.
933
   *
934
   * @param int|string $direction
935
   *
936
   * @return int
937
   */
938 38
  protected function getDirection($direction)
939
  {
940 38
    if (is_string($direction)) {
941 10
      $direction = strtolower($direction);
942
943 10
      if ($direction === 'desc') {
944 2
        $direction = SORT_DESC;
945
      } else {
946 8
        $direction = SORT_ASC;
947
      }
948
    }
949
950
    if (
951 38
        $direction !== SORT_DESC
952
        &&
953 38
        $direction !== SORT_ASC
954
    ) {
955
      $direction = SORT_ASC;
956
    }
957
958 38
    return $direction;
959
  }
960
961
  /**
962
   * alias: for "Arrayy->keys()"
963
   *
964
   * @return Arrayy (Immutable)
965
   */
966
  public function getKeys()
967
  {
968
    return $this->keys();
969
  }
970
971
  /**
972
   * alias: for "Arrayy->random()"
973
   *
974
   * @return Arrayy (Immutable)
975
   */
976 3
  public function getRandom()
977
  {
978 3
    return $this->randomImmutable();
979
  }
980
981
  /**
982
   * alias: for "Arrayy->randomKey()"
983
   *
984
   * @return mixed get a key/index or null if there wasn't a key/index
985
   */
986 3
  public function getRandomKey()
987
  {
988 3
    return $this->randomKey();
989
  }
990
991
  /**
992
   * alias: for "Arrayy->randomKeys()"
993
   *
994
   * @param int $number
995
   *
996
   * @return Arrayy (Immutable)
997
   */
998 9
  public function getRandomKeys($number)
999
  {
1000 9
    return $this->randomKeys($number);
1001
  }
1002
1003
  /**
1004
   * alias: for "Arrayy->randomValue()"
1005
   *
1006
   * @return mixed get a random value or null if there wasn't a value
1007
   */
1008 3
  public function getRandomValue()
1009
  {
1010 3
    return $this->randomValue();
1011
  }
1012
1013
  /**
1014
   * alias: for "Arrayy->randomValues()"
1015
   *
1016
   * @param int $number
1017
   *
1018
   * @return Arrayy (Immutable)
1019
   */
1020 6
  public function getRandomValues($number)
1021
  {
1022 6
    return $this->randomValues($number);
1023
  }
1024
1025
  /**
1026
   * Group values from a array according to the results of a closure.
1027
   *
1028
   * @param string $grouper a callable function name
1029
   * @param bool   $saveKeys
1030
   *
1031
   * @return Arrayy (Immutable)
1032
   */
1033 3
  public function group($grouper, $saveKeys = false)
1034
  {
1035 3
    $array = (array)$this->array;
1036 3
    $result = array();
1037
1038
    // Iterate over values, group by property/results from closure
1039 3
    foreach ($array as $key => $value) {
1040 3
      $groupKey = is_callable($grouper) ? $grouper($value, $key) : $this->get($grouper, null, $value);
1041 3
      $newValue = $this->get($groupKey, null, $result);
1042
1043
      // Add to results
1044 3
      if ($groupKey !== null) {
1045 2
        if ($saveKeys) {
1046 1
          $result[$groupKey] = $newValue;
1047 1
          $result[$groupKey][$key] = $value;
1048
        } else {
1049 1
          $result[$groupKey] = $newValue;
1050 3
          $result[$groupKey][] = $value;
1051
        }
1052
      }
1053
1054
    }
1055
1056 3
    return static::create($result);
1057
  }
1058
1059
  /**
1060
   * Check if an array has a given key.
1061
   *
1062
   * @param mixed $key
1063
   *
1064
   * @return bool
1065
   */
1066 19
  public function has($key)
1067
  {
1068
    // Generate unique string to use as marker.
1069 19
    $unFound = (string)uniqid('arrayy', true);
1070
1071 19
    return $this->get($key, $unFound) !== $unFound;
1072
  }
1073
1074
  /**
1075
   * Implodes an array.
1076
   *
1077
   * @param string $with What to implode it with
1078
   *
1079
   * @return string
1080
   */
1081 23
  public function implode($with = '')
1082
  {
1083 23
    return implode($with, $this->array);
1084
  }
1085
1086
  /**
1087
   * Given a list and an iterate-function that returns
1088
   * a key for each element in the list (or a property name),
1089
   * returns an object with an index of each item.
1090
   *
1091
   * Just like groupBy, but for when you know your keys are unique.
1092
   *
1093
   * @param mixed $key
1094
   *
1095
   * @return Arrayy (Immutable)
1096
   */
1097 3
  public function indexBy($key)
1098
  {
1099 3
    $results = array();
1100
1101 3
    foreach ($this->array as $a) {
1102 3
      if (isset($a[$key])) {
1103 3
        $results[$a[$key]] = $a;
1104
      }
1105
    }
1106
1107 3
    return static::create($results);
1108
  }
1109
1110
  /**
1111
   * alias: for "Arrayy->searchIndex()"
1112
   *
1113
   * @param mixed $value Value to search for
1114
   *
1115
   * @return mixed
1116
   */
1117 4
  public function indexOf($value)
1118
  {
1119 4
    return $this->searchIndex($value);
1120
  }
1121
1122
  /**
1123
   * Get everything but the last..$to items.
1124
   *
1125
   * @param int $to
1126
   *
1127
   * @return Arrayy (Immutable)
1128
   */
1129 12
  public function initial($to = 1)
1130
  {
1131 12
    $slice = count($this->array) - $to;
1132
1133 12
    return $this->firstsImmutable($slice);
1134
  }
1135
1136
  /**
1137
   * Internal mechanics of remove method.
1138
   *
1139
   * @param $key
1140
   *
1141
   * @return boolean
1142
   */
1143 18
  protected function internalRemove($key)
1144
  {
1145
    // Explode keys
1146 18
    $keys = explode('.', $key);
1147
1148
    // Crawl though the keys
1149 18
    while (count($keys) > 1) {
1150
      $key = array_shift($keys);
1151
1152
      if (!$this->has($key)) {
1153
        return false;
1154
      }
1155
1156
      $this->array = &$this->array[$key];
1157
    }
1158
1159 18
    $key = array_shift($keys);
1160
1161 18
    unset($this->array[$key]);
1162
1163 18
    return true;
1164
  }
1165
1166
  /**
1167
   * Internal mechanic of set method.
1168
   *
1169
   * @param string $key
1170
   * @param mixed $value
1171
   *
1172
   * @return bool
1173
   */
1174 17
  protected function internalSet($key, $value)
1175
  {
1176 17
    if (null === $key) {
1177
      return false;
1178
    }
1179
1180
    // init
1181 17
    $array = &$this->array;
1182
1183
    // Explode the keys
1184 17
    $keys = explode('.', $key);
1185
1186
    // Crawl through the keys
1187 17
    while (count($keys) > 1) {
1188 1
      $key = array_shift($keys);
1189
      // If the key doesn't exist at this depth, we will just create an empty array
1190
      // to hold the next value, allowing us to create the arrays to hold final
1191
      // values at the correct depth. Then we'll keep digging into the array.
1192 1
      if (! isset($array[$key]) || ! is_array($array[$key])) {
1193
        $array[$key] = array();
1194
      }
1195 1
      $array = &$array[$key];
1196
    }
1197
1198 17
    $array[array_shift($keys)] = $value;
1199
1200 17
    return true;
1201
  }
1202
1203
  /**
1204
   * Return an array with all elements found in input array.
1205
   *
1206
   * @param array $search
1207
   *
1208
   * @return Arrayy (Immutable)
1209
   */
1210 2
  public function intersection(array $search)
1211
  {
1212 2
    $result = array_values(array_intersect($this->array, $search));
1213
1214 2
    return static::create($result);
1215
  }
1216
1217
  /**
1218
   * Return a boolean flag which indicates whether the two input arrays have any common elements.
1219
   *
1220
   * @param array $search
1221
   *
1222
   * @return bool
1223
   */
1224 1
  public function intersects(array $search)
1225
  {
1226 1
    return count($this->intersection($search)->array) > 0;
1227
  }
1228
1229
  /** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
1230
  /**
1231
   * Invoke a function on all of an array's values.
1232
   *
1233
   * @param mixed $callable
1234
   * @param mixed $arguments
1235
   *
1236
   * @return Arrayy (Immutable)
1237
   */
1238 1
  public function invoke($callable, $arguments = array())
1239
  {
1240
    // If one argument given for each iteration, create an array for it.
1241 1
    if (!is_array($arguments)) {
1242 1
      $arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray();
1243
    }
1244
1245
    // If the callable has arguments, pass them.
1246 1
    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...
1247 1
      $array = array_map($callable, $this->array, $arguments);
1248
    } else {
1249 1
      $array = array_map($callable, $this->array);
1250
    }
1251
1252 1
    return static::create($array);
1253
  }
1254
1255
  /**
1256
   * Check whether array is associative or not.
1257
   *
1258
   * @return bool Returns true if associative, false otherwise
1259
   */
1260 15
  public function isAssoc()
1261
  {
1262 15
    if ($this->isEmpty()) {
1263 3
      return false;
1264
    }
1265
1266 13
    foreach ($this->keys()->getArray() as $key) {
1267 13
      if (!is_string($key)) {
1268 13
        return false;
1269
      }
1270
    }
1271
1272 3
    return true;
1273
  }
1274
1275
  /**
1276
   * Check whether the array is empty or not.
1277
   *
1278
   * @return bool Returns true if empty, false otherwise
1279
   */
1280 25
  public function isEmpty()
1281
  {
1282 25
    return !$this->array;
1283
  }
1284
1285
  /**
1286
   * Check if the current array is a multi-array.
1287
   *
1288
   * @return bool
1289
   */
1290 14
  public function isMultiArray()
1291
  {
1292 14
    return !(count($this->array) === count($this->array, COUNT_RECURSIVE));
1293
  }
1294
1295
  /**
1296
   * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not.
1297
   *
1298
   * @return bool
1299
   */
1300 1
  public function isSequential()
1301
  {
1302 1
    return array_keys($this->array) === range(0, count($this->array) - 1);
1303
  }
1304
1305
  /**
1306
   * Check if the current array is equal to the given "$array" or not.
1307
   *
1308
   * @param array $array
1309
   *
1310
   * @return bool
1311
   */
1312
  public function isEqual(array $array)
1313
  {
1314
    return ($this->array === $array);
1315
  }
1316
1317
  /**
1318
   * Check whether array is numeric or not.
1319
   *
1320
   * @return bool Returns true if numeric, false otherwise
1321
   */
1322 5
  public function isNumeric()
1323
  {
1324 5
    if ($this->isEmpty()) {
1325 2
      return false;
1326
    }
1327
1328 4
    foreach ($this->keys() as $key) {
1329 4
      if (!is_int($key)) {
1330 4
        return false;
1331
      }
1332
    }
1333
1334 2
    return true;
1335
  }
1336
1337
  /**
1338
   * Get all keys from the current array.
1339
   *
1340
   * @return Arrayy (Immutable)
1341
   */
1342 22
  public function keys()
1343
  {
1344 22
    $array = array_keys((array)$this->array);
1345
1346 22
    return static::create($array);
1347
  }
1348
1349
  /**
1350
   * Get the last value from the current array.
1351
   *
1352
   * @return mixed Return null if there wasn't a element.
1353
   */
1354 4
  public function last()
1355
  {
1356 4
    $result = $this->pop();
1357
1358 4
    if (null === $result) {
1359 1
      return null;
1360
    } else {
1361 3
      return $result;
1362
    }
1363
  }
1364
1365
  /**
1366
   * Get the last value(s) from the current array.
1367
   *
1368
   * @param int|null $number
1369
   *
1370
   * @return Arrayy (Immutable)
1371
   */
1372 12
  public function lastsImmutable($number = null)
1373
  {
1374 12
    if ($number === null) {
1375 8
      $poppedValue = (array)$this->pop();
1376 8
      $arrayy = static::create($poppedValue);
1377
    } else {
1378 4
      $number = (int)$number;
1379 4
      $arrayy = $this->rest(-$number);
1380
    }
1381
1382 12
    return $arrayy;
1383
  }
1384
1385
  /**
1386
   * Get the last value(s) from the current array.
1387
   *
1388
   * @param int|null $number
1389
   *
1390
   * @return self (Mutable)
1391
   */
1392 12
  public function lastsMutable($number = null)
1393
  {
1394 12
    if ($number === null) {
1395 8
      $poppedValue = (array)$this->pop();
1396 8
      $this->array = static::create($poppedValue)->array;
1397
    } else {
1398 4
      $number = (int)$number;
1399 4
      $this->array = $this->rest(-$number)->array;
1400
    }
1401
1402 12
    return $this;
1403
  }
1404
1405
  /**
1406
   * Count the values from the current array.
1407
   *
1408
   * INFO: only a alias for "$arrayy->size()"
1409
   *
1410
   * @return int
1411
   */
1412 10
  public function length()
1413
  {
1414 10
    return $this->size();
1415
  }
1416
1417
  /**
1418
   * Apply the given function to the every element of the array,
1419
   * collecting the results.
1420
   *
1421
   * @param callable $callable
1422
   *
1423
   * @return Arrayy (Immutable) Arrayy object with modified elements
1424
   */
1425 4
  public function map($callable)
1426
  {
1427 4
    $result = array_map($callable, $this->array);
1428
1429 4
    return static::create($result);
1430
  }
1431
1432
  /**
1433
   * Check if all items in current array match a truth test.
1434
   *
1435
   * @param \Closure $closure
1436
   *
1437
   * @return bool
1438
   */
1439 9 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...
1440
  {
1441
    // Reduce the array to only booleans
1442 9
    $array = $this->each($closure);
1443
1444
    // Check the results
1445 9
    if (count($array) === 0) {
1446 2
      return true;
1447
    }
1448
1449 7
    $array = array_search(false, $array->toArray(), false);
1450
1451 7
    return is_bool($array);
1452
  }
1453
1454
  /**
1455
   * Check if any item in the current array matches a truth test.
1456
   *
1457
   * @param \Closure $closure
1458
   *
1459
   * @return bool
1460
   */
1461 9 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...
1462
  {
1463
    // Reduce the array to only booleans
1464 9
    $array = $this->each($closure);
1465
1466
    // Check the results
1467 9
    if (count($array) === 0) {
1468 2
      return true;
1469
    }
1470
1471 7
    $array = array_search(true, $array->toArray(), false);
1472
1473 7
    return is_int($array);
1474
  }
1475
1476
  /**
1477
   * Get the max value from an array.
1478
   *
1479
   * @return mixed
1480
   */
1481 10
  public function max()
1482
  {
1483 10
    if ($this->count() === 0) {
1484 1
      return false;
1485
    }
1486
1487 9
    return max($this->array);
1488
  }
1489
1490
  /**
1491
   * Merge the new $array into the current array.
1492
   *
1493
   * - keep key,value from the current array, also if the index is in the new $array
1494
   *
1495
   * @param array $array
1496
   * @param bool  $recursive
1497
   *
1498
   * @return Arrayy (Immutable)
1499
   */
1500 25 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...
1501
  {
1502 25
    if (true === $recursive) {
1503 4
      $result = array_replace_recursive($this->array, $array);
1504
    } else {
1505 21
      $result = array_replace($this->array, $array);
1506
    }
1507
1508 25
    return static::create($result);
1509
  }
1510
1511
  /**
1512
   * Merge the new $array into the current array.
1513
   *
1514
   * - replace duplicate assoc-keys from the current array with the key,values from the new $array
1515
   * - create new indexes
1516
   *
1517
   * @param array $array
1518
   * @param bool  $recursive
1519
   *
1520
   * @return Arrayy (Immutable)
1521
   */
1522 16 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...
1523
  {
1524 16
    if (true === $recursive) {
1525 4
      $result = array_merge_recursive($this->array, $array);
1526
    } else {
1527 12
      $result = array_merge($this->array, $array);
1528
    }
1529
1530 16
    return static::create($result);
1531
  }
1532
1533
  /**
1534
   * Merge the the current array into the $array.
1535
   *
1536
   * - use key,value from the new $array, also if the index is in the current array
1537
   *
1538
   * @param array $array
1539
   * @param bool  $recursive
1540
   *
1541
   * @return Arrayy (Immutable)
1542
   */
1543 16 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...
1544
  {
1545 16
    if (true === $recursive) {
1546 4
      $result = array_replace_recursive($array, $this->array);
1547
    } else {
1548 12
      $result = array_replace($array, $this->array);
1549
    }
1550
1551 16
    return static::create($result);
1552
  }
1553
1554
  /**
1555
   * Merge the current array into the new $array.
1556
   *
1557
   * - replace duplicate assoc-keys from new $array with the key,values from the current array
1558
   * - create new indexes
1559
   *
1560
   * @param array $array
1561
   * @param bool  $recursive
1562
   *
1563
   * @return Arrayy (Immutable)
1564
   */
1565 16 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...
1566
  {
1567 16
    if (true === $recursive) {
1568 4
      $result = array_merge_recursive($array, $this->array);
1569
    } else {
1570 12
      $result = array_merge($array, $this->array);
1571
    }
1572
1573 16
    return static::create($result);
1574
  }
1575
1576
  /**
1577
   * Get the min value from an array.
1578
   *
1579
   * @return mixed
1580
   */
1581 10
  public function min()
1582
  {
1583 10
    if ($this->count() === 0) {
1584 1
      return false;
1585
    }
1586
1587 9
    return min($this->array);
1588
  }
1589
1590
  /**
1591
   * Pad array to the specified size with a given value.
1592
   *
1593
   * @param int   $size  Size of the result array
1594
   * @param mixed $value Empty value by default
1595
   *
1596
   * @return Arrayy (Immutable) Arrayy object padded to $size with $value
1597
   */
1598 4
  public function pad($size, $value)
1599
  {
1600 4
    $result = array_pad($this->array, $size, $value);
1601
1602 4
    return static::create($result);
1603
  }
1604
1605
  /**
1606
   * Pop a specified value off the end of the current array.
1607
   *
1608
   * @return mixed The popped element from the current array.
1609
   */
1610 16
  public function pop()
1611
  {
1612 16
    return array_pop($this->array);
1613
  }
1614
1615
  /**
1616
   * Prepend a value to the current array.
1617
   *
1618
   * @param mixed $value
1619
   *
1620
   * @return self (Mutable) Return this Arrayy object, with the prepended value.
1621
   */
1622 7
  public function prepend($value)
1623
  {
1624 7
    array_unshift($this->array, $value);
1625
1626 7
    return $this;
1627
  }
1628
1629
  /**
1630
   * Push one or more values onto the end of array at once.
1631
   *
1632
   * @return self (Mutable) Return this Arrayy object, with pushed elements to the end of array.
1633
   */
1634 4 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...
1635
  {
1636 4
    if (func_num_args()) {
1637 4
      $args = array_merge(array(&$this->array), func_get_args());
1638 4
      call_user_func_array('array_push', $args);
1639
    }
1640
1641 4
    return $this;
1642
  }
1643
1644
  /**
1645
   * Get a random value from the current array.
1646
   *
1647
   * @param null|int $number how many values you will take?
1648
   *
1649
   * @return Arrayy (Mutable)
1650
   */
1651 16
  public function randomMutable($number = null)
1652
  {
1653 16
    if ($this->count() === 0) {
1654
      return static::create();
1655
    }
1656
1657 16 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...
1658 6
      $arrayRandValue = (array)$this->array[array_rand($this->array)];
1659 6
      $this->array = $arrayRandValue;
1660
1661 6
      return $this;
1662
    }
1663
1664 11
    shuffle($this->array);
1665
1666 11
    return $this->firstsMutable($number);
1667
  }
1668
1669
  /**
1670
   * Get a random value from the current array.
1671
   *
1672
   * @param null|int $number how many values you will take?
1673
   *
1674
   * @return Arrayy (Immutable)
1675
   */
1676 17
  public function randomImmutable($number = null)
1677
  {
1678 17
    if ($this->count() === 0) {
1679
      return static::create();
1680
    }
1681
1682 17 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...
1683 14
      $arrayRandValue = (array)$this->array[array_rand($this->array)];
1684
1685 14
      return static::create($arrayRandValue);
1686
    }
1687
1688 5
    $arrayTmp = $this->array;
1689 5
    shuffle($arrayTmp);
1690
1691 5
    return self::create($arrayTmp)->firstsImmutable($number);
1692
  }
1693
1694
  /**
1695
   * Pick a random key/index from the keys of this array.
1696
   *
1697
   *
1698
   * @return mixed get a key/index or null if there wasn't a key/index
1699
   *
1700
   * @throws \RangeException If array is empty
1701
   */
1702 4
  public function randomKey()
1703
  {
1704 4
    $result = $this->randomKeys(1);
1705
1706 4
    if (!isset($result[0])) {
1707
      $result[0] = null;
1708
    }
1709
1710 4
    return $result[0];
1711
  }
1712
1713
  /**
1714
   * Pick a given number of random keys/indexes out of this array.
1715
   *
1716
   * @param int $number The number of keys/indexes (should be <= $this->count())
1717
   *
1718
   * @return Arrayy (Immutable)
1719
   *
1720
   * @throws \RangeException If array is empty
1721
   */
1722 14
  public function randomKeys($number)
1723
  {
1724 14
    $number = (int)$number;
1725 14
    $count = $this->count();
1726
1727 14
    if ($number === 0 || $number > $count) {
1728 3
      throw new \RangeException(
1729
          sprintf(
1730 3
              'Number of requested keys (%s) must be equal or lower than number of elements in this array (%s)',
1731
              $number,
1732
              $count
1733
          )
1734
      );
1735
    }
1736
1737 11
    $result = (array)array_rand($this->array, $number);
1738
1739 11
    return static::create($result);
1740
  }
1741
1742
  /**
1743
   * Pick a random value from the values of this array.
1744
   *
1745
   * @return mixed get a random value or null if there wasn't a value
1746
   */
1747 4
  public function randomValue()
1748
  {
1749 4
    $result = $this->randomImmutable();
1750
1751 4
    if (!isset($result[0])) {
1752
      $result[0] = null;
1753
    }
1754
1755 4
    return $result[0];
1756
  }
1757
1758
  /**
1759
   * Pick a given number of random values out of this array.
1760
   *
1761
   * @param int $number
1762
   *
1763
   * @return Arrayy (Mutable)
1764
   */
1765 7
  public function randomValues($number)
1766
  {
1767 7
    $number = (int)$number;
1768
1769 7
    return $this->randomMutable($number);
1770
  }
1771
1772
  /**
1773
   * Get a random value from an array, with the ability to skew the results.
1774
   *
1775
   * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
1776
   *
1777
   * @param array    $array
1778
   * @param null|int $number how many values you will take?
1779
   *
1780
   * @return Arrayy (Immutable)
1781
   */
1782 9
  public function randomWeighted(array $array, $number = null)
1783
  {
1784 9
    $options = array();
1785 9
    foreach ($array as $option => $weight) {
1786 9
      if ($this->searchIndex($option) !== false) {
1787 9
        for ($i = 0; $i < $weight; ++$i) {
1788 1
          $options[] = $option;
1789
        }
1790
      }
1791
    }
1792
1793 9
    return $this->mergeAppendKeepIndex($options)->randomImmutable($number);
1794
  }
1795
1796
  /**
1797
   * Reduce the current array via callable e.g. anonymous-function.
1798
   *
1799
   * @param mixed $callable
1800
   * @param array $init
1801
   *
1802
   * @return Arrayy (Immutable)
1803
   */
1804 3
  public function reduce($callable, array $init = array())
1805
  {
1806 3
    $result = array_reduce($this->array, $callable, $init);
1807
1808 3
    if ($result === null) {
1809
      $this->array = array();
1810
    } else {
1811 3
      $this->array = (array)$result;
1812
    }
1813
1814 3
    return static::create($this->array);
1815
  }
1816
1817
  /**
1818
   * Create a numerically re-indexed Arrayy object.
1819
   *
1820
   * @return self (Mutable) Return this Arrayy object, with re-indexed array-elements.
1821
   */
1822 9
  public function reindex()
1823
  {
1824 9
    $this->array = array_values($this->array);
1825
1826 9
    return $this;
1827
  }
1828
1829
  /**
1830
   * Return all items that fail the truth test.
1831
   *
1832
   * @param \Closure $closure
1833
   *
1834
   * @return Arrayy (Immutable)
1835
   */
1836 1 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...
1837
  {
1838 1
    $filtered = array();
1839
1840 1
    foreach ($this->array as $key => $value) {
1841 1
      if (!$closure($value, $key)) {
1842 1
        $filtered[$key] = $value;
1843
      }
1844
    }
1845
1846 1
    return static::create($filtered);
1847
  }
1848
1849
  /**
1850
   * Remove a value from the current array (optional using dot-notation).
1851
   *
1852
   * @param mixed $key
1853
   *
1854
   * @return Arrayy (Immutable)
1855
   */
1856 18
  public function remove($key)
1857
  {
1858
    // Recursive call
1859 18
    if (is_array($key)) {
1860
      foreach ($key as $k) {
1861
        $this->internalRemove($k);
1862
      }
1863
1864
      return static::create($this->array);
1865
    }
1866
1867 18
    $this->internalRemove($key);
1868
1869 18
    return static::create($this->array);
1870
  }
1871
1872
  /**
1873
   * Remove the first value from the current array.
1874
   *
1875
   * @return Arrayy (Immutable)
1876
   */
1877 7
  public function removeFirst()
1878
  {
1879 7
    array_shift($this->array);
1880
1881 7
    return static::create($this->array);
1882
  }
1883
1884
  /**
1885
   * Remove the last value from the current array.
1886
   *
1887
   * @return Arrayy (Immutable)
1888
   */
1889 7
  public function removeLast()
1890
  {
1891 7
    array_pop($this->array);
1892
1893 7
    return static::create($this->array);
1894
  }
1895
1896
  /**
1897
   * Removes a particular value from an array (numeric or associative).
1898
   *
1899
   * @param mixed $value
1900
   *
1901
   * @return Arrayy (Immutable)
1902
   */
1903 7
  public function removeValue($value)
1904
  {
1905 7
    $isNumericArray = true;
1906 7
    foreach ($this->array as $key => $item) {
1907 6
      if ($item === $value) {
1908 6
        if (!is_int($key)) {
1909
          $isNumericArray = false;
1910
        }
1911 6
        unset($this->array[$key]);
1912
      }
1913
    }
1914
1915 7
    if ($isNumericArray) {
1916 7
      $this->array = array_values($this->array);
1917
    }
1918
1919 7
    return static::create($this->array);
1920
  }
1921
1922
  /**
1923
   * Replace a key with a new key/value pair.
1924
   *
1925
   * @param $replace
1926
   * @param $key
1927
   * @param $value
1928
   *
1929
   * @return Arrayy (Immutable)
1930
   */
1931 2
  public function replace($replace, $key, $value)
1932
  {
1933 2
    $this->remove($replace);
1934
1935 2
    return $this->set($key, $value);
1936
  }
1937
1938
  /**
1939
   * Create an array using the current array as values and the other array as keys.
1940
   *
1941
   * @param array $keys Keys array
1942
   *
1943
   * @return Arrayy (Immutable) Arrayy object with keys from the other array.
1944
   */
1945 2
  public function replaceAllKeys(array $keys)
1946
  {
1947 2
    $result = array_combine($keys, $this->array);
1948
1949 2
    return static::create($result);
1950
  }
1951
1952
  /**
1953
   * Create an array using the current array as keys and the other array as values.
1954
   *
1955
   * @param array $array Values array
1956
   *
1957
   * @return Arrayy (Immutable) Arrayy object with values from the other array.
1958
   */
1959 2
  public function replaceAllValues(array $array)
1960
  {
1961 2
    $result = array_combine($this->array, $array);
1962
1963 2
    return static::create($result);
1964
  }
1965
1966
  /**
1967
   * Replace the keys in an array with another set.
1968
   *
1969
   * @param array $keys An array of keys matching the array's size
1970
   *
1971
   * @return Arrayy (Immutable)
1972
   */
1973 1
  public function replaceKeys(array $keys)
1974
  {
1975 1
    $values = array_values($this->array);
1976 1
    $result = array_combine($keys, $values);
1977
1978 1
    return static::create($result);
1979
  }
1980
1981
  /**
1982
   * Replace the first matched value in an array.
1983
   *
1984
   * @param mixed $search
1985
   * @param mixed $replacement
1986
   *
1987
   * @return Arrayy (Immutable)
1988
   */
1989 3
  public function replaceOneValue($search, $replacement = '')
1990
  {
1991 3
    $array = $this->array;
1992 3
    $key = array_search($search, $array, true);
1993
1994 3
    if ($key !== false) {
1995 3
      $array[$key] = $replacement;
1996
    }
1997
1998 3
    return static::create($array);
1999
  }
2000
2001
  /**
2002
   * Replace values in the current array.
2003
   *
2004
   * @param string $search      The string to replace.
2005
   * @param string $replacement What to replace it with.
2006
   *
2007
   * @return Arrayy (Immutable)
2008
   */
2009 1
  public function replaceValues($search, $replacement = '')
2010
  {
2011 1
    $array = $this->each(
2012
        function ($value) use ($search, $replacement) {
2013 1
          return UTF8::str_replace($search, $replacement, $value);
2014 1
        }
2015
    );
2016
2017 1
    return $array;
2018
  }
2019
2020
  /**
2021
   * Get the last elements from index $from until the end of this array.
2022
   *
2023
   * @param int $from
2024
   *
2025
   * @return Arrayy (Immutable)
2026
   */
2027 16
  public function rest($from = 1)
2028
  {
2029 16
    $result = array_splice($this->array, $from);
2030
2031 16
    return static::create($result);
2032
  }
2033
2034
  /**
2035
   * Return the array in the reverse order.
2036
   *
2037
   * @return self (Mutable) Return this Arrayy object.
2038
   */
2039 7
  public function reverse()
2040
  {
2041 7
    $this->array = array_reverse($this->array);
2042
2043 7
    return $this;
2044
  }
2045
2046
  /**
2047
   * Search for the first index of the current array via $value.
2048
   *
2049
   * @param mixed $value
2050
   *
2051
   * @return mixed
2052
   */
2053 20
  public function searchIndex($value)
2054
  {
2055 20
    return array_search($value, $this->array, true);
2056
  }
2057
2058
  /**
2059
   * Search for the value of the current array via $index.
2060
   *
2061
   * @param mixed $index
2062
   *
2063
   * @return Arrayy (Immutable) will return a empty Arrayy if the value wasn't found
2064
   */
2065 7
  public function searchValue($index)
2066
  {
2067
    // init
2068 7
    $return = array();
2069
2070 7
    if (null !== $index) {
2071 7
      $keyExists = isset($this->array[$index]);
2072
2073 7
      if ($keyExists !== false) {
2074 5
        $return = array($this->array[$index]);
2075
      }
2076
    }
2077
2078 7
    return static::create($return);
2079
  }
2080
2081
  /**
2082
   * Set a value for the current array (optional using dot-notation).
2083
   *
2084
   * @param string $key   The key to set
2085
   * @param mixed  $value Its value
2086
   *
2087
   * @return Arrayy (Immutable)
2088
   */
2089 17
  public function set($key, $value)
2090
  {
2091 17
    $this->internalSet($key, $value);
2092
2093 17
    return static::create($this->array);
2094
  }
2095
2096
  /**
2097
   * Get a value from a array and set it if it was not.
2098
   *
2099
   * WARNING: this method only set the value, if the $key is not already set
2100
   *
2101
   * @param string $key      The key
2102
   * @param mixed  $fallback The default value to set if it isn't
2103
   *
2104
   * @return mixed (Mutable)
2105
   */
2106 10
  public function setAndGet($key, $fallback = null)
2107
  {
2108
    // If the key doesn't exist, set it
2109 10
    if (!$this->has($key)) {
2110 5
      $this->array = $this->set($key, $fallback)->getArray();
2111
    }
2112
2113 10
    return $this->get($key);
2114
  }
2115
2116
  /**
2117
   * Shifts a specified value off the beginning of array.
2118
   *
2119
   * @return mixed A shifted element from the current array.
2120
   */
2121 4
  public function shift()
2122
  {
2123 4
    return array_shift($this->array);
2124
  }
2125
2126
  /**
2127
   * Shuffle the current array.
2128
   *
2129
   * @return Arrayy (Immutable)
2130
   */
2131 1
  public function shuffle()
2132
  {
2133 1
    $array = $this->array;
2134
2135 1
    shuffle($array);
2136
2137 1
    return static::create($array);
2138
  }
2139
2140
  /**
2141
   * Get the size of an array.
2142
   *
2143
   * @return int
2144
   */
2145 110
  public function size()
2146
  {
2147 110
    return count($this->array);
2148
  }
2149
2150
  /**
2151
   * Extract a slice of the array.
2152
   *
2153
   * @param int      $offset       Slice begin index
2154
   * @param int|null $length       Length of the slice
2155
   * @param bool     $preserveKeys Whether array keys are preserved or no
2156
   *
2157
   * @return static A slice of the original array with length $length
2158
   */
2159 4
  public function slice($offset, $length = null, $preserveKeys = false)
2160
  {
2161 4
    $result = array_slice($this->array, $offset, $length, $preserveKeys);
2162
2163 4
    return static::create($result);
2164
  }
2165
2166
  /**
2167
   * Sort the current array and optional you can keep the keys.
2168
   *
2169
   * @param integer $direction use SORT_ASC or SORT_DESC
2170
   * @param integer $strategy
2171
   * @param bool       $keepKeys
2172
   *
2173
   * @return self (Mutable) Return this Arrayy object.
2174
   */
2175 19
  public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2176
  {
2177 19
    $this->sorting($this->array, $direction, $strategy, $keepKeys);
2178
2179 19
    return $this;
2180
  }
2181
2182
  /**
2183
   * Sort the current array by key.
2184
   *
2185
   * @link http://php.net/manual/en/function.ksort.php
2186
   * @link http://php.net/manual/en/function.krsort.php
2187
   *
2188
   * @param int|string $direction use SORT_ASC or SORT_DESC
2189
   * @param int        $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2190
   *
2191
   * @return self (Mutable) Return this Arrayy object.
2192
   */
2193 18
  public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR)
2194
  {
2195 18
    $this->sorterKeys($this->array, $direction, $strategy);
2196
2197 18
    return $this;
2198
  }
2199
2200
  /**
2201
   * Sort the current array by value.
2202
   *
2203
   * @param int $direction use SORT_ASC or SORT_DESC
2204
   * @param int $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2205
   *
2206
   * @return Arrayy (Immutable)
2207
   */
2208 1
  public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2209
  {
2210 1
    return $this->sort($direction, $strategy, true);
2211
  }
2212
2213
  /**
2214
   * Sort the current array by value.
2215
   *
2216
   * @param int $direction use SORT_ASC or SORT_DESC
2217
   * @param int $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2218
   *
2219
   * @return Arrayy (Immutable)
2220
   */
2221 1
  public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2222
  {
2223 1
    return $this->sort($direction, $strategy, false);
2224
  }
2225
2226
  /**
2227
   * Sort a array by value, by a closure or by a property.
2228
   *
2229
   * - If the sorter is null, the array is sorted naturally.
2230
   * - Associative (string) keys will be maintained, but numeric keys will be re-indexed.
2231
   *
2232
   * @param null       $sorter
2233
   * @param string|int $direction
2234
   * @param int        $strategy
2235
   *
2236
   * @return Arrayy (Immutable)
2237
   */
2238 1
  public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2239
  {
2240 1
    $array = (array)$this->array;
2241 1
    $direction = $this->getDirection($direction);
2242
2243
    // Transform all values into their results.
2244 1
    if ($sorter) {
2245 1
      $arrayy = new self($array);
2246
2247 1
      $that = $this;
2248 1
      $results = $arrayy->each(
2249
          function ($value) use ($sorter, $that) {
2250 1
            return is_callable($sorter) ? $sorter($value) : $that->get($sorter, null, $value);
2251 1
          }
2252
      );
2253
2254 1
      $results = $results->getArray();
2255
    } else {
2256 1
      $results = $array;
2257
    }
2258
2259
    // Sort by the results and replace by original values
2260 1
    array_multisort($results, $direction, $strategy, $array);
2261
2262 1
    return static::create($array);
2263
  }
2264
2265
  /**
2266
   * sorting keys
2267
   *
2268
   * @param array $elements
2269
   * @param int   $direction
2270
   * @param int   $strategy
2271
   */
2272 18
  protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2273
  {
2274 18
    $direction = $this->getDirection($direction);
2275
2276
    switch ($direction) {
2277 18
      case 'desc':
2278 18
      case SORT_DESC:
2279 6
        krsort($elements, $strategy);
2280 6
        break;
2281 13
      case 'asc':
2282 13
      case SORT_ASC:
2283
      default:
2284 13
        ksort($elements, $strategy);
2285
    }
2286 18
  }
2287
2288
  /**
2289
   * @param array      &$elements
2290
   * @param int|string $direction
2291
   * @param int        $strategy
2292
   * @param bool       $keepKeys
2293
   */
2294 19
  protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2295
  {
2296 19
    $direction = $this->getDirection($direction);
2297
2298 19
    if (!$strategy) {
2299 19
      $strategy = SORT_REGULAR;
2300
    }
2301
2302
    switch ($direction) {
2303 19
      case 'desc':
2304 19
      case SORT_DESC:
2305 9
        if ($keepKeys) {
2306 5
          arsort($elements, $strategy);
2307
        } else {
2308 4
          rsort($elements, $strategy);
2309
        }
2310 9
        break;
2311 10
      case 'asc':
2312 10
      case SORT_ASC:
2313
      default:
2314 10
        if ($keepKeys) {
2315 4
          asort($elements, $strategy);
2316
        } else {
2317 6
          sort($elements, $strategy);
2318
        }
2319
    }
2320 19
  }
2321
2322
  /**
2323
   * Split an array in the given amount of pieces.
2324
   *
2325
   * @param int  $numberOfPieces
2326
   * @param bool $keepKeys
2327
   *
2328
   * @return Arrayy (Immutable)
2329
   */
2330 1
  public function split($numberOfPieces = 2, $keepKeys = false)
2331
  {
2332 1
    if (count($this->array) === 0) {
2333 1
      $result = array();
2334
    } else {
2335 1
      $numberOfPieces = (int)$numberOfPieces;
2336 1
      $splitSize = ceil(count($this->array) / $numberOfPieces);
2337 1
      $result = array_chunk($this->array, $splitSize, $keepKeys);
2338
    }
2339
2340 1
    return static::create($result);
2341
  }
2342
2343
  /**
2344
   * alias: for "Arrayy->getArray()"
2345
   */
2346 153
  public function toArray()
2347
  {
2348 153
    return $this->getArray();
2349
  }
2350
2351
  /**
2352
   * Convert the current array to JSON.
2353
   *
2354
   * @param null $options e.g. JSON_PRETTY_PRINT
2355
   *
2356
   * @return string
2357
   */
2358 5
  public function toJson($options = null)
2359
  {
2360 5
    return UTF8::json_encode($this->array, $options);
2361
  }
2362
2363
  /**
2364
   * Implodes array to a string with specified separator.
2365
   *
2366
   * @param string $separator The element's separator
2367
   *
2368
   * @return string The string representation of array, separated by ","
2369
   */
2370 15
  public function toString($separator = ',')
2371
  {
2372 15
    return $this->implode($separator);
2373
  }
2374
2375
  /**
2376
   * Return a duplicate free copy of the current array.
2377
   *
2378
   * @return Arrayy (Mutable)
2379
   */
2380 8
  public function unique()
2381
  {
2382 8
    $this->array = array_reduce(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_reduce($this->arra...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...
2383 8
        $this->array,
2384 8
        function ($resultArray, $value) {
2385 7
          if (in_array($value, $resultArray, true) === false) {
2386 7
            $resultArray[] = $value;
2387
          }
2388
2389 7
          return $resultArray;
2390 8
        },
2391 8
        array()
2392
    );
2393
2394 8
    if ($this->array === null) {
2395
      $this->array = array();
2396
    } else {
2397 8
      $this->array = (array)$this->array;
2398
    }
2399
2400 8
    return $this;
2401
  }
2402
2403
  /**
2404
   * Prepends one or more values to the beginning of array at once.
2405
   *
2406
   * @return self (Mutable) Return this Arrayy object, with prepended elements to the beginning of array.
2407
   */
2408 4 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...
2409
  {
2410 4
    if (func_num_args()) {
2411 4
      $args = array_merge(array(&$this->array), func_get_args());
2412 4
      call_user_func_array('array_unshift', $args);
2413
    }
2414
2415 4
    return $this;
2416
  }
2417
2418
  /**
2419
   * Get all values from a array.
2420
   *
2421
   * @return Arrayy (Immutable)
2422
   */
2423 1
  public function values()
2424
  {
2425 1
    $array = array_values((array)$this->array);
2426
2427 1
    return static::create($array);
2428
  }
2429
2430
  /**
2431
   * Apply the given function to every element in the array, discarding the results.
2432
   *
2433
   * @param callable $callable
2434
   * @param bool     $recursive Whether array will be walked recursively or no
2435
   *
2436
   * @return self (Mutable) Return this Arrayy object, with modified elements
2437
   */
2438 9
  public function walk($callable, $recursive = false)
2439
  {
2440 9
    if (true === $recursive) {
2441 4
      array_walk_recursive($this->array, $callable);
2442
    } else {
2443 5
      array_walk($this->array, $callable);
2444
    }
2445
2446 9
    return $this;
2447
  }
2448
2449
2450
}
2451