1
|
|
|
<?php |
2
|
|
|
namespace JmesPath; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Tree visitor used to evaluates JMESPath AST expressions. |
6
|
|
|
*/ |
7
|
|
|
class TreeInterpreter |
8
|
|
|
{ |
9
|
|
|
/** @var callable */ |
10
|
|
|
private $fnDispatcher; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param callable $fnDispatcher Function dispatching function that accepts |
14
|
|
|
* a function name argument and an array of |
15
|
|
|
* function arguments and returns the result. |
16
|
|
|
*/ |
17
|
|
|
public function __construct(callable $fnDispatcher = null) |
18
|
|
|
{ |
19
|
|
|
$this->fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Visits each node in a JMESPath AST and returns the evaluated result. |
24
|
|
|
* |
25
|
|
|
* @param array $node JMESPath AST node |
26
|
|
|
* @param mixed $data Data to evaluate |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function visit(array $node, $data) |
31
|
|
|
{ |
32
|
|
|
return $this->dispatch($node, $data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Recursively traverses an AST using depth-first, pre-order traversal. |
37
|
|
|
* The evaluation logic for each node type is embedded into a large switch |
38
|
|
|
* statement to avoid the cost of "double dispatch". |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
private function dispatch(array $node, $value) |
42
|
|
|
{ |
43
|
|
|
$dispatcher = $this->fnDispatcher; |
44
|
|
|
|
45
|
|
|
switch ($node['type']) { |
46
|
|
|
|
47
|
|
|
case 'field': |
48
|
|
|
if (is_array($value) || $value instanceof \ArrayAccess) { |
49
|
|
|
return isset($value[$node['value']]) ? $value[$node['value']] : null; |
50
|
|
|
} elseif ($value instanceof \stdClass) { |
51
|
|
|
return isset($value->{$node['value']}) ? $value->{$node['value']} : null; |
52
|
|
|
} |
53
|
|
|
return null; |
54
|
|
|
|
55
|
|
|
case 'subexpression': |
56
|
|
|
return $this->dispatch( |
57
|
|
|
$node['children'][1], |
58
|
|
|
$this->dispatch($node['children'][0], $value) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
case 'index': |
62
|
|
|
if (!Utils::isArray($value)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
$idx = $node['value'] >= 0 |
66
|
|
|
? $node['value'] |
67
|
|
|
: $node['value'] + count($value); |
68
|
|
|
return isset($value[$idx]) ? $value[$idx] : null; |
69
|
|
|
|
70
|
|
|
case 'projection': |
71
|
|
|
$left = $this->dispatch($node['children'][0], $value); |
72
|
|
|
switch ($node['from']) { |
73
|
|
|
case 'object': |
74
|
|
|
if (!Utils::isObject($left)) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
break; |
78
|
|
|
case 'array': |
79
|
|
|
if (!Utils::isArray($left)) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
break; |
83
|
|
|
default: |
84
|
|
|
if (!is_array($left) || !($left instanceof \stdClass)) { |
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$collected = []; |
90
|
|
|
foreach ((array) $left as $val) { |
91
|
|
|
$result = $this->dispatch($node['children'][1], $val); |
92
|
|
|
if ($result !== null) { |
93
|
|
|
$collected[] = $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $collected; |
98
|
|
|
|
99
|
|
|
case 'flatten': |
100
|
|
|
static $skipElement = []; |
101
|
|
|
$value = $this->dispatch($node['children'][0], $value); |
102
|
|
|
|
103
|
|
|
if (!Utils::isArray($value)) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$merged = []; |
108
|
|
|
foreach ($value as $values) { |
109
|
|
|
// Only merge up arrays lists and not hashes |
110
|
|
|
if (is_array($values) && isset($values[0])) { |
111
|
|
|
$merged = array_merge($merged, $values); |
112
|
|
|
} elseif ($values !== $skipElement) { |
113
|
|
|
$merged[] = $values; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $merged; |
118
|
|
|
|
119
|
|
|
case 'literal': |
120
|
|
|
return $node['value']; |
121
|
|
|
|
122
|
|
|
case 'current': |
123
|
|
|
return $value; |
124
|
|
|
|
125
|
|
|
case 'or': |
126
|
|
|
$result = $this->dispatch($node['children'][0], $value); |
127
|
|
|
return Utils::isTruthy($result) |
128
|
|
|
? $result |
129
|
|
|
: $this->dispatch($node['children'][1], $value); |
130
|
|
|
|
131
|
|
|
case 'and': |
132
|
|
|
$result = $this->dispatch($node['children'][0], $value); |
133
|
|
|
return Utils::isTruthy($result) |
134
|
|
|
? $this->dispatch($node['children'][1], $value) |
135
|
|
|
: $result; |
136
|
|
|
|
137
|
|
|
case 'not': |
138
|
|
|
return !Utils::isTruthy( |
139
|
|
|
$this->dispatch($node['children'][0], $value) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
case 'pipe': |
143
|
|
|
return $this->dispatch( |
144
|
|
|
$node['children'][1], |
145
|
|
|
$this->dispatch($node['children'][0], $value) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
case 'multi_select_list': |
149
|
|
|
if ($value === null) { |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$collected = []; |
154
|
|
|
foreach ($node['children'] as $node) { |
155
|
|
|
$collected[] = $this->dispatch($node, $value); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $collected; |
159
|
|
|
|
160
|
|
|
case 'multi_select_hash': |
161
|
|
|
if ($value === null) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$collected = []; |
166
|
|
|
foreach ($node['children'] as $node) { |
167
|
|
|
$collected[$node['value']] = $this->dispatch( |
168
|
|
|
$node['children'][0], |
169
|
|
|
$value |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $collected; |
174
|
|
|
|
175
|
|
|
case 'comparator': |
176
|
|
|
$left = $this->dispatch($node['children'][0], $value); |
177
|
|
|
$right = $this->dispatch($node['children'][1], $value); |
178
|
|
|
if ($node['value'] == '==') { |
179
|
|
|
return Utils::isEqual($left, $right); |
180
|
|
|
} elseif ($node['value'] == '!=') { |
181
|
|
|
return !Utils::isEqual($left, $right); |
182
|
|
|
} else { |
183
|
|
|
return self::relativeCmp($left, $right, $node['value']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
case 'condition': |
187
|
|
|
return Utils::isTruthy($this->dispatch($node['children'][0], $value)) |
188
|
|
|
? $this->dispatch($node['children'][1], $value) |
189
|
|
|
: null; |
190
|
|
|
|
191
|
|
|
case 'function': |
192
|
|
|
$args = []; |
193
|
|
|
foreach ($node['children'] as $arg) { |
194
|
|
|
$args[] = $this->dispatch($arg, $value); |
195
|
|
|
} |
196
|
|
|
return $dispatcher($node['value'], $args); |
197
|
|
|
|
198
|
|
|
case 'slice': |
199
|
|
|
return is_string($value) || Utils::isArray($value) |
200
|
|
|
? Utils::slice( |
201
|
|
|
$value, |
202
|
|
|
$node['value'][0], |
203
|
|
|
$node['value'][1], |
204
|
|
|
$node['value'][2] |
205
|
|
|
) : null; |
206
|
|
|
|
207
|
|
|
case 'expref': |
208
|
|
|
$apply = $node['children'][0]; |
209
|
|
|
return function ($value) use ($apply) { |
210
|
|
|
return $this->visit($apply, $value); |
211
|
|
|
}; |
212
|
|
|
|
213
|
|
|
default: |
214
|
|
|
throw new \RuntimeException("Unknown node type: {$node['type']}"); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
private static function relativeCmp($left, $right, $cmp) |
222
|
|
|
{ |
223
|
|
|
if (!(is_int($left) || is_float($left)) || !(is_int($right) || is_float($right))) { |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
switch ($cmp) { |
228
|
|
|
case '>': return $left > $right; |
229
|
|
|
case '>=': return $left >= $right; |
230
|
|
|
case '<': return $left < $right; |
231
|
|
|
case '<=': return $left <= $right; |
232
|
|
|
default: throw new \RuntimeException("Invalid comparison: $cmp"); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|