1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Ariadne Component Library. |
4
|
|
|
* |
5
|
|
|
* (c) Muze <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace arc; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class hash |
14
|
|
|
* Utility methods to work with recursive hashes, setting/getting values and convert hashes to trees. |
15
|
|
|
* @package arc |
16
|
|
|
*/ |
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
|
|
|
public static function get($path, $hash, $default = null) |
29
|
|
|
{ |
30
|
|
|
$result = \arc\path::reduce( $path, function ($result, $item) { |
31
|
|
|
if (is_array( $result ) && array_key_exists( $item, $result )) { |
32
|
|
|
return $result[$item]; |
33
|
|
|
} |
34
|
|
|
}, $hash ); |
35
|
|
|
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
|
|
|
public static function exists($path, $hash) |
45
|
|
|
{ |
46
|
|
|
$path = \arc\path::collapse($path); |
47
|
|
|
$parent = \arc\path::parent($path); |
48
|
|
|
$filename = basename( $path ); |
49
|
|
|
$hash = self::get( $parent, $hash ); |
50
|
|
|
|
51
|
|
|
return (is_array($hash) && array_key_exists( $filename, $hash )); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private static function escape($name) { |
55
|
|
|
return str_replace('/','%2F',$name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static function unescape($name) { |
59
|
|
|
return str_replace('%2F','/',$name); |
60
|
|
|
} |
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
|
|
|
public static function parseName($name) |
68
|
|
|
{ |
69
|
|
|
$elements = explode( '[', $name ); |
70
|
|
|
$path = array(); |
71
|
|
|
foreach ($elements as $element) { |
72
|
|
|
if ($element[ strlen($element) -1 ] === ']') { |
73
|
|
|
$element = substr($element, 0, -1); |
74
|
|
|
} |
75
|
|
|
if ($element[0] === "'") { |
76
|
|
|
$element = substr($element, 1, -1); |
77
|
|
|
} |
78
|
|
|
$path[] = self::escape($element); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return '/'.implode( '/', $path ).'/'; |
82
|
|
|
} |
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
|
|
|
public static function compileName($path, $root = '') |
91
|
|
|
{ |
92
|
|
|
return \arc\path::reduce( $path, function ($result, $item) { |
93
|
|
|
$item = self::unescape($item); |
94
|
|
|
return (!$result ? $item : $result . '[' . $item . ']'); |
95
|
|
|
}, $root ); |
96
|
|
|
} |
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
|
|
|
public static function tree($hash, $parent = null) |
105
|
|
|
{ |
106
|
|
|
if (!isset( $parent )) { |
107
|
|
|
$parent = \arc\tree::expand(); |
108
|
|
|
} |
109
|
|
|
if (is_array( $hash ) || $hash instanceof \Traversable) { |
110
|
|
|
foreach ($hash as $index => $value) { |
111
|
|
|
$child = $parent->appendChild( self::escape($index) ); |
112
|
|
|
if (is_array( $value )) { |
113
|
|
|
self::tree( $value, $child ); |
114
|
|
|
} else { |
115
|
|
|
$child->nodeValue = $value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
$parent->nodeValue = $hash; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $parent; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|