Completed
Push — master ( da1cfa...dccc7f )
by Vladimir
03:06
created

Arr   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 0
dl 0
loc 157
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A accessible() 0 4 2
A exists() 0 8 2
C has() 0 22 7
B get() 0 19 6
A set() 0 17 4
C forget() 0 28 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Helpers;
6
7
use ArrayAccess;
8
9
class Arr
10
{
11
    /**
12
     * Determine whether the given value is array accessible.
13
     *
14
     * @param mixed $value
15
     *
16
     * @return bool
17
     */
18 8
    public static function accessible($value): bool
19
    {
20 8
        return is_array($value) || $value instanceof ArrayAccess;
21
    }
22
23
    /**
24
     * Determine if the given key exists in the provided array.
25
     *
26
     * @param \ArrayAccess|array $array
27
     * @param string|int         $key
28
     *
29
     * @return bool
30
     */
31 15
    public static function exists($array, $key): bool
32
    {
33 15
        if ($array instanceof ArrayAccess) {
34 2
            return $array->offsetExists($key);
35
        }
36
37 15
        return array_key_exists($key, $array);
38
    }
39
40
    /**
41
     * Check if an item or items exist in an array using "dot" notation.
42
     *
43
     * @param  \ArrayAccess|array $array
44
     * @param  array              $keys
45
     *
46
     * @return bool
47
     */
48 3
    public static function has($array, array $keys): bool
49
    {
50 3
        if ($keys === []) {
51 1
            return false;
52
        }
53
54 3
        foreach ($keys as $key) {
55 3
            $subKeyArray = $array;
56 3
            if (static::exists($array, $key)) {
57 3
                continue;
58
            }
59 3
            foreach (explode('.', $key) as $segment) {
60 3
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
61 2
                    $subKeyArray = $subKeyArray[$segment];
62
                } else {
63 3
                    return false;
64
                }
65
            }
66
        }
67
68 3
        return true;
69
    }
70
71
    /**
72
     * Get an item from an array using "dot" notation.
73
     *
74
     * @param \ArrayAccess|array $array
75
     * @param string|null        $key
76
     * @param mixed              $default
77
     *
78
     * @return mixed
79
     */
80 12
    public static function get($array, ?string $key, $default = null)
81
    {
82 12
        if ($key === null) {
83 3
            return $array;
84
        }
85
86 12
        if (static::exists($array, $key)) {
87 10
            return $array[$key];
88
        }
89 6
        foreach (explode('.', $key) as $segment) {
90 6
            if (static::accessible($array) && static::exists($array, $segment)) {
91 2
                $array = $array[$segment];
92
            } else {
93 6
                return value($default);
94
            }
95
        }
96
97 2
        return $array;
98
    }
99
100
    /**
101
     * Set an array item to a given value using "dot" notation.
102
     *
103
     * If no key is given to the method, the entire array will be replaced.
104
     *
105
     * @param  array  $array
106
     * @param  string $key
107
     * @param  mixed  $value
108
     *
109
     * @return array
110
     */
111 2
    public static function set(array &$array, string $key, $value): array
112
    {
113 2
        $keys = explode('.', $key);
114 2
        while (count($keys) > 1) {
115 2
            $key = array_shift($keys);
116
            // If the key doesn't exist at this depth, we will just create an empty array
117
            // to hold the next value, allowing us to create the arrays to hold final
118
            // values at the correct depth. Then we'll keep digging into the array.
119 2
            if (!isset($array[$key]) || !is_array($array[$key])) {
120 2
                $array[$key] = [];
121
            }
122 2
            $array = &$array[$key];
123
        }
124 2
        $array[array_shift($keys)] = $value;
125
126 2
        return $array;
127
    }
128
129
    /**
130
     * Remove one or many array items from a given array using "dot" notation.
131
     *
132
     * @param  array        $array
133
     * @param  array|string $keys
134
     *
135
     * @return void
136
     */
137 1
    public static function forget(&$array, $keys): void
138
    {
139 1
        $original = &$array;
140 1
        $keys = (array) $keys;
141 1
        if (count($keys) === 0) {
142 1
            return;
143
        }
144
145 1
        foreach ($keys as $key) {
146
            // if the exact key exists in the top-level, remove it
147 1
            if (static::exists($array, $key)) {
148 1
                unset($array[$key]);
149 1
                continue;
150
            }
151 1
            $parts = explode('.', $key);
152
            // clean up before each pass
153 1
            $array = &$original;
154 1
            while (count($parts) > 1) {
155 1
                $part = array_shift($parts);
156 1
                if (isset($array[$part]) && is_array($array[$part])) {
157 1
                    $array = &$array[$part];
158
                } else {
159 1
                    continue 2;
160
                }
161
            }
162 1
            unset($array[array_shift($parts)]);
163
        }
164 1
    }
165
}
166