Skip to content

Commit

Permalink
Merge pull request #48 from Galbar/add_support_for_not_symbol
Browse files Browse the repository at this point in the history
add support for '!' as synonym of 'not'
  • Loading branch information
Galbar authored Apr 7, 2021
2 parents 8b5e984 + 48bf831 commit ad493c7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ indexlist = index (',' index)*
arrayslice = index? ':' index? ':' index?
filterexpr = '?(' ors ')'
ors = ands (' ' (or|\|\|) ' ' ands)*
ands = expr (' ' (and|&&) ' ' expr)*
expr = 'not'? (value | comp)
ors = ands (' ' ( 'or' | '\|\|' ) ' ' ands)*
ands = expr (' ' ( 'and' | '&&' ) ' ' expr)*
expr = ( 'not ' | '! ' )? (value | comp)
comp = value ('==' | '!=' | '<' | '>' | '<=' | '>=' | '=~') value
value = (jsonpath | childpath | number | string | boolean | regpattern | null | length)
length = (jsonpath | childpath) '.length'
Expand Down
4 changes: 2 additions & 2 deletions src/Galbar/JsonPath/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class JsonObject
const RE_NEXT_SUBEXPR = '/.*?(\(|\)|\[|\])/';
const RE_OR = '/\s+(or|\|\|)\s+/';
const RE_AND = '/\s+(and|&&)\s+/';
const RE_NOT = '/^not\s+(.*)/';
const RE_NOT = '/^(not|!)\s+(.*)/';

// Tokens
const TOK_ROOT = '$';
Expand Down Expand Up @@ -465,7 +465,7 @@ private function booleanExpressionAnds(&$jsonObject, $expression)
foreach ($values as $subexpr) {
$not = false;
if (preg_match(self::RE_NOT, $subexpr, $match)) {
$subexpr = $match[1];
$subexpr = $match[2];
$not = true;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Galbar/JsonPath/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ public function testGetProvider()
),
"$.store.book[?(not @.category == 'fiction')].price"
),
array(
array(
8.95
),
"$.store.book[?(! @.category == 'fiction')].price"
),
array(
array(
8.95
Expand All @@ -354,12 +360,24 @@ public function testGetProvider()
),
"$.store[?(not @..price or @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(! @..price or @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(not @..price || @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(! @..price || @..color == 'red')].available"
),
array(
false,
"$.store[?(@.price.length == 3)]"
Expand Down Expand Up @@ -623,6 +641,12 @@ public function testSmartGetProvider()
),
"$.store.book[?(not @.category == 'fiction')].price"
),
array(
array(
8.95
),
"$.store.book[?(! @.category == 'fiction')].price"
),
array(
array(
8.95
Expand All @@ -641,12 +665,24 @@ public function testSmartGetProvider()
),
"$.store[?(not @..price or @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(! @..price or @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(not @..price || @..color == 'red')].available"
),
array(
array(
true
),
"$.store[?(! @..price || @..color == 'red')].available"
),
array(
false,
"$.store[?(@.price.length == 3)]"
Expand Down

0 comments on commit ad493c7

Please sign in to comment.