Completed
Push — master ( 6e9c6b...e47a30 )
by Lars
03:00
created

Arrayy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 721
  public function __construct($array = array())
29
  {
30 721
    $array = $this->fallbackForArray($array);
31
32 719
    $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 719
  }
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 16
  public function __toString()
96
  {
97 16
    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 35
  public function offsetExists($offset)
166
  {
167 35
    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 4
    } 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 3
    }
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 2
  public function unserialize($string)
227
  {
228 2
    $this->array = unserialize($string);
229
230 2
    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 2
    }
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 3
    }
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
        }
299 8
    );
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 13
        array_map(
338
            array(
339 13
                new UTF8(),
340 13
                'strtolower',
341 13
            ),
342 13
            $this->array
343 13
        ),
344
        true
345 13
    );
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 $this->offsetExists($key);
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 447
  public static function create($array = array())
369
  {
370 447
    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 4
    }
418
419 4
    return $array;
420
  }
421
422
  /**
423
   * Create an new Arrayy object via string.
424
   *
425
   * @param string      $str       The input string.
426
   * @param string|null $delimiter The boundary string.
427
   * @param string|null $regEx     Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be used.
428
   *
429
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
430
   */
431 8
  public static function createFromString($str, $delimiter, $regEx = null)
432
  {
433 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...
434 1
      preg_match_all($regEx, $str, $array);
435
436 1
      if (count($array) > 0) {
437 1
        $array = $array[0];
438 1
      }
439
440 1
    } else {
441 7
      $array = explode($delimiter, $str);
442
    }
443
444
    // trim all string in the array
445 8
    array_walk(
446 8
        $array,
447
        function (&$val) {
448
          /** @noinspection ReferenceMismatchInspection */
449 8
          if (is_string($val)) {
450 8
            $val = trim($val);
451 8
          }
452 8
        }
453 8
    );
454
455 8
    return static::create($array);
456
  }
457
458
  /**
459
   * Create an new instance containing a range of elements.
460
   *
461
   * @param mixed $low  First value of the sequence
462
   * @param mixed $high The sequence is ended upon reaching the end value
463
   * @param int   $step Used as the increment between elements in the sequence
464
   *
465
   * @return Arrayy (Immutable) Returns an new instance of the Arrayy object.
466
   */
467 1
  public static function createWithRange($low, $high, $step = 1)
468
  {
469 1
    return static::create(range($low, $high, $step));
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 current multi-dimensional array.
520
   *
521
   * @param array      $array
522
   * @param null|array $helperVariableForRecursion (only for internal usage)
523
   *
524
   * @return Arrayy (Immutable)
525
   */
526 1
  public function diffRecursive(array $array = array(), $helperVariableForRecursion = null)
527
  {
528 1
    $result = array();
529
530 1
    if ($helperVariableForRecursion !== null && is_array($helperVariableForRecursion) === true) {
531 1
      $arrayForTheLoop = $helperVariableForRecursion;
532 1
    } else {
533 1
      $arrayForTheLoop = $this->array;
534
    }
535
536 1
    foreach ($arrayForTheLoop as $key => $value) {
537 1
      if (array_key_exists($key, $array)) {
538 1
        if (is_array($value)) {
539 1
          $recursiveDiff = $this->diffRecursive($array[$key], $value);
540 1
          if (count($recursiveDiff)) {
541 1
            $result[$key] = $recursiveDiff;
542 1
          }
543 1
        } else {
544 1
          if ($value != $array[$key]) {
545 1
            $result[$key] = $value;
546 1
          }
547
        }
548 1
      } else {
549 1
        $result[$key] = $value;
550
      }
551 1
    }
552
553 1
    return static::create($result);
554
  }
555
556
  /**
557
   * Return values that are only in the new $array.
558
   *
559
   * @param array $array
560
   *
561
   * @return Arrayy (Immutable)
562
   */
563 8
  public function diffReverse(array $array = array())
564
  {
565 8
    $result = array_diff($array, $this->array);
566
567 8
    return static::create($result);
568
  }
569
570
  /**
571
   * Divide an array into two arrays. One with keys and the other with values.
572
   *
573
   * @return Arrayy (Immutable)
574
   */
575 1
  public function divide()
576
  {
577 1
    return static::create(
578
        array(
579 1
            $this->keys(),
580 1
            $this->values(),
581
        )
582 1
    );
583
  }
584
585
  /**
586
   * Iterate over the current array and modify the array's value.
587
   *
588
   * @param \Closure $closure
589
   *
590
   * @return Arrayy (Immutable)
591
   */
592 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...
593
  {
594 22
    $array = $this->array;
595
596 22
    foreach ($array as $key => &$value) {
597 18
      $value = $closure($value, $key);
598 22
    }
599
600 22
    return static::create($array);
601
  }
602
603
  /**
604
   * Check if a value is in the current array using a closure.
605
   *
606
   * @param \Closure $closure
607
   *
608
   * @return bool Returns true if the given value is found, false otherwise
609
   */
610 4
  public function exists(\Closure $closure)
611
  {
612 4
    $isExists = false;
613 4
    foreach ($this->array as $key => $value) {
614 3
      if ($closure($value, $key)) {
615 1
        $isExists = true;
616 1
        break;
617
      }
618 4
    }
619
620 4
    return $isExists;
621
  }
622
623
  /**
624
   * create a fallback for array
625
   *
626
   * 1. use the current array, if it's a array
627
   * 2. call "getArray()" on object, if there is a "Arrayy"-object
628
   * 3. fallback to empty array, if there is nothing
629
   * 4. call "createFromObject()" on object, if there is a "ArrayAccess"-object
630
   * 5. call "__toArray()" on object, if the method exists
631
   * 6. cast a string or object with "__toString()" into an array
632
   * 7. throw a "InvalidArgumentException"-Exception
633
   *
634
   * @param $array
635
   *
636
   * @return array
637
   *
638
   * @throws \InvalidArgumentException
639
   */
640 721
  protected function fallbackForArray(&$array)
641
  {
642 721
    if (is_array($array)) {
643 718
      return $array;
644
    }
645
646 9
    if ($array instanceof self) {
647
      return $array->getArray();
648
    }
649
650 9
    if (!$array) {
651 6
      return array();
652
    }
653
654 8
    if ($array instanceof ArrayAccess) {
655
      /** @noinspection ReferenceMismatchInspection */
656
      return self::createFromObject($array);
657
    }
658
659 8
    if (is_object($array) && method_exists($array, '__toArray')) {
660
      return (array)$array->__toArray();
661
    }
662
663
    /** @noinspection ReferenceMismatchInspection */
664
    if (
665 8
        is_string($array)
666
        ||
667 2
        (is_object($array) && method_exists($array, '__toString'))
668 8
    ) {
669 6
      return (array)$array;
670
    }
671
672 2
    throw new \InvalidArgumentException(
673
        'Passed value should be a array'
674 2
    );
675
  }
676
677
  /**
678
   * Find all items in an array that pass the truth test.
679
   *
680
   * @param \Closure|null $closure
681
   *
682
   * @return Arrayy (Immutable)
683
   */
684 9
  public function filter($closure = null)
685
  {
686 9
    if (!$closure) {
687 1
      return $this->clean();
688
    }
689
690 9
    $array = array_filter($this->array, $closure);
691
692 9
    return static::create($array);
693
  }
694
695
  /**
696
   * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property
697
   * within that.
698
   *
699
   * @param        $property
700
   * @param        $value
701
   * @param string $comparisonOp
702
   *                            'eq' (equals),<br />
703
   *                            'gt' (greater),<br />
704
   *                            'gte' || 'ge' (greater or equals),<br />
705
   *                            'lt' (less),<br />
706
   *                            'lte' || 'le' (less or equals),<br />
707
   *                            'ne' (not equals),<br />
708
   *                            'contains',<br />
709
   *                            'notContains',<br />
710
   *                            'newer' (via strtotime),<br />
711
   *                            'older' (via strtotime),<br />
712
   *
713
   * @return Arrayy (Immutable)
714
   */
715 1
  public function filterBy($property, $value, $comparisonOp = null)
716
  {
717 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...
718 1
      $comparisonOp = is_array($value) ? 'contains' : 'eq';
719 1
    }
720
721
    $ops = array(
722
        'eq'          => function ($item, $prop, $value) {
723 1
          return $item[$prop] === $value;
724 1
        },
725
        'gt'          => function ($item, $prop, $value) {
726
          return $item[$prop] > $value;
727 1
        },
728
        'ge'          => function ($item, $prop, $value) {
729
          return $item[$prop] >= $value;
730 1
        },
731
        'gte'         => function ($item, $prop, $value) {
732
          return $item[$prop] >= $value;
733 1
        },
734
        'lt'          => function ($item, $prop, $value) {
735 1
          return $item[$prop] < $value;
736 1
        },
737
        'le'          => function ($item, $prop, $value) {
738
          return $item[$prop] <= $value;
739 1
        },
740
        'lte'         => function ($item, $prop, $value) {
741
          return $item[$prop] <= $value;
742 1
        },
743
        'ne'          => function ($item, $prop, $value) {
744
          return $item[$prop] !== $value;
745 1
        },
746
        'contains'    => function ($item, $prop, $value) {
747 1
          return in_array($item[$prop], (array)$value, true);
748 1
        },
749
        'notContains' => function ($item, $prop, $value) {
750
          return !in_array($item[$prop], (array)$value, true);
751 1
        },
752
        'newer'       => function ($item, $prop, $value) {
753
          return strtotime($item[$prop]) > strtotime($value);
754 1
        },
755
        'older'       => function ($item, $prop, $value) {
756
          return strtotime($item[$prop]) < strtotime($value);
757 1
        },
758 1
    );
759
760 1
    $result = array_values(
761 1
        array_filter(
762 1
            (array)$this->array,
763
            function ($item) use (
764 1
                $property,
765 1
                $value,
766 1
                $ops,
767 1
                $comparisonOp
768
            ) {
769 1
              $item = (array)$item;
770 1
              $itemArrayy = new Arrayy($item);
771 1
              $item[$property] = $itemArrayy->get($property, array());
772
773 1
              return $ops[$comparisonOp]($item, $property, $value);
774
            }
775 1
        )
776 1
    );
777
778 1
    return static::create($result);
779
  }
780
781
  /**
782
   * Find the first item in an array that passes the truth test,
783
   *  otherwise return false
784
   *
785
   * @param \Closure $closure
786
   *
787
   * @return mixed|false false if we did not find the value
788
   */
789 8
  public function find(\Closure $closure)
790
  {
791 8
    foreach ($this->array as $key => $value) {
792 6
      if ($closure($value, $key)) {
793 5
        return $value;
794
      }
795 5
    }
796
797 3
    return false;
798
  }
799
800
  /**
801
   * find by ...
802
   *
803
   * @param        $property
804
   * @param        $value
805
   * @param string $comparisonOp
806
   *
807
   * @return Arrayy (Immutable)
808
   */
809
  public function findBy($property, $value, $comparisonOp = 'eq')
810
  {
811
    return $this->filterBy($property, $value, $comparisonOp);
812
  }
813
814
  /**
815
   * Get the first value from the current array.
816
   *
817
   * @return mixed Return null if there wasn't a element.
818
   */
819 13
  public function first()
820
  {
821 13
    $result = array_shift($this->array);
822
823 13
    if (null === $result) {
824 3
      return null;
825
    } else {
826 10
      return $result;
827
    }
828
  }
829
830
  /**
831
   * Get the first value(s) from the current array.
832
   *
833
   * @param int|null $number how many values you will take?
834
   *
835
   * @return Arrayy (Immutable)
836
   */
837 28
  public function firstsImmutable($number = null)
838
  {
839 28
    if ($number === null) {
840 7
      $array = (array)array_shift($this->array);
841 7
    } else {
842 21
      $number = (int)$number;
843 21
      $array = array_splice($this->array, 0, $number, true);
844
    }
845
846 28
    return static::create($array);
847
  }
848
849
  /**
850
   * Get the first value(s) from the current array.
851
   *
852
   * @param int|null $number how many values you will take?
853
   *
854
   * @return self (Mutable)
855
   */
856 26
  public function firstsMutable($number = null)
857
  {
858 26
    if ($number === null) {
859 11
      $this->array = (array)array_shift($this->array);
860 11
    } else {
861 15
      $number = (int)$number;
862 15
      $this->array = array_splice($this->array, 0, $number, true);
863
    }
864
865 26
    return $this;
866
  }
867
868
  /**
869
   * Exchanges all keys with their associated values in an array.
870
   *
871
   * @return Arrayy (Immutable)
872
   */
873 1
  public function flip()
874
  {
875 1
    $result = array_flip($this->array);
876
877 1
    return static::create($result);
878
  }
879
880
  /**
881
   * Get a value from an array (optional using dot-notation).
882
   *
883
   * @param string $key     The key to look for.
884
   * @param mixed  $default Default value to fallback to.
885
   * @param array  $array   The array to get from, if it's set to "null" we use the current array from the class.
886
   *
887
   * @return mixed
888
   */
889 34
  public function get($key, $default = null, $array = null)
890
  {
891 34
    if (is_array($array) === true) {
892 3
      $usedArray = $array;
893 3
    } else {
894 32
      $usedArray = $this->array;
895
    }
896
897 34
    if (null === $key) {
898 1
      return $usedArray;
899
    }
900
901 34
    if (isset($usedArray[$key])) {
902 23
      return $usedArray[$key];
903
    }
904
905
    // Crawl through array, get key according to object or not
906 19
    foreach (explode('.', $key) as $segment) {
907 19
      if (!isset($usedArray[$segment])) {
908 17
        return $default instanceof Closure ? $default() : $default;
909
      }
910
911 2
      $usedArray = $usedArray[$segment];
912 2
    }
913
914 2
    return $usedArray;
915
  }
916
917
  /**
918
   * Get the current array from the "Arrayy"-object.
919
   *
920
   * @return array
921
   */
922 475
  public function getArray()
923
  {
924 475
    return $this->array;
925
  }
926
927
  /**
928
   * Returns the values from a single column of the input array, identified by
929
   * the $columnKey, can be used to extract data-columns from multi-arrays.
930
   *
931
   * Info: Optionally, you may provide an $indexKey to index the values in the returned
932
   * array by the values from the $indexKey column in the input array.
933
   *
934
   * @param mixed $columnKey
935
   * @param mixed $indexKey
936
   *
937
   * @return Arrayy (Immutable)
938
   */
939 1
  public function getColumn($columnKey = null, $indexKey = null)
940
  {
941 1
    $result = array_column($this->array, $columnKey, $indexKey);
942
943 1
    return static::create($result);
944
  }
