forked from SoftCreatR/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryMatchFilter.php
More file actions
218 lines (180 loc) · 8.1 KB
/
QueryMatchFilter.php
File metadata and controls
218 lines (180 loc) · 8.1 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
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
declare(strict_types=1);
namespace Flow\JSONPath\Filters;
use Flow\JSONPath\AccessHelper;
use Flow\JSONPath\JSONPath;
use Flow\JSONPath\JSONPathException;
use JsonException;
use RuntimeException;
use const JSON_THROW_ON_ERROR;
use const PREG_OFFSET_CAPTURE;
use const PREG_UNMATCHED_AS_NULL;
class QueryMatchFilter extends AbstractFilter
{
protected const MATCH_QUERY_NEGATION_WRAPPED = '^(?<negate>!)\((?<logicalexpr>.+)\)$';
protected const MATCH_QUERY_NEGATION_UNWRAPPED = '^(?<negate>!)(?<logicalexpr>.+)$';
protected const MATCH_QUERY_OPERATORS = '
(@\.(?<key>[^\s<>!=]+)|@\[["\']?(?<keySquare>.*?)["\']?\]|(?<node>@)|(%group(?<group>\d+)%))
(\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+?(?=\s*(?:&&|$|\|\||%))))?
(\s*(?<logicalandor>&&|\|\|)\s*)?
';
protected const MATCH_GROUPED_EXPRESSION = '#\([^)(]*+(?:(?R)[^)(]*)*+\)#';
/**
* @throws JSONPathException
*/
public function filter($collection): array
{
$filterExpression = $this->token->value;
$negateFilter = false;
if (
\preg_match('/' . static::MATCH_QUERY_NEGATION_WRAPPED . '/x', $filterExpression, $negationMatches)
|| \preg_match('/' . static::MATCH_QUERY_NEGATION_UNWRAPPED . '/x', $filterExpression, $negationMatches)
) {
$negateFilter = true;
$filterExpression = $negationMatches['logicalexpr'];
}
$filterGroups = [];
if (
\preg_match_all(
static::MATCH_GROUPED_EXPRESSION,
$filterExpression,
$matches,
PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL
)
) {
foreach ($matches[0] as $i => $matchesGroup) {
$test = \substr($matchesGroup[0], 1, -1);
//sanity check that our group is a group and not something within a string or regular expression
if (\preg_match('/' . static::MATCH_QUERY_OPERATORS . '/x', $test)) {
$filterGroups[$i] = $test;
$filterExpression = \str_replace($matchesGroup[0], "%group{$i}%", $filterExpression);
}
}
}
$match = \preg_match_all(
'/' . static::MATCH_QUERY_OPERATORS . '/x',
$filterExpression,
$matches,
PREG_UNMATCHED_AS_NULL
);
if (
$match === false
|| !isset($matches[1][0])
|| isset($matches['logicalandor'][\array_key_last($matches['logicalandor'])])
) {
throw new RuntimeException('Malformed filter query');
}
$return = [];
$matchCount = \count($matches[0]);
for ($expressionPart = 0; $expressionPart < $matchCount; $expressionPart++) {
$filteredCollection = $collection;
$logicalJoin = $expressionPart > 0 ? $matches['logicalandor'][$expressionPart - 1] : null;
if ($logicalJoin === '&&') {
//Restrict the nodes we need to look at to those already meeting criteria
$filteredCollection = $return;
$return = [];
}
//Processing a group
if ($matches['group'][$expressionPart] !== null) {
$filter = '$[?(' . $filterGroups[$matches['group'][$expressionPart]] . ')]';
$resolve = (new JSONPath($filteredCollection))->find($filter)->getData();
$return = $resolve;
continue;
}
//Process a normal expression
$key = $matches['key'][$expressionPart] ?: $matches['keySquare'][$expressionPart];
$operator = $matches['operator'][$expressionPart] ?? null;
$comparisonValue = $matches['comparisonValue'][$expressionPart] ?? null;
if (\is_string($comparisonValue)) {
$comparisonValue = \preg_replace('/^\'/', '"', $comparisonValue);
$comparisonValue = \preg_replace('/\'$/', '"', $comparisonValue);
try {
$comparisonValue = \json_decode($comparisonValue, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
//Leave $comparisonValue as raw (e.g. regular express or non quote wrapped string)
}
}
foreach ($filteredCollection as $nodeIndex => $node) {
if ($logicalJoin === '||' && \array_key_exists($nodeIndex, $return)) {
//Short-circuit, node already exists in output due to previous test
continue;
}
$selectedNode = null;
$notNothing = AccessHelper::keyExists($node, $key, $this->magicIsAllowed);
if ($key) {
if ($notNothing) {
$selectedNode = AccessHelper::getValue($node, $key, $this->magicIsAllowed);
} elseif (\str_contains($key, '.')) {
$foundValue = (new JSONPath($node))->find($key)->getData();
if ($foundValue) {
$selectedNode = $foundValue[0];
$notNothing = true;
}
}
} else {
//Node selection was plain @
$selectedNode = $node;
$notNothing = true;
}
$comparisonResult = null;
if ($notNothing) {
$comparisonResult = match ($operator) {
null => AccessHelper::keyExists($node, $key, $this->magicIsAllowed) || (!$key),
"=", "==" => $this->compareEquals($selectedNode, $comparisonValue),
"!=", "!==", "<>" => !$this->compareEquals($selectedNode, $comparisonValue),
'=~' => @\preg_match($comparisonValue, $selectedNode),
'<' => $this->compareLessThan($selectedNode, $comparisonValue),
'<=' => $this->compareLessThan($selectedNode, $comparisonValue)
|| $this->compareEquals($selectedNode, $comparisonValue),
'>' => $this->compareLessThan($comparisonValue, $selectedNode), //rfc semantics
'>=' => $this->compareLessThan($comparisonValue, $selectedNode) //rfc semantics
|| $this->compareEquals($selectedNode, $comparisonValue),
"in" => \is_array($comparisonValue) && \in_array($selectedNode, $comparisonValue, true),
'nin', "!in" => \is_array($comparisonValue) && !\in_array($selectedNode, $comparisonValue, true)
};
}
if ($negateFilter) {
$comparisonResult = !$comparisonResult;
}
if ($comparisonResult) {
$return[$nodeIndex] = $node;
}
}
}
//Keep out returned nodes in the same order they were defined in the original collection
\ksort($return);
return $return;
}
protected function isNumber($value): bool
{
return !\is_string($value) && \is_numeric($value);
}
protected function compareEquals($a, $b): bool
{
$type_a = \gettype($a);
$type_b = \gettype($b);
if ($type_a === $type_b || ($this->isNumber($a) && $this->isNumber($b))) {
//Primitives or Numbers
if ($a === null || \is_scalar($a)) {
/** @noinspection TypeUnsafeComparisonInspection */
return $a == $b;
}
//Object/Array
//@TODO array and object comparison
}
return false;
}
protected function compareLessThan($a, $b): bool
{
if ((\is_string($a) && \is_string($b)) || ($this->isNumber($a) && $this->isNumber($b))) {
//numerical and string comparison supported only
return $a < $b;
}
return false;
}
}