| 1 |
<?php |
| 2 |
namespace JmesPath; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class TreeInterpreter |
| 8 |
{ |
| 9 |
|
| 10 |
private $fnDispatcher; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public function __construct(?callable $fnDispatcher = null) |
| 18 |
{ |
| 19 |
$this->fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance(); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function visit(array $node, $data) |
| 31 |
{ |
| 32 |
return $this->dispatch($node, $data); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
$from = isset($node['from']) ? $node['from'] : null; |
| 73 |
switch ($from) { |
| 74 |
case 'object': |
| 75 |
if (!Utils::isObject($left)) { |
| 76 |
return null; |
| 77 |
} |
| 78 |
break; |
| 79 |
case 'array': |
| 80 |
if (!Utils::isArray($left)) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
break; |
| 84 |
default: |
| 85 |
if (!is_array($left) && !($left instanceof \stdClass)) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$collected = []; |
| 91 |
foreach ((array) $left as $val) { |
| 92 |
$result = $this->dispatch($node['children'][1], $val); |
| 93 |
if ($result !== null) { |
| 94 |
$collected[] = $result; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $collected; |
| 99 |
|
| 100 |
case 'flatten': |
| 101 |
static $skipElement = []; |
| 102 |
$value = $this->dispatch($node['children'][0], $value); |
| 103 |
|
| 104 |
if (!Utils::isArray($value)) { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
$merged = []; |
| 109 |
foreach ($value as $values) { |
| 110 |
|
| 111 |
if (is_array($values) && array_key_exists(0, $values)) { |
| 112 |
$merged = array_merge($merged, $values); |
| 113 |
} elseif ($values !== $skipElement) { |
| 114 |
$merged[] = $values; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return $merged; |
| 119 |
|
| 120 |
case 'literal': |
| 121 |
return $node['value']; |
| 122 |
|
| 123 |
case 'current': |
| 124 |
return $value; |
| 125 |
|
| 126 |
case 'or': |
| 127 |
$result = $this->dispatch($node['children'][0], $value); |
| 128 |
return Utils::isTruthy($result) |
| 129 |
? $result |
| 130 |
: $this->dispatch($node['children'][1], $value); |
| 131 |
|
| 132 |
case 'and': |
| 133 |
$result = $this->dispatch($node['children'][0], $value); |
| 134 |
return Utils::isTruthy($result) |
| 135 |
? $this->dispatch($node['children'][1], $value) |
| 136 |
: $result; |
| 137 |
|
| 138 |
case 'not': |
| 139 |
return !Utils::isTruthy( |
| 140 |
$this->dispatch($node['children'][0], $value) |
| 141 |
); |
| 142 |
|
| 143 |
case 'pipe': |
| 144 |
return $this->dispatch( |
| 145 |
$node['children'][1], |
| 146 |
$this->dispatch($node['children'][0], $value) |
| 147 |
); |
| 148 |
|
| 149 |
case 'multi_select_list': |
| 150 |
if ($value === null) { |
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
$collected = []; |
| 155 |
foreach ($node['children'] as $node) { |
| 156 |
$collected[] = $this->dispatch($node, $value); |
| 157 |
} |
| 158 |
|
| 159 |
return $collected; |
| 160 |
|
| 161 |
case 'multi_select_hash': |
| 162 |
if ($value === null) { |
| 163 |
return null; |
| 164 |
} |
| 165 |
|
| 166 |
$collected = []; |
| 167 |
foreach ($node['children'] as $node) { |
| 168 |
$collected[$node['value']] = $this->dispatch( |
| 169 |
$node['children'][0], |
| 170 |
$value |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
return $collected; |
| 175 |
|
| 176 |
case 'comparator': |
| 177 |
$left = $this->dispatch($node['children'][0], $value); |
| 178 |
$right = $this->dispatch($node['children'][1], $value); |
| 179 |
if ($node['value'] == '==') { |
| 180 |
return Utils::isEqual($left, $right); |
| 181 |
} elseif ($node['value'] == '!=') { |
| 182 |
return !Utils::isEqual($left, $right); |
| 183 |
} else { |
| 184 |
return self::relativeCmp($left, $right, $node['value']); |
| 185 |
} |
| 186 |
|
| 187 |
case 'condition': |
| 188 |
return Utils::isTruthy($this->dispatch($node['children'][0], $value)) |
| 189 |
? $this->dispatch($node['children'][1], $value) |
| 190 |
: null; |
| 191 |
|
| 192 |
case 'function': |
| 193 |
$args = []; |
| 194 |
foreach ($node['children'] as $arg) { |
| 195 |
$args[] = $this->dispatch($arg, $value); |
| 196 |
} |
| 197 |
return $dispatcher($node['value'], $args); |
| 198 |
|
| 199 |
case 'slice': |
| 200 |
return is_string($value) || Utils::isArray($value) |
| 201 |
? Utils::slice( |
| 202 |
$value, |
| 203 |
$node['value'][0], |
| 204 |
$node['value'][1], |
| 205 |
$node['value'][2] |
| 206 |
) : null; |
| 207 |
|
| 208 |
case 'expref': |
| 209 |
$apply = $node['children'][0]; |
| 210 |
return function ($value) use ($apply) { |
| 211 |
return $this->visit($apply, $value); |
| 212 |
}; |
| 213 |
|
| 214 |
default: |
| 215 |
throw new \RuntimeException("Unknown node type: {$node['type']}"); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private static function relativeCmp($left, $right, $cmp) |
| 223 |
{ |
| 224 |
if (!(is_int($left) || is_float($left)) || !(is_int($right) || is_float($right))) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
switch ($cmp) { |
| 229 |
case '>': return $left > $right; |
| 230 |
case '>=': return $left >= $right; |
| 231 |
case '<': return $left < $right; |
| 232 |
case '<=': return $left <= $right; |
| 233 |
default: throw new \RuntimeException("Invalid comparison: $cmp"); |
| 234 |
} |
| 235 |
} |
| 236 |
} |