Skip to content

feat: Allow in_array expression to fetch all available items - related to #68 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -127,7 +127,7 @@ expr = ( 'not ' | '! ' )? (value | comp | in_array)
comp = value ('==' | '!=' | '<' | '>' | '<=' | '>=' | '=~') value
value = (jsonpath | childpath | number | string | boolean | regpattern | null | length)
length = (jsonpath | childpath) '.length'
in_array = value 'in' '[' value (',' value)* ']'
in_array = value 'in' ( '[' value (',' value)* ']' | jsonpath )
```

¹`var_name`: the regex roughly translates to "any valid JavaScript variable
Expand Down Expand Up @@ -220,6 +220,7 @@ JsonPath | Result
`$['store']` | The store.
`$..book[*][title, 'category', "author"]` | title, category and author of all books.
`$..book[?(@.author in [$.authors[0], $.authors[2]])]` | All books by "Nigel Rees" or "Herman Melville".
`$..book[?(@.author in $.authors)]` | All books written by an author in the authors list.
`$.store.book[?(@.category == 'fiction' and @.price < 10 or @.color == "red")].price` | Books of fiction with a price lower than 10 or something with of color red. (`and` takes precedence to `or`)

See more examples in the `./tests/Galbar/JsonPath` folder.
Expand Down
2 changes: 1 addition & 1 deletion src/Galbar/JsonPath/Expression/BooleanExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static function booleanExpressionAnds(&$root, &$partial, $expression)
if (preg_match(Language\Regex::BINOP_COMP, $subexpr, $match)) {
$result = Comparison::evaluate($root, $partial, $match[1], $match[2], $match[3]);
} elseif (preg_match(Language\Regex::BINOP_IN_ARRAY, $subexpr, $match)) {
$result = InArray::evaluate($root, $partial, $match[1], $match[2]);
$result = InArray::evaluate($root, $partial, $match[1], end($match));
} else {
$result = Value::evaluate($root, $partial, $subexpr);
}
Expand Down
31 changes: 29 additions & 2 deletions src/Galbar/JsonPath/Expression/InArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace JsonPath\Expression;

use JsonPath\Language;

class InArray
{
const SEPARATOR = ',';
Expand All @@ -17,13 +19,38 @@ public static function evaluate(&$root, &$partial, $attribute, $listExpression)
private static function prepareList(&$root, &$partial, $expression)
{
if (strpos($expression, self::SEPARATOR) === false) {
return [Value::evaluate($root, $partial, trim($expression))];
if ($expression[0] === Language\Token::ROOT){
list($result, $_) = \JsonPath\JsonPath::subtreeGet($root, $root, $expression);
if (!$_) {
$result = reset($result);
}
return $result;
}
else if ($expression[0] === Language\Token::CHILD) {
$expression[0] = Language\Token::ROOT;
list($result, $_) = \JsonPath\JsonPath::subtreeGet($root, $partial, $expression);
if (!$_) {
$result = reset($result);
}
return $result;
}

return [Value::evaluate($root, $partial, self::unwrapExpression($expression))];
}

return array_map(
function ($value) use ($root, $partial) { return Value::evaluate($root, $partial, trim($value)); },
explode(self::SEPARATOR, $expression)
explode(self::SEPARATOR, self::unwrapExpression($expression))
);
}

private static function unwrapExpression($expression) {
if ($expression[0] === Language\Token::SELECTOR_BEGIN
&& $expression[strlen($expression) - 1] === Language\Token::SELECTOR_END
) {
$expression = substr($expression, 1, -1);
}
return trim($expression);
}
}

2 changes: 1 addition & 1 deletion src/Galbar/JsonPath/Language/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Regex
const EXPR_STRING = '/^(?:\'(.*)\'|"(.*)")$/';
const EXPR_REGEX = '/^\/.*\/i?x?$/';
const BINOP_COMP = '/^(.+)\s*(==|!=|<=|>=|<|>|=\~)\s*(.+)$/';
const BINOP_IN_ARRAY = '/^(.+)\s*in\s*\[(.+)\]$/';
const BINOP_IN_ARRAY = '/^(.+)\s*in\s*(\[.+\]|[\$|@]\..+)$/';
const BINOP_OR = '/\s+(or|\|\|)\s+/';
const BINOP_AND = '/\s+(and|&&)\s+/';
const OP_NOT = '/^(not|!)\s+(.*)/';
Expand Down
47 changes: 47 additions & 0 deletions tests/Galbar/JsonPath/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,53 @@ public function testSmartGetProvider()
)
),
"$..book[?(@.author in [$.authors[0], $.authors[2]])]"
),
array(
array(
array(
"category" => "reference",
"author" => "Nigel Rees",
"title" => "Sayings of the Century",
"price" => 8.95,
"available" => true,
)
),
'$..book[?(@.author in [$.authors[*]])]'
),
array(
array (
array (
'category' => 'reference',
'author' => 'Nigel Rees',
'title' => 'Sayings of the Century',
'price' => 8.95,
'available' => true,
),
array (
'category' => 'fiction',
'author' => 'Evelyn Waugh',
'title' => 'Sword of Honour',
'price' => 12.99,
'available' => false,
),
array (
'category' => 'fiction',
'author' => 'Herman Melville',
'title' => 'Moby Dick',
'isbn' => '0-553-21311-3',
'price' => 8.99,
'available' => true,
),
array (
'category' => 'fiction',
'author' => 'J. R. R. Tolkien',
'title' => 'The Lord of the Rings',
'isbn' => '0-395-19395-8',
'price' => 22.99,
'available' => false,
),
),
'$..book[?(@.author in $.authors[*])]'
)
);
}
Expand Down