945
946
  /**
947
   * Get correct PHP constant for direction.
948
   *
949
   * @param int|string $direction
950
   *
951
   * @return int
952
   */
953 38
  protected function getDirection($direction)
954
  {
955 38
    if (is_string($direction)) {
956 10
      $direction = strtolower($direction);
957
958 10
      if ($direction === 'desc') {
959 2
        $direction = SORT_DESC;
960 2
      } else {
961 8
        $direction = SORT_ASC;
962
      }
963 10
    }
964
965
    if (
966
        $direction !== SORT_DESC
967 38
        &&
968
        $direction !== SORT_ASC
969 38
    ) {
970
      $direction = SORT_ASC;
971
    }
972
973 38
    return $direction;
974
  }
975
976
  /**
977
   * alias: for "Arrayy->keys()"
978
   *
979
   * @return Arrayy (Immutable)
980
   */
981
  public function getKeys()
982
  {
983
    return $this->keys();
984
  }
985
986
  /**
987
   * alias: for "Arrayy->random()"
988
   *
989
   * @return Arrayy (Immutable)
990
   */
991 3
  public function getRandom()
992
  {
993 3
    return $this->randomImmutable();
994
  }
995
996
  /**
997
   * alias: for "Arrayy->randomKey()"
998
   *
999
   * @return mixed get a key/index or null if there wasn't a key/index
1000
   */
1001 3
  public function getRandomKey()
1002
  {
1003 3
    return $this->randomKey();
1004
  }
1005
1006
  /**
1007
   * alias: for "Arrayy->randomKeys()"
1008
   *
1009
   * @param int $number
1010
   *
1011
   * @return Arrayy (Immutable)
1012
   */
1013 9
  public function getRandomKeys($number)
1014
  {
1015 9
    return $this->randomKeys($number);
1016
  }
1017
1018
  /**
1019
   * alias: for "Arrayy->randomValue()"
1020
   *
1021
   * @return mixed get a random value or null if there wasn't a value
1022
   */
1023 3
  public function getRandomValue()
1024
  {
1025 3
    return $this->randomValue();
1026
  }
1027
1028
  /**
1029
   * alias: for "Arrayy->randomValues()"
1030
   *
1031
   * @param int $number
1032
   *
1033
   * @return Arrayy (Immutable)
1034
   */
1035 6
  public function getRandomValues($number)
1036
  {
1037 6
    return $this->randomValues($number);
1038
  }
1039
1040
  /**
1041
   * Group values from a array according to the results of a closure.
1042
   *
1043
   * @param string $grouper a callable function name
1044
   * @param bool   $saveKeys
1045
   *
1046
   * @return Arrayy (Immutable)
1047
   */
1048 3
  public function group($grouper, $saveKeys = false)
1049
  {
1050 3
    $array = (array)$this->array;
1051 3
    $result = array();
1052
1053
    // Iterate over values, group by property/results from closure
1054 3
    foreach ($array as $key => $value) {
1055 3
      $groupKey = is_callable($grouper) ? $grouper($value, $key) : $this->get($grouper, null, $value);
1056 3
      $newValue = $this->get($groupKey, null, $result);
1057
1058
      // Add to results
1059 3
      if ($groupKey !== null) {
1060 2
        if ($saveKeys) {
1061 1
          $result[$groupKey] = $newValue;
1062 1
          $result[$groupKey][$key] = $value;
1063 1
        } else {
1064 1
          $result[$groupKey] = $newValue;
1065 1
          $result[$groupKey][] = $value;
1066
        }
1067 2
      }
1068
1069 3
    }
1070
1071 3
    return static::create($result);
1072
  }
1073
1074
  /**
1075
   * Check if an array has a given key.
1076
   *
1077
   * @param mixed $key
1078
   *
1079
   * @return bool
1080
   */
1081 19
  public function has($key)
1082
  {
1083
    // Generate unique string to use as marker.
1084 19
    $unFound = (string)uniqid('arrayy', true);
1085
1086 19
    return $this->get($key, $unFound) !== $unFound;
1087
  }
1088
1089
  /**
1090
   * Implodes an array.
1091
   *
1092
   * @param string $with What to implode it with
1093
   *
1094
   * @return string
1095
   */
1096 27
  public function implode($with = '')
1097
  {
1098 27
    return implode($with, $this->array);
1099
  }
1100
1101
  /**
1102
   * Given a list and an iterate-function that returns
1103
   * a key for each element in the list (or a property name),
1104
   * returns an object with an index of each item.
1105
   *
1106
   * Just like groupBy, but for when you know your keys are unique.
1107
   *
1108
   * @param mixed $key
1109
   *
1110
   * @return Arrayy (Immutable)
1111
   */
1112 3
  public function indexBy($key)
1113
  {
1114 3
    $results = array();
1115
1116 3
    foreach ($this->array as $a) {
1117 3
      if (isset($a[$key])) {
1118 2
        $results[$a[$key]] = $a;
1119 2
      }
1120 3
    }
1121
1122 3
    return static::create($results);
1123
  }
1124
1125
  /**
1126
   * alias: for "Arrayy->searchIndex()"
1127
   *
1128
   * @param mixed $value Value to search for
1129
   *
1130
   * @return mixed
1131
   */
1132 4
  public function indexOf($value)
1133
  {
1134 4
    return $this->searchIndex($value);
1135
  }
1136
1137
  /**
1138
   * Get everything but the last..$to items.
1139
   *
1140
   * @param int $to
1141
   *
1142
   * @return Arrayy (Immutable)
1143
   */
1144 12
  public function initial($to = 1)
1145
  {
1146 12
    $slice = count($this->array) - $to;
1147
1148 12
    return $this->firstsImmutable($slice);
1149
  }
1150
1151
  /**
1152
   * Internal mechanics of remove method.
1153
   *
1154
   * @param $key
1155
   *
1156
   * @return boolean
1157
   */
1158 18
  protected function internalRemove($key)
1159
  {
1160
    // Explode keys
1161 18
    $keys = explode('.', $key);
1162
1163
    // Crawl though the keys
1164 18
    while (count($keys) > 1) {
1165
      $key = array_shift($keys);
1166
1167
      if (!$this->has($key)) {
1168
        return false;
1169
      }
1170
1171
      $this->array = &$this->array[$key];
1172
    }
1173
1174 18
    $key = array_shift($keys);
1175
1176 18
    unset($this->array[$key]);
1177
1178 18
    return true;
1179
  }
1180
1181
  /**
1182
   * Internal mechanic of set method.
1183
   *
1184
   * @param string $key
1185
   * @param mixed  $value
1186
   *
1187
   * @return bool
1188
   */
1189 17
  protected function internalSet($key, $value)
1190
  {
1191 17
    if (null === $key) {
1192
      return false;
1193
    }
1194
1195
    // init
1196 17
    $array = &$this->array;
1197
1198
    // Explode the keys
1199 17
    $keys = explode('.', $key);
1200
1201
    // Crawl through the keys
1202 17
    while (count($keys) > 1) {
1203 1
      $key = array_shift($keys);
1204
      // If the key doesn't exist at this depth, we will just create an empty array
1205
      // to hold the next value, allowing us to create the arrays to hold final
1206
      // values at the correct depth. Then we'll keep digging into the array.
1207 1
      if (!isset($array[$key]) || !is_array($array[$key])) {
1208
        $array[$key] = array();
1209
      }
1210 1
      $array = &$array[$key];
1211 1
    }
1212
1213 17
    $array[array_shift($keys)] = $value;
1214
1215 17
    return true;
1216
  }
