1 | <?php |
||
7 | final class Dot { |
||
8 | /** |
||
9 | * Get the value of the element at the given dot key path. |
||
10 | * |
||
11 | * @param array $array Multi-dimensional array to access. |
||
12 | * @param string $path Dot-notation key path. e.g. `parent.child` |
||
13 | * @param null|mixed $default Default value to return |
||
14 | * |
||
15 | * @return mixed |
||
16 | */ |
||
17 | public static function get(array $array, $path, $default = null) { |
||
27 | |||
28 | /** |
||
29 | * Has returns true if an element exists at the given dot key path. |
||
30 | * |
||
31 | * @param array $array Multi-dimensionsal array to search. |
||
32 | * @param string $path Dot-notation key path to search. |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | public static function has(array $array, $path) { |
||
41 | |||
42 | /** |
||
43 | * Set a value at a certain path by creating missing elements and overwriting non-array values. |
||
44 | * |
||
45 | * If any of the visited elements is not an array, it will be replaced with an array. |
||
46 | * |
||
47 | * This will overwrite existing non-array values. |
||
48 | * |
||
49 | * @param array $array |
||
50 | * @param string $path |
||
51 | * @param mixed $value |
||
52 | */ |
||
53 | public static function set(array &$array, $path, $value) { |
||
56 | |||
57 | /** |
||
58 | * trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
||
59 | * |
||
60 | * If any of the visited elements is not an array, a \RuntimeException is thrown. |
||
61 | * |
||
62 | * Use this if you want to avoid overwriting existing non-array values. |
||
63 | * |
||
64 | * @param array $array |
||
65 | * @param string $path |
||
66 | * @param mixed $value |
||
67 | * |
||
68 | * @throws \RuntimeException |
||
69 | */ |
||
70 | public static function trySet(array &$array, $path, $value) { |
||
73 | |||
74 | /** |
||
75 | * Remove an element if it exists. |
||
76 | * |
||
77 | * @param array $array |
||
78 | * @param string $path |
||
79 | */ |
||
80 | public static function remove(array &$array, $path) { |
||
97 | |||
98 | /** |
||
99 | * Flatten the array into a single dimension array with dot paths as keys. |
||
100 | * |
||
101 | * @param array $array |
||
102 | * @param null|string $parent |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public static function flatten(array $array, $parent = null) { |
||
121 | |||
122 | /** |
||
123 | * escapeKey escapes an individual part of a key. |
||
124 | * |
||
125 | * @param string $key |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | private static function escapeKey($key) { |
||
134 | |||
135 | /** |
||
136 | * @param string $path |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | private static function extractKeys($path) { |
||
150 | |||
151 | private static function setHelper(array &$array, $path, $value, $strict = true) { |
||
162 | |||
163 | private static function prepareNode(array &$node, $key, $path, $strict) { |
||
178 | } |
||
179 |