1 | <?php |
||
17 | class hash |
||
18 | { |
||
19 | /** |
||
20 | * Returns the value from $hash matching the given path ($path) or |
||
21 | * if the path cannot be found in the hash, it returns the default |
||
22 | * value ($default). |
||
23 | * @param $path A list of keys to traverse, seperated by '/' |
||
24 | * @param $hash The hash to search |
||
25 | * @param null $default The default value if the path is not found. |
||
26 | * @return mixed|null |
||
27 | */ |
||
28 | 4 | public static function get($path, $hash, $default = null) |
|
29 | { |
||
30 | 4 | $result = \arc\path::reduce( $path, function ($result, $item) { |
|
31 | 4 | if (is_array( $result ) && array_key_exists( $item, $result )) { |
|
32 | 4 | return $result[$item]; |
|
33 | } |
||
34 | 4 | }, $hash ); |
|
35 | 4 | return isset($result) ? $result : $default; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Checks whether the given path ($path) is available in the hash. |
||
40 | * @param $path A list of keys to traverse, seperated by '/' |
||
41 | * @param $hash The hash to search |
||
42 | * @return bool |
||
43 | */ |
||
44 | 2 | public static function exists($path, $hash) |
|
53 | |||
54 | 6 | private static function escape($name) { |
|
57 | |||
58 | 4 | private static function unescape($name) { |
|
61 | |||
62 | /** |
||
63 | * Parse a variable name like 'name[index][index2]' to a key-path like '/name/index/index2/' |
||
64 | * @param $name The variable name to parse |
||
65 | * @return string |
||
66 | */ |
||
67 | 4 | public static function parseName($name) |
|
83 | |||
84 | /** |
||
85 | * Compile a key-path like '/name/index/index2/' to a variable name like 'name[index][index2]' |
||
86 | * @param $path |
||
87 | * @param string $root |
||
88 | * @return mixed |
||
89 | */ |
||
90 | 4 | public static function compileName($path, $root = '') |
|
97 | |||
98 | /** |
||
99 | * Converts a hash to a \arc\tree\NamedNode |
||
100 | * @param $hash |
||
101 | * @param null $parent |
||
102 | * @return tree\NamedNode|null |
||
103 | */ |
||
104 | 2 | public static function tree($hash, $parent = null) |
|
124 | } |
||
125 |