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
|
|
|
* Recursively search a hash for a key-path and return its value or the default value if the key-path is not found. |
21
|
|
|
* @param $path |
22
|
|
|
* @param $hash |
23
|
|
|
* @param null $default |
24
|
|
|
* @return mixed|null |
25
|
|
|
*/ |
26
|
2 |
|
public static function get($path, $hash, $default = null) |
27
|
|
|
{ |
28
|
|
|
$result = \arc\path::reduce( $path, function ($result, $item) { |
29
|
2 |
|
if (is_array( $result ) && array_key_exists( $item, $result )) { |
30
|
2 |
|
return $result[$item]; |
31
|
|
|
} |
32
|
2 |
|
}, $hash ); |
33
|
2 |
|
return isset($result) ? $result : $default; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check if a key-path is defined in the hash. |
38
|
|
|
* @param $path |
39
|
|
|
* @param $hash |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
1 |
|
public static function exists($path, $hash) |
43
|
|
|
{ |
44
|
1 |
|
$parent = \arc\path::parent($path); |
45
|
1 |
|
$filename = basename( $path ); |
46
|
1 |
|
$hash = self::get( $parent, $hash ); |
47
|
|
|
|
48
|
1 |
|
return (is_array($hash) && array_key_exists( $filename, $hash )); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
3 |
|
private static function escape($name) { |
52
|
3 |
|
return str_replace('/','%2F',$name); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
private static function unescape($name) { |
56
|
2 |
|
return str_replace('%2F','/',$name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Parse a variable name like 'name[index][index2]' to a key-path like '/name/index/index2/' |
61
|
|
|
* @param $name |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
public static function parseName($name) |
65
|
|
|
{ |
66
|
2 |
|
$elements = explode( '[', $name ); |
67
|
2 |
|
$path = array(); |
68
|
2 |
|
foreach ($elements as $element) { |
69
|
2 |
|
if ($element[ strlen($element) -1 ] === ']') { |
70
|
2 |
|
$element = substr($element, 0, -1); |
71
|
2 |
|
} |
72
|
2 |
|
if ($element[0] === "'") { |
73
|
|
|
$element = substr($element, 1, -1); |
74
|
|
|
} |
75
|
2 |
|
$path[] = self::escape($element); |
76
|
2 |
|
} |
77
|
|
|
|
78
|
2 |
|
return '/'.implode( '/', $path ).'/'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Compile a key-path like '/name/index/index2/' to a variable name like 'name[index][index2]' |
83
|
|
|
* @param $path |
84
|
|
|
* @param string $root |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public static function compileName($path, $root = '') |
88
|
|
|
{ |
89
|
2 |
|
return \arc\path::reduce( $path, function ($result, $item) { |
90
|
2 |
|
$item = self::unescape($item); |
91
|
2 |
|
return (!$result ? $item : $result . '[' . $item . ']'); |
92
|
2 |
|
}, $root ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Generate a NamedNode tree from a hash. |
97
|
|
|
* @param $hash |
98
|
|
|
* @param null $parent |
99
|
|
|
* @return tree\NamedNode|null |
100
|
|
|
*/ |
101
|
1 |
|
public static function tree($hash, $parent = null) |
102
|
|
|
{ |
103
|
1 |
|
if (!isset( $parent )) { |
104
|
1 |
|
$parent = \arc\tree::expand(); |
105
|
1 |
|
} |
106
|
1 |
|
if (is_array( $hash ) || $hash instanceof \Traversable) { |
107
|
1 |
|
foreach ($hash as $index => $value) { |
108
|
1 |
|
$child = $parent->appendChild( self::escape($index) ); |
109
|
1 |
|
if (is_array( $value )) { |
110
|
1 |
|
self::tree( $value, $child ); |
|
|
|
|
111
|
1 |
|
} else { |
112
|
1 |
|
$child->nodeValue = $value; |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
1 |
|
} else { |
116
|
|
|
$parent->nodeValue = $hash; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $parent; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: