Completed
Push — master ( cf2e2e...a40fc9 )
by ARCANEDEV
02:34
created

Arr::getArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php namespace Arcanedev\Settings\Utilities;
2
3
use Illuminate\Support\Arr as BaseArr;
4
5
/**
6
 * Class     Arr
7
 *
8
 * @package  Arcanedev\Settings\Utilities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Arr extends BaseArr
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Main Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Get an item from an array using "dot" notation.
19
     *
20
     * @param  array         $array
21
     * @param  array|string  $key
22
     * @param  mixed         $default
23
     *
24
     * @return mixed
25
     */
26 33
    public static function get($array, $key, $default = null)
27
    {
28 33
        return is_array($key)
29 33
            ? static::getArray($array, $key, $default)
30 33
            : parent::get($array, $key, $default);
31
    }
32
33
    /**
34
     * Get array by keys.
35
     *
36
     * @param  array  $input
37
     * @param  array  $keys
38
     * @param  mixed  $default
39
     *
40
     * @return array
41
     */
42 12
    protected static function getArray(array $input, array $keys, $default = null)
43
    {
44 12
        $output = [];
45
46 12
        foreach ($keys as $key) {
47 12
            static::set($output, $key, static::get($input, $key, $default));
48 12
        }
49
50 12
        return $output;
51
    }
52
53
    /**
54
     * Set an array item to a given value using "dot" notation.
55
     *
56
     * If no key is given to the method, the entire array will be replaced.
57
     *
58
     * @param  array   $array
59
     * @param  string  $key
60
     * @param  mixed   $value
61
     *
62
     * @return array
63
     */
64 33
    public static function set(&$array, $key, $value)
65
    {
66
        // @codeCoverageIgnoreStart
67
        if (is_null($key)) {
68
            return $array = $value;
69
        }
70
        // @codeCoverageIgnoreEnd
71
72 33
        $keys = explode('.', $key);
73
74 33
        while (count($keys) > 1) {
75 24
            $key = array_shift($keys);
76
77 24
            if ( ! isset($array[$key])) {
78 18
                $array[$key] = [];
79 18
            }
80 9
            elseif ( ! is_array($array[$key])) {
81 3
                throw new \UnexpectedValueException('Non-array segment encountered');
82
            }
83
84 21
            $array = &$array[$key];
85 21
        }
86
87 30
        $array[array_shift($keys)] = $value;
88
89 30
        return $array;
90
    }
91
}
92