1 | <?php |
||
6 | class Access |
||
7 | { |
||
8 | use ValueTrait; |
||
9 | |||
10 | /** |
||
11 | * Set an array item to a given value using "dot" notation. |
||
12 | * If no key is given to the method, the entire array will be replaced. |
||
13 | * |
||
14 | * @param array $array |
||
15 | * @param string $key |
||
16 | * @param mixed $value |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | 11 | public function set(array $array, $key, $value) |
|
21 | { |
||
22 | 11 | if ($key === null) { |
|
23 | 1 | return $value; |
|
24 | } |
||
25 | |||
26 | 11 | $keys = explode('.', $key); |
|
27 | 11 | $current = &$array; |
|
28 | |||
29 | 11 | while (count($keys) > 1) { |
|
30 | 9 | $key = array_shift($keys); |
|
31 | |||
32 | // If the key doesn't exist at this depth, we will just create an empty array |
||
33 | // to hold the next value, allowing us to create the arrays to hold final |
||
34 | // values at the correct depth. Then we'll keep digging into the array. |
||
35 | 9 | if (! isset($current[$key]) || ! is_array($current[$key])) { |
|
36 | 4 | $current[$key] = []; |
|
37 | 4 | } |
|
38 | |||
39 | 9 | $current = &$current[$key]; |
|
40 | 9 | } |
|
41 | |||
42 | 11 | $current[array_shift($keys)] = $value; |
|
43 | |||
44 | 11 | return $array; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get an item from an array using "dot" notation. |
||
49 | * If key dont exist, you get a default value back. |
||
50 | * |
||
51 | * @param array $array |
||
52 | * @param string[]|callable|null $key |
||
53 | * @param mixed $default |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | 5 | public function get(array $array, $key = null, $default = null) |
|
77 | |||
78 | /** |
||
79 | * Add an element to the array at a specific location |
||
80 | * using the "dot" notation. |
||
81 | * |
||
82 | * @param array $array |
||
83 | * @param $key |
||
84 | * @param $value |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 4 | public function add(array $array, $key, $value) |
|
101 | |||
102 | /** |
||
103 | * Check if any item or items exist in an array using "dot" notation. |
||
104 | * |
||
105 | * @param array $array |
||
106 | * @param string|array $keys |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | 1 | public function any(array $array, $keys) |
|
120 | |||
121 | /** |
||
122 | * Check if an item exists in an array using "dot" notation. |
||
123 | * |
||
124 | * @param array $array |
||
125 | * @param string $key |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 11 | public function has(array $array, $key) |
|
149 | |||
150 | /** |
||
151 | * Updates data at the given path. |
||
152 | * |
||
153 | * @param array $array |
||
154 | * @param array|string[] $key |
||
155 | * @param callable $callback Callback to update the value. |
||
156 | * |
||
157 | * @return mixed Updated data. |
||
158 | */ |
||
159 | 1 | public function update(array $array, $key, callable $callback) |
|
176 | |||
177 | /** |
||
178 | * Remove one or many array items from a given array using "dot" notation. |
||
179 | * |
||
180 | * @param array $array |
||
181 | * @param array|string $keys |
||
182 | */ |
||
183 | 9 | public function forget(array $array, $keys) |
|
212 | } |
||
213 |