1217
1218
  /**
1219
   * Return an array with all elements found in input array.
1220
   *
1221
   * @param array $search
1222
   *
1223
   * @return Arrayy (Immutable)
1224
   */
1225 2
  public function intersection(array $search)
1226
  {
1227 2
    return static::create(array_values(array_intersect($this->array, $search)));
1228
  }
1229
1230
  /** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
1231
1232
  /**
1233
   * Return a boolean flag which indicates whether the two input arrays have any common elements.
1234
   *
1235
   * @param array $search
1236
   *
1237
   * @return bool
1238
   */
1239 1
  public function intersects(array $search)
1240
  {
1241 1
    return count($this->intersection($search)->array) > 0;
1242
  }
1243
1244
  /**
1245
   * Invoke a function on all of an array's values.
1246
   *
1247
   * @param mixed $callable
1248
   * @param mixed $arguments
1249
   *
1250
   * @return Arrayy (Immutable)
1251
   */
1252 1
  public function invoke($callable, $arguments = array())
1253
  {
1254
    // If one argument given for each iteration, create an array for it.
1255 1
    if (!is_array($arguments)) {
1256 1
      $arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray();
1257 1
    }
1258
1259
    // If the callable has arguments, pass them.
1260 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...
1261 1
      $array = array_map($callable, $this->array, $arguments);
1262 1
    } else {
1263 1
      $array = array_map($callable, $this->array);
1264
    }
1265
1266 1
    return static::create($array);
1267
  }
1268
1269
  /**
1270
   * Check whether array is associative or not.
1271
   *
1272
   * @return bool Returns true if associative, false otherwise
1273
   */
1274 15
  public function isAssoc()
1275
  {
1276 15
    if ($this->isEmpty()) {
1277 3
      return false;
1278
    }
1279
1280 13
    foreach ($this->keys()->getArray() as $key) {
1281 13
      if (!is_string($key)) {
1282 11
        return false;
1283
      }
1284 3
    }
1285
1286 3
    return true;
1287
  }
1288
1289
  /**
1290
   * Check whether the array is empty or not.
1291
   *
1292
   * @return bool Returns true if empty, false otherwise
1293
   */
1294 25
  public function isEmpty()
1295
  {
1296 25
    return !$this->array;
1297
  }
1298
1299
  /**
1300
   * Check if the current array is equal to the given "$array" or not.
1301
   *
1302
   * @param array $array
1303
   *
1304
   * @return bool
1305
   */
1306
  public function isEqual(array $array)
1307
  {
1308
    return ($this->array === $array);
1309
  }
1310
1311
  /**
1312
   * Check if the current array is a multi-array.
1313
   *
1314
   * @return bool
1315
   */
1316 14
  public function isMultiArray()
1317
  {
1318 14
    return !(count($this->array) === count($this->array, COUNT_RECURSIVE));
1319
  }
1320
1321
  /**
1322
   * Check whether array is numeric or not.
1323
   *
1324
   * @return bool Returns true if numeric, false otherwise
1325
   */
1326 5
  public function isNumeric()
1327
  {
1328 5
    if ($this->isEmpty()) {
1329 2
      return false;
1330
    }
1331
1332 4
    foreach ($this->keys() as $key) {
1333 4
      if (!is_int($key)) {
1334 2
        return false;
1335
      }
1336 3
    }
1337
1338 2
    return true;
1339
  }
1340
1341
  /**
1342
   * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not.
1343
   *
1344
   * @return bool
1345
   */
1346 1
  public function isSequential()
1347
  {
1348 1
    return array_keys($this->array) === range(0, count($this->array) - 1);
1349
  }
1350
1351
  /**
1352
   * Get all keys from the current array.
1353
   *
1354
   * @return Arrayy (Immutable)
1355
   */
1356 23
  public function keys()
1357
  {
1358 23
    return static::create(array_keys((array)$this->array));
1359
  }
1360
1361
  /**
1362
   * Get the last value from the current array.
1363
   *
1364
   * @return mixed Return null if there wasn't a element.
1365
   */
1366 4
  public function last()
1367
  {
1368 4
    $result = $this->pop();
1369
1370 4
    if (null === $result) {
1371 1
      return null;
1372
    } else {
1373 3
      return $result;
1374
    }
1375
  }
1376
1377
  /**
1378
   * Get the last value(s) from the current array.
1379
   *
1380
   * @param int|null $number
1381
   *
1382
   * @return Arrayy (Immutable)
1383
   */
1384 12
  public function lastsImmutable($number = null)
1385
  {
1386 12
    if ($number === null) {
1387 8
      $poppedValue = (array)$this->pop();
1388 8
      $arrayy = static::create($poppedValue);
1389 8
    } else {
1390 4
      $number = (int)$number;
1391 4
      $arrayy = $this->rest(-$number);
1392
    }
1393
1394 12
    return $arrayy;
1395
  }
1396
1397
  /**
1398
   * Get the last value(s) from the current array.
1399
   *
1400
   * @param int|null $number
1401
   *
1402
   * @return self (Mutable)
1403
   */
1404 12
  public function lastsMutable($number = null)
1405
  {
1406 12
    if ($number === null) {
1407 8
      $poppedValue = (array)$this->pop();
1408 8
      $this->array = static::create($poppedValue)->array;
1409 8
    } else {
1410 4
      $number = (int)$number;
1411 4
      $this->array = $this->rest(-$number)->array;
1412
    }
1413
1414 12
    return $this;
1415
  }
1416
1417
  /**
1418
   * Count the values from the current array.
1419
   *
1420
   * INFO: only a alias for "$arrayy->size()"
1421
   *
1422
   * @return int
1423
   */
1424 10
  public function length()
1425
  {
1426 10
    return $this->size();
1427
  }
1428
1429
  /**
1430
   * Apply the given function to the every element of the array,
1431
   * collecting the results.
1432
   *
1433
   * @param callable $callable
1434
   *
1435
   * @return Arrayy (Immutable) Arrayy object with modified elements
1436
   */
1437 4
  public function map($callable)
1438
  {
1439 4
    $result = array_map($callable, $this->array);
1440
1441 4
    return static::create($result);
1442
  }
1443
1444
  /**
1445
   * Check if all items in current array match a truth test.
1446
   *
1447
   * @param \Closure $closure
1448
   *
1449
   * @return bool
1450
   */
1451 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...
1452
  {
1453
    // Reduce the array to only booleans
1454 9
    $array = $this->each($closure);
1455
1456
    // Check the results
1457 9
    if (count($array) === 0) {
1458 2
      return true;
1459
    }
1460
1461 7
    $array = array_search(false, $array->toArray(), false);
1462
1463 7
    return is_bool($array);
1464
  }
1465
1466
  /**
1467
   * Check if any item in the current array matches a truth test.
1468
   *
1469
   * @param \Closure $closure
1470
   *
1471
   * @return bool
1472
   */
1473 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...
1474
  {
1475
    // Reduce the array to only booleans
1476 9
    $array = $this->each($closure);
1477
1478
    // Check the results
1479 9
    if (count($array) === 0) {
1480 2
      return true;
1481
    }
1482
1483 7
    $array = array_search(true, $array->toArray(), false);
1484
1485 7
    return is_int($array);
1486
  }
1487
1488
  /**
1489
   * Get the max value from an array.
1490
   *
1491
   * @return mixed
1492
   */
1493 10
  public function max()
1494
  {
1495 10
    if ($this->count() === 0) {
1496 1
      return false;
1497
    }
1498
1499 9
    return max($this->array);
1500
  }
1501
1502
  /**
1503
   * Merge the new $array into the current array.
1504
   *
1505
   * - keep key,value from the current array, also if the index is in the new $array
1506
   *
1507
   * @param array $array
1508
   * @param bool  $recursive
1509
   *
1510
   * @return Arrayy (Immutable)
1511
   */
1512 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...
1513
  {
1514 25
    if (true === $recursive) {
1515 4
      $result = array_replace_recursive($this->array, $array);
1516 4
    } else {
1517 21
      $result = array_replace($this->array, $array);
1518
    }
1519
1520 25
    return static::create($result);
1521
  }
1522
1523
  /**
1524
   * Merge the new $array into the current array.
1525
   *
1526
   * - replace duplicate assoc-keys from the current array with the key,values from the new $array
1527
   * - create new indexes
1528
   *
1529
   * @param array $array
1530
   * @param bool  $recursive
1531
   *
1532
   * @return Arrayy (Immutable)
1533
   */
1534 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...
1535
  {
1536 16
    if (true === $recursive) {
1537 4
      $result = array_merge_recursive($this->array, $array);
1538 4
    } else {
1539 12
      $result = array_merge($this->array, $array);
1540
    }
1541
1542 16
    return static::create($result);
1543
  }
1544
1545
  /**
1546
   * Merge the the current array into the $array.
1547
   *
1548
   * - use key,value from the new $array, also if the index is in the current array
1549
   *
1550
   * @param array $array
1551
   * @param bool  $recursive
1552
   *
1553
   * @return Arrayy (Immutable)
1554
   */
1555 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...
1556
  {
1557 16
    if (true === $recursive) {
1558 4
      $result = array_replace_recursive($array, $this->array);
1559 4
    } else {
1560 12
      $result = array_replace($array, $this->array);
1561
    }
1562
1563 16
    return static::create($result);
1564
  }
1565
1566
  /**
1567
   * Merge the current array into the new $array.
1568
   *
1569
   * - replace duplicate assoc-keys from new $array with the key,values from the current array
1570
   * - create new indexes
1571
   *
1572
   * @param array $array
1573
   * @param bool  $recursive
1574
   *
1575
   * @return Arrayy (Immutable)
1576
   */
1577 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...
1578
  {
1579 16
    if (true === $recursive) {
1580 4
      $result = array_merge_recursive($array, $this->array);
1581 4
    } else {
1582 12
      $result = array_merge($array, $this->array);
1583
    }
1584
1585 16
    return static::create($result);
1586
  }
1587
1588
  /**
1589
   * Get the min value from an array.
1590
   *
1591
   * @return mixed
1592
   */
1593 10
  public function min()
1594
  {
1595 10
    if ($this->count() === 0) {
1596 1
      return false;
1597
    }
1598
1599 9
    return min($this->array);
1600
  }
1601
1602
  /**
1603
   * Get a subset of the items from the given array.
1604
   *
1605
   * @param mixed[] $keys
1606
   *
1607
   * @return Arrayy (Immutable)
1608
   */
1609
  public function only(array $keys)
1610
  {
1611
    $array = $this->array;
1612
1613
    return static::create(array_intersect_key($array, array_flip($keys)));
1614
  }
1615
1616
  /**
1617
   * Pad array to the specified size with a given value.
1618
   *
1619
   * @param int   $size  Size of the result array
1620
   * @param mixed $value Empty value by default
1621
   *
1622
   * @return Arrayy (Immutable) Arrayy object padded to $size with $value
1623
   */
1624 4
  public function pad($size, $value)
1625
  {
1626 4
    $result = array_pad($this->array, $size, $value);
1627
1628 4
    return static::create($result);
1629
  }
1630
1631
  /**
1632
   * Pop a specified value off the end of the current array.
1633
   *
1634
   * @return mixed The popped element from the current array.
1635
   */
1636 16
  public function pop()
1637
  {
1638 16
    return array_pop($this->array);
1639
  }
1640
1641
  /**
1642
   * Prepend a value to the current array.
1643
   *
1644
   * @param mixed $value
1645
   * @param mixed $key
1646
   *
1647
   * @return self (Mutable) Return this Arrayy object, with the prepended value.
1648
   */
1649 8
  public function prepend($value, $key = null)
1650
  {
1651 8
    if ($key === null) {
1652 8
      array_unshift($this->array, $value);
1653 8
    } else {
1654
      /** @noinspection AdditionOperationOnArraysInspection */
1655 1
      $this->array = array($key => $value) + $this->array;
1656
    }
1657
1658 8
    return $this;
1659
  }
1660
1661
  /**
1662
   * Push one or more values onto the end of array at once.
1663
   *
1664
   * @return self (Mutable) Return this Arrayy object, with pushed elements to the end of array.
1665
   */
1666 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...
1667
  {
1668 4
    if (func_num_args()) {
1669 4
      $args = array_merge(array(&$this->array), func_get_args());
1670 4
      call_user_func_array('array_push', $args);
1671 4
    }
1672
1673 4
    return $this;
1674
  }
1675
1676
  /**
1677
   * Get a random value from the current array.
1678
   *
1679
   * @param null|int $number how many values you will take?
1680
   *
1681
   * @return Arrayy (Immutable)
1682
   */
1683 17
  public function randomImmutable($number = null)
1684
  {
1685 17
    if ($this->count() === 0) {
1686
      return static::create();
1687
    }
1688
1689 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...
1690 14
      $arrayRandValue = (array)$this->array[array_rand($this->array)];
1691
1692 14
      return static::create($arrayRandValue);
1693
    }
1694
1695 5
    $arrayTmp = $this->array;
1696 5
    shuffle($arrayTmp);
1697
1698 5
    return self::create($arrayTmp)->firstsImmutable($number);
1699
  }
1700
1701
  /**
1702
   * Pick a random key/index from the keys of this array.
1703
   *
1704
   *
1705
   * @return mixed get a key/index or null if there wasn't a key/index
1706
   *
1707
   * @throws \RangeException If array is empty
1708
   */
1709 4
  public function randomKey()
1710
  {
1711 4
    $result = $this->randomKeys(1);
1712
1713 4
    if (!isset($result[0])) {
1714
      $result[0] = null;
1715
    }
1716
1717 4
    return $result[0];
1718
  }
1719
1720
  /**
1721
   * Pick a given number of random keys/indexes out of this array.
1722
   *
1723
   * @param int $number The number of keys/indexes (should be <= $this->count())
1724
   *
1725
   * @return Arrayy (Immutable)
1726
   *
1727
   * @throws \RangeException If array is empty
1728
   */
1729 14
  public function randomKeys($number)
1730
  {
1731 14
    $number = (int)$number;
1732 14
    $count = $this->count();
1733
1734 14
    if ($number === 0 || $number > $count) {
1735 3
      throw new \RangeException(
1736 3
          sprintf(
1737 3
              'Number of requested keys (%s) must be equal or lower than number of elements in this array (%s)',
1738 3
              $number,
1739
              $count
1740 3
          )
1741 3
      );
1742
    }
1743
1744 11
    $result = (array)array_rand($this->array, $number);
1745
1746 11
    return static::create($result);
1747
  }
