Skip to content

Commit 33feedf

Browse files
authored
Merge pull request #78 from Schrank/filter-subpaths
Fixes #20 Filter subpaths
2 parents 43bfb6a + 25c7c11 commit 33feedf

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/Filters/QueryMatchFilter.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Flow\JSONPath\Filters;
1010

1111
use Flow\JSONPath\AccessHelper;
12+
use Flow\JSONPath\JSONPath;
1213
use RuntimeException;
1314

1415
class QueryMatchFilter extends AbstractFilter
@@ -59,9 +60,13 @@ public function filter($collection): array
5960
$return = [];
6061

6162
foreach ($collection as $value) {
63+
$value1 = null;
6264
if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
6365
$value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed);
64-
66+
} elseif (\str_contains($key, '.')) {
67+
$value1 = (new JSONPath($value))->find($key)->getData()[0] ?? '';
68+
}
69+
if ($value1) {
6570
if ($operator === null && $value1) {
6671
$return[] = $value;
6772
}

tests/QueryTest.php

+20-11
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
use PHPUnit\Framework\TestCase;
1818
use RuntimeException;
1919

20-
use const STDERR;
21-
2220
class QueryTest extends TestCase
2321
{
22+
public static array $baselineFailedQueries;
23+
24+
public static function setUpBeforeClass(): void
25+
{
26+
parent::setUpBeforeClass();
27+
self::$baselineFailedQueries = \array_map('trim', \file(__DIR__ . '/data/baselineFailedQueries.txt'));
28+
}
29+
2430
/**
2531
* This method aims to test the current implementation against
2632
* all queries listed on https://cburgmer.github.io/json-path-comparison/
@@ -70,21 +76,24 @@ public function testQueries(
7076
// assert in these cases. There might be still some false positives
7177
// (e.g. multidimensional comparisons), but that's okay, I guess. Maybe,
7278
// we can also find a way around that in the future.
79+
$message = "==========================\n";
80+
$message .= "Query: {$query}\n\nMore information: {$url}\n";
81+
$message .= "==========================\n\n";
7382
self::assertEqualsCanonicalizing(
7483
\json_decode($consensus, true),
75-
\json_decode($results, true)
84+
\json_decode($results, true),
85+
$message
7686
);
7787
} catch (ExpectationFailedException) {
78-
$e = $e->getComparisonFailure();
79-
80-
\fwrite(STDERR, "==========================\n");
81-
\fwrite(STDERR, "Query: {$query}\n\n{$e->toString()}\nMore information: {$url}\n");
82-
\fwrite(STDERR, "==========================\n\n");
88+
if (!\in_array($id, self::$baselineFailedQueries, true)) {
89+
throw new ExpectationFailedException(
90+
$e->getMessage() . "\nQuery: {$query}\n\nMore information: {$url}",
91+
$e->getComparisonFailure()
92+
);
93+
}
8394
}
8495
} catch (JSONPathException $e) {
85-
\fwrite(STDERR, "==========================\n");
86-
\fwrite(STDERR, "Query: {$query}\n\n{$e->getMessage()}\n");
87-
\fwrite(STDERR, "==========================\n\n");
96+
// ignore
8897
} catch (RuntimeException) {
8998
// ignore
9099
}

tests/data/baselineFailedQueries.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
array_slice_with_large_number_for_end_and_negative_step
2+
array_slice_with_large_number_for_start_end_negative_step
3+
array_slice_with_negative_step
4+
array_slice_with_negative_step_on_partially_overlapping_array
5+
bracket_notation_with_negative_number_on_short_array
6+
bracket_notation_with_number_on_object
7+
bracket_notation_with_quoted_escaped_backslash
8+
bracket_notation_with_quoted_escaped_single_quote
9+
bracket_notation_with_quoted_special_characters_combined
10+
bracket_notation_with_quoted_wildcard_literal_on_object_without_key
11+
bracket_notation_with_wildcard_after_recursive_descent
12+
dot_notation_with_number
13+
dot_notation_with_number_-1
14+
dot_notation_with_wildcard_after_recursive_descent
15+
filter_expression_with_boolean_and_operator
16+
filter_expression_with_boolean_or_operator
17+
filter_expression_with_bracket_notation_with_-1
18+
filter_expression_with_equals
19+
filter_expression_with_equals_false
20+
filter_expression_with_equals_null
21+
filter_expression_with_equals_number_with_fraction
22+
filter_expression_with_equals_true
23+
filter_expression_with_equals_with_root_reference
24+
filter_expression_with_greater_than
25+
filter_expression_with_greater_than_or_equal
26+
filter_expression_with_less_than
27+
filter_expression_with_less_than_or_equal
28+
filter_expression_with_not_equals
29+
filter_expression_with_value
30+
script_expression
31+
union_with_filter
32+
union_with_repeated_matches_after_dot_notation_with_wildcard
33+
union_with_slice_and_number

0 commit comments

Comments
 (0)