forked from phpDocumentor/json-path
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.php
More file actions
224 lines (189 loc) · 6.32 KB
/
Executor.php
File metadata and controls
224 lines (189 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/
namespace phpDocumentor\JsonPath;
use ArrayAccess;
use Generator;
use phpDocumentor\JsonPath\AST\Expression;
use phpDocumentor\JsonPath\AST\FieldName;
use phpDocumentor\JsonPath\AST\PathNode;
use phpDocumentor\JsonPath\AST\QueryNode;
use phpDocumentor\JsonPath\AST\Wildcard;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyPath;
use function array_merge;
use function current;
use function is_array;
use function is_iterable;
use function is_object;
use function is_string;
use function iterator_to_array;
use function str_starts_with;
use function strrpos;
use function substr;
final class Executor
{
private readonly PropertyAccessor $propertyAccessor;
public function __construct()
{
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}
/** @return mixed */
public function evaluate(QueryNode $query, mixed $currentElement, mixed $rootElement = null)
{
return $query->visit($this, $currentElement, $rootElement);
}
public function evaluateEqualsComparison(
mixed $root,
mixed $currentObject,
QueryNode $left,
QueryNode $right,
): bool {
$leftValue = $this->toValue($this->evaluate($left, $currentObject, $root));
$rightValue = $this->toValue($this->evaluate($right, $currentObject, $root));
if (is_string($rightValue)) {
return ((string) $leftValue) === $rightValue;
}
return $leftValue === $rightValue;
}
public function evaluateNotEqualsComparison(
mixed $root,
mixed $currentObject,
QueryNode $left,
QueryNode $right,
): bool {
return ! $this->evaluateEqualsComparison($root, $currentObject, $left, $right);
}
public function evaluateStartsWithComparison(
mixed $root,
mixed $currentObject,
QueryNode $left,
QueryNode $right,
): bool {
$leftValue = $this->toValue($this->evaluate($left, $currentObject, $root));
$rightValue = $this->toValue($this->evaluate($right, $currentObject, $root));
return str_starts_with((string) $leftValue, (string) $rightValue);
}
public function evaluateContainsComparison(
mixed $root,
mixed $currentObject,
QueryNode $left,
QueryNode $right,
): bool {
$leftValue = $this->toValue($this->evaluate($left, $currentObject, $root));
$rightValue = $this->toValue($this->evaluate($right, $currentObject, $root));
if (is_iterable($leftValue)) {
foreach ($leftValue as $value) {
if (is_string($rightValue) && ((string) $value) === $rightValue) {
return true;
}
if ($value === $rightValue) {
return true;
}
}
return false;
}
return false;
}
/**
* @param Generator<mixed>|mixed $value
*
* @return mixed
*/
private function toValue($value)
{
if ($value instanceof Generator) {
$result = iterator_to_array($value, false);
return current($result);
}
return $value;
}
/** @return mixed */
public function evaluatePath(mixed $root, mixed $currentElement, PathNode ...$nodes)
{
$result = $currentElement;
foreach ($nodes as $node) {
$result = $this->evaluate($node, $result, $root);
}
return $result;
}
/** @return mixed */
public function evaluateFunctionCall(
mixed $root,
mixed $currentElement,
string $functionName,
QueryNode ...$arguments,
) {
switch ($functionName) {
case 'type':
$class = $this->evaluate($arguments[0], $currentElement, $root)::class;
$isNamespacedClass = strrpos($class, '\\');
if ($isNamespacedClass !== false) {
return substr($class, $isNamespacedClass + 1);
}
return $class;
}
}
/** @return Generator<mixed> */
public function evaluateFieldAccess(mixed $currentElement, FieldName|Expression $fieldName): Generator
{
if ($fieldName instanceof Wildcard && is_iterable($currentElement)) {
foreach ($currentElement as $element) {
foreach ($element as $value) {
yield $value;
}
}
return;
}
if ($currentElement instanceof Generator) {
foreach ($currentElement as $element) {
foreach ($this->evaluateFieldAccess($element, $fieldName) as $result) {
yield $result;
}
}
return;
}
if (
(is_array($currentElement) || $currentElement instanceof ArrayAccess) &&
isset($currentElement[$fieldName->getName()])
) {
yield $currentElement[$fieldName->getName()];
} elseif (is_iterable($currentElement)) {
$result = [];
foreach ($currentElement as $element) {
foreach ($this->evaluateFieldAccess($element, $fieldName) as $row) {
if (is_iterable($row)) {
$result = array_merge(
$result,
is_array($row) ? $row : iterator_to_array($row, false),
);
continue;
}
$result[] = $row;
}
}
yield from $result;
} else {
if (is_object($currentElement) === false) {
return;
}
if (
$this->propertyAccessor->isReadable(
$currentElement,
new PropertyPath($fieldName->getName()),
) === false
) {
return;
}
yield $this->propertyAccessor->getValue($currentElement, new PropertyPath($fieldName->getName()));
}
}
}