Arr::forget()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 34
rs 8.8333
cc 7
nc 6
nop 2
1
<?php
2
3
namespace CloudyCity\KuaishouMarketingSDK\Kernel\Support;
4
5
/**
6
 * Array helper from Illuminate\Support\Arr.
7
 */
8
class Arr
9
{
10
    /**
11
     * Get all of the given array except for a specified array of items.
12
     *
13
     * @param array        $array
14
     * @param array|string $keys
15
     *
16
     * @return array
17
     */
18
    public static function except(array $array, $keys)
19
    {
20
        static::forget($array, $keys);
21
22
        return $array;
23
    }
24
25
    /**
26
     * Determine if the given key exists in the provided array.
27
     *
28
     * @param array      $array
29
     * @param string|int $key
30
     *
31
     * @return bool
32
     */
33
    public static function exists(array $array, $key)
34
    {
35
        return array_key_exists($key, $array);
36
    }
37
38
    /**
39
     * Remove one or many array items from a given array using "dot" notation.
40
     *
41
     * @param array        $array
42
     * @param array|string $keys
43
     */
44
    public static function forget(array &$array, $keys)
45
    {
46
        $original = &$array;
47
48
        $keys = (array) $keys;
49
50
        if (0 === count($keys)) {
51
            return;
52
        }
53
54
        foreach ($keys as $key) {
55
            // if the exact key exists in the top-level, remove it
56
            if (static::exists($array, $key)) {
57
                unset($array[$key]);
58
59
                continue;
60
            }
61
62
            $parts = explode('.', $key);
63
64
            // clean up before each pass
65
            $array = &$original;
66
67
            while (count($parts) > 1) {
68
                $part = array_shift($parts);
69
70
                if (isset($array[$part]) && is_array($array[$part])) {
71
                    $array = &$array[$part];
72
                } else {
73
                    continue 2;
74
                }
75
            }
76
77
            unset($array[array_shift($parts)]);
78
        }
79
    }
80
81
    /**
82
     * Get an item from an array using "dot" notation.
83
     *
84
     * @param array  $array
85
     * @param string $key
86
     * @param mixed  $default
87
     *
88
     * @return mixed
89
     */
90
    public static function get(array $array, $key, $default = null)
91
    {
92
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
93
            return $array;
94
        }
95
96
        if (static::exists($array, $key)) {
97
            return $array[$key];
98
        }
99
100
        foreach (explode('.', $key) as $segment) {
101
            if (static::exists($array, $segment)) {
102
                $array = $array[$segment];
103
            } else {
104
                return $default;
105
            }
106
        }
107
108
        return $array;
109
    }
110
111
    /**
112
     * Set an array item to a given value using "dot" notation.
113
     *
114
     * If no key is given to the method, the entire array will be replaced.
115
     *
116
     * @param array  $array
117
     * @param string $key
118
     * @param mixed  $value
119
     *
120
     * @return array
121
     */
122
    public static function set(array &$array, $key, $value)
123
    {
124
        $keys = explode('.', $key);
125
126
        while (count($keys) > 1) {
127
            $key = array_shift($keys);
128
129
            // If the key doesn't exist at this depth, we will just create an empty array
130
            // to hold the next value, allowing us to create the arrays to hold final
131
            // values at the correct depth. Then we'll keep digging into the array.
132
            if (!isset($array[$key]) || !is_array($array[$key])) {
133
                $array[$key] = [];
134
            }
135
136
            $array = &$array[$key];
137
        }
138
139
        $array[array_shift($keys)] = $value;
140
141
        return $array;
142
    }
143
}
144