1748
1749
  /**
1750
   * Get a random value from the current array.
1751
   *
1752
   * @param null|int $number how many values you will take?
1753
   *
1754
   * @return Arrayy (Mutable)
1755
   */
1756 16
  public function randomMutable($number = null)
1757
  {
1758 16
    if ($this->count() === 0) {
1759
      return static::create();
1760
    }
1761
1762 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...
1763 6
      $arrayRandValue = (array)$this->array[array_rand($this->array)];
1764 6
      $this->array = $arrayRandValue;
1765
1766 6
      return $this;
1767
    }
1768
1769 11
    shuffle($this->array);
1770
1771 11
    return $this->firstsMutable($number);
1772
  }
1773
1774
  /**
1775
   * Pick a random value from the values of this array.
1776
   *
1777
   * @return mixed get a random value or null if there wasn't a value
1778
   */
1779 4
  public function randomValue()
1780
  {
1781 4
    $result = $this->randomImmutable();
1782
1783 4
    if (!isset($result[0])) {
1784
      $result[0] = null;
1785
    }
1786
1787 4
    return $result[0];
1788
  }
1789
1790
  /**
1791
   * Pick a given number of random values out of this array.
1792
   *
1793
   * @param int $number
1794
   *
1795
   * @return Arrayy (Mutable)
1796
   */
1797 7
  public function randomValues($number)
1798
  {
1799 7
    $number = (int)$number;
1800
1801 7
    return $this->randomMutable($number);
1802
  }
1803
1804
  /**
1805
   * Get a random value from an array, with the ability to skew the results.
1806
   *
1807
   * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
1808
   *
1809
   * @param array    $array
1810
   * @param null|int $number how many values you will take?
1811
   *
1812
   * @return Arrayy (Immutable)
1813
   */
1814 9
  public function randomWeighted(array $array, $number = null)
1815
  {
1816 9
    $options = array();
1817 9
    foreach ($array as $option => $weight) {
1818 9
      if ($this->searchIndex($option) !== false) {
1819 2
        for ($i = 0; $i < $weight; ++$i) {
1820 1
          $options[] = $option;
1821 1
        }
1822 2
      }
1823 9
    }
1824
1825 9
    return $this->mergeAppendKeepIndex($options)->randomImmutable($number);
1826
  }
1827
1828
  /**
1829
   * Reduce the current array via callable e.g. anonymous-function.
1830
   *
1831
   * @param mixed $callable
1832
   * @param array $init
1833
   *
1834
   * @return Arrayy (Immutable)
1835
   */
1836 3
  public function reduce($callable, array $init = array())
1837
  {
1838 3
    $result = array_reduce($this->array, $callable, $init);
1839
1840 3
    if ($result === null) {
1841
      $this->array = array();
1842
    } else {
1843 3
      $this->array = (array)$result;
1844
    }
1845
1846 3
    return static::create($this->array);
1847
  }
1848
1849
  /**
1850
   * Create a numerically re-indexed Arrayy object.
1851
   *
1852
   * @return self (Mutable) Return this Arrayy object, with re-indexed array-elements.
1853
   */
1854 9
  public function reindex()
1855
  {
1856 9
    $this->array = array_values($this->array);
1857
1858 9
    return $this;
1859
  }
1860
1861
  /**
1862
   * Return all items that fail the truth test.
1863
   *
1864
   * @param \Closure $closure
1865
   *
1866
   * @return Arrayy (Immutable)
1867
   */
1868 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...
1869
  {
1870 1
    $filtered = array();
1871
1872 1
    foreach ($this->array as $key => $value) {
1873 1
      if (!$closure($value, $key)) {
1874 1
        $filtered[$key] = $value;
1875 1
      }
1876 1
    }
1877
1878 1
    return static::create($filtered);
1879
  }
1880
1881
  /**
1882
   * Remove a value from the current array (optional using dot-notation).
1883
   *
1884
   * @param mixed $key
1885
   *
1886
   * @return Arrayy (Immutable)
1887
   */
1888 18
  public function remove($key)
1889
  {
1890
    // Recursive call
1891 18
    if (is_array($key)) {
1892
      foreach ($key as $k) {
1893
        $this->internalRemove($k);
1894
      }
1895
1896
      return static::create($this->array);
1897
    }
1898
1899 18
    $this->internalRemove($key);
1900
1901 18
    return static::create($this->array);
1902
  }
1903
1904
  /**
1905
   * Remove the first value from the current array.
1906
   *
1907
   * @return Arrayy (Immutable)
1908
   */
1909 7
  public function removeFirst()
1910
  {
1911 7
    array_shift($this->array);
1912
1913 7
    return static::create($this->array);
1914
  }
1915
1916
  /**
1917
   * Remove the last value from the current array.
1918
   *
1919
   * @return Arrayy (Immutable)
1920
   */
1921 7
  public function removeLast()
1922
  {
1923 7
    array_pop($this->array);
1924
1925 7
    return static::create($this->array);
1926
  }
1927
1928
  /**
1929
   * Removes a particular value from an array (numeric or associative).
1930
   *
1931
   * @param mixed $value
1932
   *
1933
   * @return Arrayy (Immutable)
1934
   */
1935 7
  public function removeValue($value)
1936
  {
1937 7
    $isNumericArray = true;
1938 7
    foreach ($this->array as $key => $item) {
1939 6
      if ($item === $value) {
1940 6
        if (!is_int($key)) {
1941
          $isNumericArray = false;
1942
        }
1943 6
        unset($this->array[$key]);
1944 6
      }
1945 7
    }
1946
1947 7
    if ($isNumericArray) {
1948 7
      $this->array = array_values($this->array);
1949 7
    }
1950
1951 7
    return static::create($this->array);
1952
  }
1953
1954
  /**
1955
   * Replace a key with a new key/value pair.
1956
   *
1957
   * @param $replace
1958
   * @param $key
1959
   * @param $value
1960
   *
1961
   * @return Arrayy (Immutable)
1962
   */
1963 2
  public function replace($replace, $key, $value)
1964
  {
1965 2
    $this->remove($replace);
1966
1967 2
    return $this->set($key, $value);
1968
  }
1969
1970
  /**
1971
   * Create an array using the current array as values and the other array as keys.
1972
   *
1973
   * @param array $keys Keys array
1974
   *
1975
   * @return Arrayy (Immutable) Arrayy object with keys from the other array.
1976
   */
1977 2
  public function replaceAllKeys(array $keys)
1978
  {
1979 2
    $result = array_combine($keys, $this->array);
1980
1981 2
    return static::create($result);
1982
  }
1983
1984
  /**
1985
   * Create an array using the current array as keys and the other array as values.
1986
   *
1987
   * @param array $array Values array
1988
   *
1989
   * @return Arrayy (Immutable) Arrayy object with values from the other array.
1990
   */
1991 2
  public function replaceAllValues(array $array)
1992
  {
1993 2
    $result = array_combine($this->array, $array);
1994
1995 2
    return static::create($result);
1996
  }
1997
1998
  /**
1999
   * Replace the keys in an array with another set.
2000
   *
2001
   * @param array $keys An array of keys matching the array's size
2002
   *
2003
   * @return Arrayy (Immutable)
2004
   */
2005 1
  public function replaceKeys(array $keys)
2006
  {
2007 1
    $values = array_values($this->array);
2008 1
    $result = array_combine($keys, $values);
2009
2010 1
    return static::create($result);
2011
  }
