Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ Script expressions are not supported as the original author intended because:

So here are the types of query expressions that are supported:

[?(@._KEY_ _OPERATOR_ _VALUE_)] // <, >, !=, and ==
[?(@._KEY_ _OPERATOR_ _VALUE_)] // <, >, !=, == and in
Eg.
[?(@.title == "A string")] //
[?(@.title = "A string")]
// A single equals is not an assignment but the SQL-style of '=='
[?(@.title in ["A string", "Another string"])]

Similar projects
----------------
Expand Down
38 changes: 25 additions & 13 deletions src/Flow/JSONPath/Filters/QueryMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class QueryMatchFilter extends AbstractFilter
{
const MATCH_QUERY_OPERATORS = '
@(\.(?<key>\w+)|\[["\'](?<keySquare>.*?)["\']\])
(\s*(?<operator>==|=|>|<)\s*(?<comparisonValue>\S.+))?
(\s*(?<operator>==|=|>|<|in)\s*(?<comparisonValue>\S.+))?
';

/**
Expand All @@ -34,18 +34,27 @@ public function filter($collection)
$operator = isset($matches['operator']) ? $matches['operator'] : null;
$comparisonValue = isset($matches['comparisonValue']) ? $matches['comparisonValue'] : null;

if (strtolower($comparisonValue) === "false") {
$comparisonValue = false;
}
if (strtolower($comparisonValue) === "true") {
$comparisonValue = true;
}
if (strtolower($comparisonValue) === "null") {
$comparisonValue = null;
}
if (substr($comparisonValue, 0, 1) === "[" && substr($comparisonValue, -1) === "]") {
$comparisonValue = substr($comparisonValue, 1, -1);
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"],[ ]{0,}[\'"]/', ',', $comparisonValue);

$comparisonValue = explode(",", $comparisonValue);
} else {
if (strtolower($comparisonValue) === "false") {
$comparisonValue = false;
}
if (strtolower($comparisonValue) === "true") {
$comparisonValue = true;
}
if (strtolower($comparisonValue) === "null") {
$comparisonValue = null;
}

$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
}

foreach ($collection as $value) {
if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
Expand All @@ -67,10 +76,13 @@ public function filter($collection)
if ($operator == "<" && $value1 < $comparisonValue) {
$return[] = $value;
}
if ($operator == "in" && in_array($value1, $comparisonValue)) {
$return[] = $value;
}
}
}

return $return;
}
}


2 changes: 1 addition & 1 deletion src/Flow/JSONPath/Filters/QueryResultFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function filter($collection)
return $result;
}
}


12 changes: 12 additions & 0 deletions tests/JSONPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public function testQueryMatchEquals()
$this->assertEquals($results[0], 'The Lord of the Rings');
}

/**
* $..books[?(@.author in ["J. R. R. Tolkien", "Nigel Rees"])]
* Filter books that have a title in ["...", "..."]
*/
public function testQueryMatchIn()
{
$result = (new JSONPath($this->exampleData(rand(0, 1))))->find('$..books[?(@.author in ["J. R. R. Tolkien", "Nigel Rees"])].title');
$resultArray = $result->data();
$expectedArray = ['The Lord of the Rings', 'Sayings of the Century'];
$this->assertEquals(ksort($expectedArray), ksort($resultArray));
}

/**
* $.store.books[*].author
*/
Expand Down