Completed
Push — master ( b8ee44...86e435 )
by Jim
02:27
created

Arr::has()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/13/2018
6
 * Time: 11:36 PM
7
 */
8
9
namespace TimSDK\Support;
10
11
use ArrayAccess;
12
13
class Arr
14
{
15
    /**
16
     * Determine whether the given value is array accessible.
17
     *
18
     * @param  mixed  $value
19
     * @return bool
20
     */
21
    public static function accessible($value)
22
    {
23
        return is_array($value) || $value instanceof ArrayAccess;
24
    }
25
26
    /**
27
     * Determine if the given key exists in the provided array.
28
     *
29
     * @param  \ArrayAccess|array  $array
30
     * @param  string|int  $key
31
     * @return bool
32
     */
33
    public static function exists($array, $key)
34
    {
35
        if ($array instanceof ArrayAccess) {
36
            return $array->offsetExists($key);
37
        }
38
39
        return array_key_exists($key, $array);
40
    }
41
42
    /**
43
     * Get a subset of the items from the given array.
44
     *
45
     * @param  array  $array
46
     * @param  array|string  $keys
47
     * @return array
48
     */
49
    public static function only($array, $keys)
50
    {
51
        if (is_string($keys)) {
52
            $keys = explode(',', str_replace(' ', '', $keys));
53
        }
54
55
        return array_intersect_key($array, array_flip((array) $keys));
56
    }
57
    
58
    /**
59
     * Set an array item to a given value using "dot" notation.
60
     *
61
     * If no key is given to the method, the entire array will be replaced.
62
     *
63
     * @param array  $array
64
     * @param string $key
65
     * @param mixed  $value
66
     *
67
     * @return array
68
     */
69
    public static function set(&$array, $key, $value)
70
    {
71
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
72
            return $array = $value;
73
        }
74
75
        $keys = explode('.', $key);
76
77
        while (count($keys) > 1) {
78
            $key = array_shift($keys);
79
            // If the key doesn't exist at this depth, we will just create an empty array
80
            // to hold the next value, allowing us to create the arrays to hold final
81
            // values at the correct depth. Then we'll keep digging into the array.
82
            if (!isset($array[$key]) || !is_array($array[$key])) {
83
                $array[$key] = [];
84
            }
85
            $array = &$array[$key];
86
        }
87
        $array[array_shift($keys)] = $value;
88
        return $array;
89
    }
90
91
    /**
92
     * Get an item from an array using "dot" notation.
93
     *
94
     * @param array  $array
95
     * @param string $key
96
     * @param mixed  $default
97
     *
98
     * @return mixed
99
     */
100
    public static function get($array, $key, $default = null)
101
    {
102
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
103
            return $array;
104
        }
105
106
        if (isset($array[$key])) {
107
            return $array[$key];
108
        }
109
110
        foreach (explode('.', $key) as $segment) {
111
            if (!is_array($array) || !array_key_exists($segment, $array)) {
112
                return timsdk_value($default);
113
            }
114
            $array = $array[$segment];
115
        }
116
117
        return $array;
118
    }
119
120
    /**
121
     * To determine Whether an array item exists
122
     *
123
     * @static
124
     * @param array        $array
125
     * @param array|string $keys
126
     * @return bool
127
     */
128
    public static function has($array, $keys)
129
    {
130
        if (is_null($keys)) {
0 ignored issues
show
introduced by
The condition is_null($keys) is always false.
Loading history...
131
            return false;
132
        }
133
134
        $keys = (array) $keys;
135
136
        if (! $array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array 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...
137
            return false;
138
        }
139
140
        if ($keys === []) {
141
            return false;
142
        }
143
144
        foreach ($keys as $key) {
145
            $subKeyArray = $array;
146
147
            if (static::exists($array, $key)) {
148
                continue;
149
            }
150
151
            foreach (explode('.', $key) as $segment) {
152
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
153
                    $subKeyArray = $subKeyArray[$segment];
154
                } else {
155
                    return false;
156
                }
157
            }
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * Remove one or many array items from a given array using "dot" notation.
165
     *
166
     * @param array        $array
167
     * @param array|string $keys
168
     */
169
    public static function forget(&$array, $keys)
170
    {
171
        $original = &$array;
172
        foreach ((array) $keys as $key) {
173
            $parts = explode('.', $key);
174
            while (count($parts) > 1) {
175
                $part = array_shift($parts);
176
177
                if (!static::exists($array, $part)) {
178
                    return ;
179
                }
180
181
                if (is_array($array[$part])) {
182
                    $array = &$array[$part];
183
                }
184
            }
185
            unset($array[array_shift($parts)]);
186
            // clean up after each pass
187
            $array = &$original;
188
        }
189
    }
190
}
191