2012
2013
  /**
2014
   * Replace the first matched value in an array.
2015
   *
2016
   * @param mixed $search
2017
   * @param mixed $replacement
2018
   *
2019
   * @return Arrayy (Immutable)
2020
   */
2021 3
  public function replaceOneValue($search, $replacement = '')
2022
  {
2023 3
    $array = $this->array;
2024 3
    $key = array_search($search, $array, true);
2025
2026 3
    if ($key !== false) {
2027 3
      $array[$key] = $replacement;
2028 3
    }
2029
2030 3
    return static::create($array);
2031
  }
2032
2033
  /**
2034
   * Replace values in the current array.
2035
   *
2036
   * @param string $search      The string to replace.
2037
   * @param string $replacement What to replace it with.
2038
   *
2039
   * @return Arrayy (Immutable)
2040
   */
2041 1
  public function replaceValues($search, $replacement = '')
2042
  {
2043 1
    $array = $this->each(
2044
        function ($value) use ($search, $replacement) {
2045 1
          return UTF8::str_replace($search, $replacement, $value);
2046
        }
2047 1
    );
2048
2049 1
    return $array;
2050
  }
2051
2052
  /**
2053
   * Get the last elements from index $from until the end of this array.
2054
   *
2055
   * @param int $from
2056
   *
2057
   * @return Arrayy (Immutable)
2058
   */
2059 15
  public function rest($from = 1)
2060
  {
2061 15
    $result = array_splice($this->array, $from);
2062
2063 15
    return static::create($result);
2064
  }
2065
2066
  /**
2067
   * Return the array in the reverse order.
2068
   *
2069
   * @return self (Mutable) Return this Arrayy object.
2070
   */
2071 7
  public function reverse()
2072
  {
2073 7
    $this->array = array_reverse($this->array);
2074
2075 7
    return $this;
2076
  }
2077
2078
  /**
2079
   * Search for the first index of the current array via $value.
2080
   *
2081
   * @param mixed $value
2082
   *
2083
   * @return mixed
2084
   */
2085 20
  public function searchIndex($value)
2086
  {
2087 20
    return array_search($value, $this->array, true);
2088
  }
2089
2090
  /**
2091
   * Search for the value of the current array via $index.
2092
   *
2093
   * @param mixed $index
2094
   *
2095
   * @return Arrayy (Immutable) will return a empty Arrayy if the value wasn't found
2096
   */
2097 7
  public function searchValue($index)
2098
  {
2099
    // init
2100 7
    $return = array();
2101
2102 7
    if (null !== $index) {
2103 7
      $keyExists = isset($this->array[$index]);
2104
2105 7
      if ($keyExists !== false) {
2106 5
        $return = array($this->array[$index]);
2107 5
      }
2108 7
    }
2109
2110 7
    return static::create($return);
2111
  }
2112
2113
  /**
2114
   * Set a value for the current array (optional using dot-notation).
2115
   *
2116
   * @param string $key   The key to set
2117
   * @param mixed  $value Its value
2118
   *
2119
   * @return Arrayy (Immutable)
2120
   */
2121 17
  public function set($key, $value)
2122
  {
2123 17
    $this->internalSet($key, $value);
2124
2125 17
    return static::create($this->array);
2126
  }
2127
2128
  /**
2129
   * Get a value from a array and set it if it was not.
2130
   *
2131
   * WARNING: this method only set the value, if the $key is not already set
2132
   *
2133
   * @param string $key      The key
2134
   * @param mixed  $fallback The default value to set if it isn't
2135
   *
2136
   * @return mixed (Mutable)
2137
   */
2138 10
  public function setAndGet($key, $fallback = null)
2139
  {
2140
    // If the key doesn't exist, set it
2141 10
    if (!$this->has($key)) {
2142 5
      $this->array = $this->set($key, $fallback)->getArray();
2143 5
    }
2144
2145 10
    return $this->get($key);
2146
  }
2147
2148
  /**
2149
   * Shifts a specified value off the beginning of array.
2150
   *
2151
   * @return mixed A shifted element from the current array.
2152
   */
2153 4
  public function shift()
2154
  {
2155 4
    return array_shift($this->array);
2156
  }
2157
2158
  /**
2159
   * Shuffle the current array.
2160
   *
2161
   * @return Arrayy (Immutable)
2162
   */
2163 1
  public function shuffle()
2164
  {
2165 1
    $array = $this->array;
2166
2167 1
    shuffle($array);
2168
2169 1
    return static::create($array);
2170
  }
2171
2172
  /**
2173
   * Get the size of an array.
2174
   *
2175
   * @return int
2176
   */
2177 110
  public function size()
2178
  {
2179 110
    return count($this->array);
2180
  }
2181
2182
  /**
2183
   * Extract a slice of the array.
2184
   *
2185
   * @param int      $offset       Slice begin index
2186
   * @param int|null $length       Length of the slice
2187
   * @param bool     $preserveKeys Whether array keys are preserved or no
2188
   *
2189
   * @return static A slice of the original array with length $length
2190
   */
2191 4
  public function slice($offset, $length = null, $preserveKeys = false)
2192
  {
2193 4
    $result = array_slice($this->array, $offset, $length, $preserveKeys);
2194
2195 4
    return static::create($result);
2196
  }
2197
2198
  /**
2199
   * Sort the current array and optional you can keep the keys.
2200
   *
2201
   * @param integer $direction use SORT_ASC or SORT_DESC
2202
   * @param integer $strategy
2203
   * @param bool    $keepKeys
2204
   *
2205
   * @return self (Mutable) Return this Arrayy object.
2206
   */
2207 19
  public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2208
  {
2209 19
    $this->sorting($this->array, $direction, $strategy, $keepKeys);
2210
2211 19
    return $this;
2212
  }
2213
2214
  /**
2215
   * Sort the current array by key.
2216
   *
2217
   * @link http://php.net/manual/en/function.ksort.php
2218
   * @link http://php.net/manual/en/function.krsort.php
2219
   *
2220
   * @param int|string $direction use SORT_ASC or SORT_DESC
2221
   * @param int        $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2222
   *
2223
   * @return self (Mutable) Return this Arrayy object.
2224
   */
2225 18
  public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR)
2226
  {
2227 18
    $this->sorterKeys($this->array, $direction, $strategy);
2228
2229 18
    return $this;
2230
  }
2231
2232
  /**
2233
   * Sort the current array by value.
2234
   *
2235
   * @param int $direction use SORT_ASC or SORT_DESC
2236
   * @param int $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2237
   *
2238
   * @return Arrayy (Immutable)
2239
   */
2240 1
  public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2241
  {
2242 1
    return $this->sort($direction, $strategy, true);
2243
  }
2244
2245
  /**
2246
   * Sort the current array by value.
2247
   *
2248
   * @param int $direction use SORT_ASC or SORT_DESC
2249
   * @param int $strategy  use e.g.: SORT_REGULAR or SORT_NATURAL
2250
   *
2251
   * @return Arrayy (Immutable)
2252
   */
2253 1
  public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR)
2254
  {
2255 1
    return $this->sort($direction, $strategy, false);
2256
  }
2257
2258
  /**
2259
   * Sort a array by value, by a closure or by a property.
2260
   *
2261
   * - If the sorter is null, the array is sorted naturally.
2262
   * - Associative (string) keys will be maintained, but numeric keys will be re-indexed.
2263
   *
2264
   * @param null       $sorter
2265
   * @param string|int $direction
2266
   * @param int        $strategy
2267
   *
2268
   * @return Arrayy (Immutable)
2269
   */
