Arr   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 91
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 20 4
A has() 0 19 6
A get() 0 19 6
1
<?php
2
3
namespace ArrayHelpers;
4
5
class Arr
6
{
7
    /**
8
     * Get an item from an array.
9
     * Supports dot notation:
10
     * e.g `Arr::get($array, 'section.subsection.item')`
11
     *
12
     * @param array $array
13
     * @param string $key
14
     * @param mixed|null $default
15
     * @return mixed|null
16
     */
17 5
    public static function get(array $array, $key, $default = null)
18
    {
19 5
        if (array_key_exists($key, $array)) {
20 1
            return $array[$key];
21
        }
22
23 4
        if (strpos($key, '.') === false) {
24 2
            return $default;
25
        }
26
27 2
        foreach (explode('.', $key) as $segment) {
28 2
            if (is_array($array) && array_key_exists($segment, $array)) {
29 1
                $array = $array[$segment];
30
            } else {
31 1
                return $default;
32
            }
33
        }
34
35 1
        return $array;
36
    }
37
38
    /**
39
     * Checks if an item is available in an array.
40
     * Supports dot notation:
41
     * e.g `Arr::has($array, 'section.subsection.item')`
42
     *
43
     * @param array $array
44
     * @param string $key
45
     * @return bool
46
     */
47 4
    public static function has(array $array, $key)
48
    {
49 4
        if (array_key_exists($key, $array)) {
50 1
            return true;
51
        }
52
53 3
        if (strpos($key, '.') === false) {
54 1
            return false;
55
        }
56
57 2
        foreach (explode('.', $key) as $segment) {
58 2
            if (is_array($array) && array_key_exists($segment, $array)) {
59 1
                $array = $array[$segment];
60
            } else {
61 1
                return false;
62
            }
63
        }
64
65 1
        return true;
66
    }
67
68
    /**
69
     * Set an array item to a given value using "dot" notation.
70
     *
71
     * @param  array   $array
72
     * @param  string  $key
73
     * @param  mixed   $value
74
     * @return array
75
     */
76 4
    public static function set(&$array, $key, $value)
77
    {
78 4
        $keys = explode('.', $key);
79
80 4
        while (count($keys) > 1) {
81 2
            $key = array_shift($keys);
82
83
            // If the key doesn't exist at this depth, we will just create an empty array
84
            // to hold the next value, allowing us to create the arrays to hold final
85
            // values at the correct depth. Then we'll keep digging into the array.
86 2
            if (! isset($array[$key]) || ! is_array($array[$key])) {
87 2
                $array[$key] = [];
88
            }
89
90 2
            $array = &$array[$key];
91
        }
92
93 4
        $array[array_shift($keys)] = $value;
94
95 4
        return $array;
96
    }
97
}
98