Skip to content

Commit de9a971

Browse files
committed
Handle standalone @ for selecting current node as value
1 parent 618b081 commit de9a971

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/Filters/QueryMatchFilter.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class QueryMatchFilter extends AbstractFilter
2020
protected const MATCH_QUERY_NEGATION_WRAPPED = '^(?<negate>!)\((?<logicalexpr>.+)\)$';
2121
protected const MATCH_QUERY_NEGATION_UNWRAPPED = '^(?<negate>!)(?<logicalexpr>.+)$';
2222
protected const MATCH_QUERY_OPERATORS = '
23-
@(\.(?<key>[^\s<>!=]+)|\[["\']?(?<keySquare>.*?)["\']?\])
23+
(@\.(?<key>[^\s<>!=]+)|@\[["\']?(?<keySquare>.*?)["\']?\]|(?<node>@))
2424
(\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+?(?=(&&|$))))?
2525
(\s*(?<logicaland>&&)\s*)?
2626
';
@@ -60,9 +60,9 @@ public function filter($collection): array
6060
for ($logicalAndNum = 0; $logicalAndNum < \count($matches[0]); $logicalAndNum++) {
6161
$key = $matches['key'][$logicalAndNum] ?: $matches['keySquare'][$logicalAndNum];
6262

63-
if ($key === '') {
64-
throw new RuntimeException('Malformed filter query: key was not set');
65-
}
63+
// if ($key === '') {
64+
// throw new RuntimeException('Malformed filter query: key was not set');
65+
// }
6666

6767
$operator = $matches['operator'][$logicalAndNum] ?? null;
6868
$comparisonValue = $matches['comparisonValue'][$logicalAndNum] ?? null;
@@ -87,14 +87,19 @@ public function filter($collection): array
8787
$value1 = null;
8888

8989
$notNothing = AccessHelper::keyExists($value, $key, $this->magicIsAllowed);
90-
if ($notNothing) {
91-
$value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed);
92-
} elseif (\str_contains($key, '.')) {
93-
$foundValue = (new JSONPath($value))->find($key)->getData();
94-
if ($foundValue) {
95-
$value1 = $foundValue[0];
96-
$notNothing = true;
90+
if ($key) {
91+
if ($notNothing) {
92+
$value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed);
93+
} elseif (\str_contains($key, '.')) {
94+
$foundValue = (new JSONPath($value))->find($key)->getData();
95+
if ($foundValue) {
96+
$value1 = $foundValue[0];
97+
$notNothing = true;
98+
}
9799
}
100+
} else {
101+
$value1 = $value;
102+
$notNothing = true;
98103
}
99104

100105
$comparisonResult = null;

tests/data/baselineFailedQueries.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ bracket_notation_with_quoted_closing_bracket_literal
22
dot_notation_with_key_root_literal
33
filter_expression_with_current_object
44
filter_expression_with_different_grouped_operators
5-
filter_expression_with_equals_on_array_of_numbers
65
filter_expression_with_tautological_comparison
76
union_with_wildcard_and_number
87
array_slice_with_large_number_for_end_and_negative_step

0 commit comments

Comments
 (0)