2270 1
  public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2271
  {
2272 1
    $array = (array)$this->array;
2273 1
    $direction = $this->getDirection($direction);
2274
2275
    // Transform all values into their results.
2276 1
    if ($sorter) {
2277 1
      $arrayy = new self($array);
2278
2279 1
      $that = $this;
2280 1
      $results = $arrayy->each(
2281
          function ($value) use ($sorter, $that) {
2282 1
            return is_callable($sorter) ? $sorter($value) : $that->get($sorter, null, $value);
2283
          }
2284 1
      );
2285
2286 1
      $results = $results->getArray();
2287 1
    } else {
2288 1
      $results = $array;
2289
    }
2290
2291
    // Sort by the results and replace by original values
2292 1
    array_multisort($results, $direction, $strategy, $array);
2293
2294 1
    return static::create($array);
2295
  }
2296
2297
  /**
2298
   * sorting keys
2299
   *
2300
   * @param array $elements
2301
   * @param int   $direction
2302
   * @param int   $strategy
2303
   */
2304 18
  protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR)
2305
  {
2306 18
    $direction = $this->getDirection($direction);
2307
2308
    switch ($direction) {
2309 18
      case 'desc':
2310 18
      case SORT_DESC:
2311 6
        krsort($elements, $strategy);
2312 6
        break;
2313 13
      case 'asc':
2314 13
      case SORT_ASC:
2315 13
      default:
2316 13
        ksort($elements, $strategy);
2317 13
    }
2318 18
  }
2319
2320
  /**
2321
   * @param array      &$elements
2322
   * @param int|string $direction
2323
   * @param int        $strategy
2324
   * @param bool       $keepKeys
2325
   */
2326 19
  protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false)
2327
  {
2328 19
    $direction = $this->getDirection($direction);
2329
2330 19
    if (!$strategy) {
2331 19
      $strategy = SORT_REGULAR;
2332 19
    }
2333
2334
    switch ($direction) {
2335 19
      case 'desc':
2336 19
      case SORT_DESC:
2337 9
        if ($keepKeys) {
2338 5
          arsort($elements, $strategy);
2339 5
        } else {
2340 4
          rsort($elements, $strategy);
2341
        }
2342 9
        break;
2343 10
      case 'asc':
2344 10
      case SORT_ASC:
2345 10
      default:
2346 10
        if ($keepKeys) {
2347 4
          asort($elements, $strategy);
2348 4
        } else {
2349 6
          sort($elements, $strategy);
2350
        }
2351 10
    }
2352 19
  }
2353
2354
  /**
2355
   * Split an array in the given amount of pieces.
2356
   *
2357
   * @param int  $numberOfPieces
2358
   * @param bool $keepKeys
2359
   *
2360
   * @return Arrayy (Immutable)
2361
   */
2362 1
  public function split($numberOfPieces = 2, $keepKeys = false)
2363
  {
2364 1
    if (count($this->array) === 0) {
2365 1
      $result = array();
2366 1
    } else {
2367 1
      $numberOfPieces = (int)$numberOfPieces;
2368 1
      $splitSize = ceil(count($this->array) / $numberOfPieces);
2369 1
      $result = array_chunk($this->array, $splitSize, $keepKeys);
2370
    }
2371
2372 1
    return static::create($result);
2373
  }
2374
2375
  /**
2376
   * Stripe all empty items.
2377
   *
2378
   * @return Arrayy (Immutable)
2379
   */
2380 1
  public function stripEmpty()
2381
  {
2382 1
    return $this->filter(
2383
        function ($item) {
2384 1
          if (null === $item) {
2385 1
            return false;
2386
          }
2387
2388 1
          return (bool)trim($item);
2389
        }
2390 1
    );
2391
  }
2392
2393
  /**
2394
   * Swap two values between positions by key.
2395
   *
2396
   * @param string|int $swapA an key in the array
2397
   * @param string|int $swapB an key in the array
2398
   *
2399
   * @return Arrayy (Immutable)
2400
   */
2401 1
  public function swap($swapA, $swapB)
2402
  {
2403 1
    $array = $this->array;
2404
2405 1
    list($array[$swapA], $array[$swapB]) = array($array[$swapB], $array[$swapA]);
2406
2407 1
    return static::create($array);
2408
  }
2409
2410
  /**
2411
   * alias: for "Arrayy->getArray()"
2412
   */
2413 153
  public function toArray()
2414
  {
2415 153
    return $this->getArray();
2416
  }
2417
2418
  /**
2419
   * Convert the current array to JSON.
2420
   *
2421
   * @param null $options e.g. JSON_PRETTY_PRINT
2422
   *
2423
   * @return string
2424
   */
2425 5
  public function toJson($options = null)
2426
  {
2427 5
    return UTF8::json_encode($this->array, $options);
2428
  }
2429
2430
  /**
2431
   * Implodes array to a string with specified separator.
2432
   *
2433
   * @param string $separator The element's separator
2434
   *
2435
   * @return string The string representation of array, separated by ","
2436
   */
2437 19
  public function toString($separator = ',')
2438
  {
2439 19
    return $this->implode($separator);
2440
  }
2441
2442
  /**
2443
   * Return a duplicate free copy of the current array.
2444
   *
2445
   * @return Arrayy (Mutable)
2446
   */
2447 8
  public function unique()
2448
  {
2449 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...
2450 8
        $this->array,
2451 8
        function ($resultArray, $value) {
2452 7
          if (in_array($value, $resultArray, true) === false) {
2453 7
            $resultArray[] = $value;
2454 7
          }
2455
2456 7
          return $resultArray;
2457 8
        },
2458 8
        array()
2459 8
    );
2460
2461 8
    if ($this->array === null) {
2462
      $this->array = array();
2463
    } else {
2464 8
      $this->array = (array)$this->array;
2465
    }
2466
2467 8
    return $this;
2468
  }
2469
2470
  /**
2471
   * Prepends one or more values to the beginning of array at once.
2472
   *
2473
   * @return self (Mutable) Return this Arrayy object, with prepended elements to the beginning of array.
2474
   */
2475 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...
2476
  {
2477 4
    if (func_num_args()) {
2478 4
      $args = array_merge(array(&$this->array), func_get_args());
2479 4
      call_user_func_array('array_unshift', $args);
2480 4
    }
2481
2482 4
    return $this;
2483
  }
2484
2485
  /**
2486
   * Get all values from a array.
2487
   *
2488
   * @return Arrayy (Immutable)
2489
   */
2490 2
  public function values()
2491
  {
2492 2
    return static::create(array_values((array)$this->array));
2493
  }
2494
2495
  /**
2496
   * Apply the given function to every element in the array, discarding the results.
2497
   *
2498
   * @param callable $callable
2499
   * @param bool     $recursive Whether array will be walked recursively or no
2500
   *
2501
   * @return self (Mutable) Return this Arrayy object, with modified elements
2502
   */
2503 9
  public function walk($callable, $recursive = false)
2504
  {
2505 9
    if (true === $recursive) {
2506 4
      array_walk_recursive($this->array, $callable);
2507 4
    } else {
2508 5
      array_walk($this->array, $callable);
2509
    }
2510
2511 9
    return $this;
2512
  }
2513
2514
2515
}
2516