|
6 | 6 | * @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License |
7 | 7 | */ |
8 | 8 |
|
| 9 | +declare(strict_types=1); |
| 10 | + |
9 | 11 | namespace Flow\JSONPath\Filters; |
10 | 12 |
|
11 | 13 | use Flow\JSONPath\AccessHelper; |
12 | 14 | use Flow\JSONPath\JSONPath; |
13 | 15 | use Flow\JSONPath\JSONPathException; |
| 16 | +use JsonException; |
14 | 17 | use RuntimeException; |
15 | 18 |
|
| 19 | +use const JSON_THROW_ON_ERROR; |
| 20 | +use const PREG_OFFSET_CAPTURE; |
| 21 | +use const PREG_UNMATCHED_AS_NULL; |
| 22 | + |
16 | 23 | class QueryMatchFilter extends AbstractFilter |
17 | 24 | { |
| 25 | + protected const MATCH_QUERY_NEGATION_WRAPPED = '^(?<negate>!)\((?<logicalexpr>.+)\)$'; |
| 26 | + |
| 27 | + protected const MATCH_QUERY_NEGATION_UNWRAPPED = '^(?<negate>!)(?<logicalexpr>.+)$'; |
| 28 | + |
18 | 29 | protected const MATCH_QUERY_OPERATORS = ' |
19 | | - @(\.(?<key>[^\s<>!=]+)|\[["\']?(?<keySquare>.*?)["\']?\]) |
20 | | - (\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))? |
| 30 | + (@\.(?<key>[^\s<>!=]+)|@\[["\']?(?<keySquare>.*?)["\']?\]|(?<node>@)|(%group(?<group>\d+)%)) |
| 31 | + (\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+?(?=(&&|$|\|\||%))))? |
| 32 | + (\s*(?<logicalandor>&&|\|\|)\s*)? |
21 | 33 | '; |
22 | 34 |
|
| 35 | + protected const MATCH_GROUPED_EXPRESSION = '#\([^)(]*+(?:(?R)[^)(]*)*+\)#'; |
| 36 | + |
23 | 37 | /** |
24 | 38 | * @throws JSONPathException |
25 | 39 | */ |
26 | 40 | public function filter($collection): array |
27 | 41 | { |
28 | | - \preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches); |
29 | | - |
30 | | - if (!isset($matches[1])) { |
31 | | - throw new RuntimeException('Malformed filter query'); |
32 | | - } |
33 | | - |
34 | | - $key = $matches['key'] ?: $matches['keySquare']; |
35 | | - |
36 | | - if ($key === '') { |
37 | | - throw new RuntimeException('Malformed filter query: key was not set'); |
| 42 | + $filterExpression = $this->token->value; |
| 43 | + $negateFilter = false; |
| 44 | + if ( |
| 45 | + \preg_match('/' . static::MATCH_QUERY_NEGATION_WRAPPED . '/x', $filterExpression, $negationMatches) |
| 46 | + || \preg_match('/' . static::MATCH_QUERY_NEGATION_UNWRAPPED . '/x', $filterExpression, $negationMatches) |
| 47 | + ) { |
| 48 | + $negateFilter = true; |
| 49 | + $filterExpression = $negationMatches['logicalexpr']; |
38 | 50 | } |
39 | 51 |
|
40 | | - $operator = $matches['operator'] ?? null; |
41 | | - $comparisonValue = $matches['comparisonValue'] ?? null; |
42 | | - |
43 | | - if (\is_string($comparisonValue)) { |
44 | | - if (\str_starts_with($comparisonValue, "[") && \str_ends_with($comparisonValue, "]")) { |
45 | | - $comparisonValue = \substr($comparisonValue, 1, -1); |
46 | | - $comparisonValue = \preg_replace('/^[\'"]/', '', $comparisonValue); |
47 | | - $comparisonValue = \preg_replace('/[\'"]$/', '', $comparisonValue); |
48 | | - $comparisonValue = \preg_replace('/[\'"], *[\'"]/', ',', $comparisonValue); |
49 | | - $comparisonValue = \array_map('trim', \explode(",", $comparisonValue)); |
50 | | - } else { |
51 | | - $comparisonValue = \preg_replace('/^[\'"]/', '', $comparisonValue); |
52 | | - $comparisonValue = \preg_replace('/[\'"]$/', '', $comparisonValue); |
53 | | - |
54 | | - if (\strtolower($comparisonValue) === 'false') { |
55 | | - $comparisonValue = false; |
56 | | - } elseif (\strtolower($comparisonValue) === 'true') { |
57 | | - $comparisonValue = true; |
58 | | - } elseif (\strtolower($comparisonValue) === 'null') { |
59 | | - $comparisonValue = null; |
| 52 | + $filterGroups = []; |
| 53 | + if ( |
| 54 | + \preg_match_all( |
| 55 | + static::MATCH_GROUPED_EXPRESSION, |
| 56 | + $filterExpression, |
| 57 | + $matches, |
| 58 | + PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL |
| 59 | + ) |
| 60 | + ) { |
| 61 | + foreach ($matches[0] as $i => $matchesGroup) { |
| 62 | + $test = \substr($matchesGroup[0], 1, -1); |
| 63 | + //sanity check that our group is a group and not something within a string or regular expression |
| 64 | + if (\preg_match('/' . static::MATCH_QUERY_OPERATORS . '/x', $test)) { |
| 65 | + $filterGroups[$i] = $test; |
| 66 | + $filterExpression = \str_replace($matchesGroup[0], "%group{$i}%", $filterExpression); |
60 | 67 | } |
61 | 68 | } |
62 | 69 | } |
63 | 70 |
|
| 71 | + $match = \preg_match_all( |
| 72 | + '/' . static::MATCH_QUERY_OPERATORS . '/x', |
| 73 | + $filterExpression, |
| 74 | + $matches, |
| 75 | + PREG_UNMATCHED_AS_NULL |
| 76 | + ); |
| 77 | + |
| 78 | + if ( |
| 79 | + $match === false |
| 80 | + || !isset($matches[1][0]) |
| 81 | + || isset($matches['logicalandor'][\array_key_last($matches['logicalandor'])]) |
| 82 | + ) { |
| 83 | + throw new RuntimeException('Malformed filter query'); |
| 84 | + } |
| 85 | + |
64 | 86 | $return = []; |
| 87 | + $matchCount = \count($matches[0]); |
65 | 88 |
|
66 | | - foreach ($collection as $value) { |
67 | | - $value1 = null; |
| 89 | + for ($expressionPart = 0; $expressionPart < $matchCount; $expressionPart++) { |
| 90 | + $filteredCollection = $collection; |
| 91 | + $logicalJoin = $expressionPart > 0 ? $matches['logicalandor'][$expressionPart - 1] : null; |
68 | 92 |
|
69 | | - if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) { |
70 | | - $value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed); |
71 | | - } elseif (\str_contains($key, '.')) { |
72 | | - $value1 = (new JSONPath($value))->find($key)->getData()[0] ?? ''; |
| 93 | + if ($logicalJoin === '&&') { |
| 94 | + //Restrict the nodes we need to look at to those already meeting criteria |
| 95 | + $filteredCollection = $return; |
| 96 | + $return = []; |
73 | 97 | } |
74 | 98 |
|
75 | | - if ($value1) { |
76 | | - if ($operator === null) { |
77 | | - $return[] = $value; |
78 | | - } |
| 99 | + //Processing a group |
| 100 | + if ($matches['group'][$expressionPart] !== null) { |
| 101 | + $filter = '$[?(' . $filterGroups[$matches['group'][$expressionPart]] . ')]'; |
| 102 | + $resolve = (new JSONPath($filteredCollection))->find($filter)->getData(); |
| 103 | + $return = $resolve; |
79 | 104 |
|
80 | | - /** @noinspection TypeUnsafeComparisonInspection */ |
81 | | - // phpcs:ignore -- This is a loose comparison by design. |
82 | | - if (($operator === '=' || $operator === '==') && $value1 == $comparisonValue) { |
83 | | - $return[] = $value; |
84 | | - } |
| 105 | + continue; |
| 106 | + } |
85 | 107 |
|
86 | | - /** @noinspection TypeUnsafeComparisonInspection */ |
87 | | - // phpcs:ignore -- This is a loose comparison by design. |
88 | | - if (($operator === '!=' || $operator === '!==' || $operator === '<>') && $value1 != $comparisonValue) { |
89 | | - $return[] = $value; |
90 | | - } |
| 108 | + //Process a normal expression |
| 109 | + $key = $matches['key'][$expressionPart] ?: $matches['keySquare'][$expressionPart]; |
91 | 110 |
|
92 | | - if ($operator === '=~' && @\preg_match($comparisonValue, $value1)) { |
93 | | - $return[] = $value; |
94 | | - } |
| 111 | + $operator = $matches['operator'][$expressionPart] ?? null; |
| 112 | + $comparisonValue = $matches['comparisonValue'][$expressionPart] ?? null; |
95 | 113 |
|
96 | | - if ($operator === '>' && $value1 > $comparisonValue) { |
97 | | - $return[] = $value; |
| 114 | + if (\is_string($comparisonValue)) { |
| 115 | + $comparisonValue = \preg_replace('/^\'/', '"', $comparisonValue); |
| 116 | + $comparisonValue = \preg_replace('/\'$/', '"', $comparisonValue); |
| 117 | + |
| 118 | + try { |
| 119 | + $comparisonValue = \json_decode($comparisonValue, true, 512, JSON_THROW_ON_ERROR); |
| 120 | + } catch (JsonException) { |
| 121 | + //Leave $comparisonValue as raw (e.g. regular express or non quote wrapped string) |
98 | 122 | } |
| 123 | + } |
99 | 124 |
|
100 | | - if ($operator === '>=' && $value1 >= $comparisonValue) { |
101 | | - $return[] = $value; |
| 125 | + foreach ($filteredCollection as $nodeIndex => $node) { |
| 126 | + if ($logicalJoin === '||' && \array_key_exists($nodeIndex, $return)) { |
| 127 | + //Short-circuit, node already exists in output due to previous test |
| 128 | + continue; |
102 | 129 | } |
103 | 130 |
|
104 | | - if ($operator === '<' && $value1 < $comparisonValue) { |
105 | | - $return[] = $value; |
| 131 | + $selectedNode = null; |
| 132 | + $notNothing = AccessHelper::keyExists($node, $key, $this->magicIsAllowed); |
| 133 | + |
| 134 | + if ($key) { |
| 135 | + if ($notNothing) { |
| 136 | + $selectedNode = AccessHelper::getValue($node, $key, $this->magicIsAllowed); |
| 137 | + } elseif (\str_contains($key, '.')) { |
| 138 | + $foundValue = (new JSONPath($node))->find($key)->getData(); |
| 139 | + |
| 140 | + if ($foundValue) { |
| 141 | + $selectedNode = $foundValue[0]; |
| 142 | + $notNothing = true; |
| 143 | + } |
| 144 | + } |
| 145 | + } else { |
| 146 | + //Node selection was plain @ |
| 147 | + $selectedNode = $node; |
| 148 | + $notNothing = true; |
106 | 149 | } |
107 | 150 |
|
108 | | - if ($operator === '<=' && $value1 <= $comparisonValue) { |
109 | | - $return[] = $value; |
| 151 | + $comparisonResult = null; |
| 152 | + |
| 153 | + if ($notNothing) { |
| 154 | + $comparisonResult = match ($operator) { |
| 155 | + null => AccessHelper::keyExists($node, $key, $this->magicIsAllowed) || (!$key), |
| 156 | + "=", "==" => $this->compareEquals($selectedNode, $comparisonValue), |
| 157 | + "!=", "!==", "<>" => !$this->compareEquals($selectedNode, $comparisonValue), |
| 158 | + '=~' => @\preg_match($comparisonValue, $selectedNode), |
| 159 | + '<' => $this->compareLessThan($selectedNode, $comparisonValue), |
| 160 | + '<=' => $this->compareLessThan($selectedNode, $comparisonValue) |
| 161 | + || $this->compareEquals($selectedNode, $comparisonValue), |
| 162 | + '>' => $this->compareLessThan($comparisonValue, $selectedNode), //rfc semantics |
| 163 | + '>=' => $this->compareLessThan($comparisonValue, $selectedNode) //rfc semantics |
| 164 | + || $this->compareEquals($selectedNode, $comparisonValue), |
| 165 | + "in" => \is_array($comparisonValue) && \in_array($selectedNode, $comparisonValue, true), |
| 166 | + 'nin', "!in" => \is_array($comparisonValue) && !\in_array($selectedNode, $comparisonValue, true) |
| 167 | + }; |
110 | 168 | } |
111 | 169 |
|
112 | | - if ($operator === 'in' && \is_array($comparisonValue) && \in_array($value1, $comparisonValue, false)) { |
113 | | - $return[] = $value; |
| 170 | + if ($negateFilter) { |
| 171 | + $comparisonResult = !$comparisonResult; |
114 | 172 | } |
115 | 173 |
|
116 | | - if ( |
117 | | - ($operator === 'nin' || $operator === '!in') |
118 | | - && \is_array($comparisonValue) |
119 | | - && !\in_array($value1, $comparisonValue, false) |
120 | | - ) { |
121 | | - $return[] = $value; |
| 174 | + if ($comparisonResult) { |
| 175 | + $return[$nodeIndex] = $node; |
122 | 176 | } |
123 | 177 | } |
124 | 178 | } |
125 | 179 |
|
| 180 | + //Keep out returned nodes in the same order they were defined in the original collection |
| 181 | + \ksort($return); |
| 182 | + |
126 | 183 | return $return; |
127 | 184 | } |
| 185 | + |
| 186 | + protected function isNumber($value): bool |
| 187 | + { |
| 188 | + return !\is_string($value) && \is_numeric($value); |
| 189 | + } |
| 190 | + |
| 191 | + protected function compareEquals($a, $b): bool |
| 192 | + { |
| 193 | + $type_a = \gettype($a); |
| 194 | + $type_b = \gettype($b); |
| 195 | + |
| 196 | + if ($type_a === $type_b || ($this->isNumber($a) && $this->isNumber($b))) { |
| 197 | + //Primitives or Numbers |
| 198 | + if ($a === null || \is_scalar($a)) { |
| 199 | + /** @noinspection TypeUnsafeComparisonInspection */ |
| 200 | + return $a == $b; |
| 201 | + } |
| 202 | + //Object/Array |
| 203 | + //@TODO array and object comparison |
| 204 | + } |
| 205 | + |
| 206 | + return false; |
| 207 | + } |
| 208 | + |
| 209 | + protected function compareLessThan($a, $b): bool |
| 210 | + { |
| 211 | + if ((\is_string($a) && \is_string($b)) || ($this->isNumber($a) && $this->isNumber($b))) { |
| 212 | + //numerical and string comparison supported only |
| 213 | + return $a < $b; |
| 214 | + } |
| 215 | + |
| 216 | + return false; |
| 217 | + } |
128 | 218 | } |
0 commit comments