Skip to content

Commit

Permalink
Release 0.6.4
Browse files Browse the repository at this point in the history
 - Added support for `nin` operator
 - Added support for greater than or equal operator (`>=`)
 - Added support for less or equal operator (`<=`)
  • Loading branch information
SoftCreatR committed Oct 18, 2020
1 parent 6676bff commit 79da94b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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_)] // <, >, !=, ==, in and nin
[?(@._KEY_ _OPERATOR_ _VALUE_)] // <, >, <=, >=, !=, ==, in and nin
Eg.
[?(@.title == "A string")] //
[?(@.title = "A string")]
Expand Down Expand Up @@ -138,8 +138,14 @@ The original JsonPath implementations is available at [http://code.google.com/p/
Changelog
---------

### 0.6.4
- Removed unnecessary type casting, that caused problems under certain circumstances
- Added support for `nin` operator
- Added support for greater than or equal operator (`>=`)
- Added support for less or equal operator (`<=`)

### 0.6.3
- Added support for `IN` expressions
- Added support for `in` operator
- Fixed evaluation on indexed object

### 0.6.x
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "softcreatr/jsonpath",
"description": "JSONPath implementation for parsing, searching and flattening arrays",
"version": "0.6.3",
"version": "0.6.4",
"license": "MIT",
"authors": [
{
Expand Down
12 changes: 10 additions & 2 deletions src/Filters/QueryMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class QueryMatchFilter extends AbstractFilter
{
public const MATCH_QUERY_OPERATORS = '
@(\.(?<key>[^ =]+)|\[["\']?(?<keySquare>.*?)["\']?\])
(\s*(?<operator>==|=|<>|!==|!=|>|<|in|nin)\s*(?<comparisonValue>.+))?
(\s*(?<operator>==|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))?
';

/**
Expand Down Expand Up @@ -93,15 +93,23 @@ public function filter($collection): array
$return[] = $value;
}

if ($operator === '>=' && $value1 >= $comparisonValue) {
$return[] = $value;
}

if ($operator === '<' && $value1 < $comparisonValue) {
$return[] = $value;
}

if ($operator === '<=' && $value1 <= $comparisonValue) {
$return[] = $value;
}

if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue, true)) {
$return[] = $value;
}

if ($operator === 'nin' && is_array($comparisonValue) && !in_array($value1, $comparisonValue, true)) {
if (($operator === 'nin' || $operator === '!in') && is_array($comparisonValue) && !in_array($value1, $comparisonValue, true)) {
$return[] = $value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/JSONPathSliceAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class JSONPathSliceAccessTest extends TestCase
{
/**
* @return array[]
* @return array
*/
public function sliceDataProvider(): array
{
Expand Down
54 changes: 53 additions & 1 deletion tests/JSONPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,45 @@ public function testQueryMatchLessThan(): void
self::assertEquals(['Sayings of the Century', 'Moby Dick'], $result->getData());
}

/**
* $.store.books[?(@.price > 10)].title
* Filter books that have a price more than 10
*
* @throws Exception
*/
public function testQueryMatchMoreThan(): void
{
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price > 10)].title');

self::assertEquals(['Sword of Honour', 'The Lord of the Rings'], $result->getData());
}

/**
* $.store.books[?(@.price <= 12.99)].title
* Filter books that have a price less or equal to 12.99
*
* @throws Exception
*/
public function testQueryMatchLessOrEqual(): void
{
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price <= 12.99)].title');

self::assertEquals(['Sayings of the Century', 'Sword of Honour', 'Moby Dick'], $result->getData());
}

/**
* $.store.books[?(@.price >= 12.99)].title
* Filter books that have a price less or equal to 12.99
*
* @throws Exception
*/
public function testQueryMatchEqualOrMore(): void
{
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price >= 12.99)].title');

self::assertEquals(['Sword of Honour', 'The Lord of the Rings'], $result->getData());
}

/**
* $..books[?(@.author == "J. R. R. Tolkien")]
* Filter books that have a title equal to "..."
Expand Down Expand Up @@ -282,13 +321,26 @@ public function testQueryMatchIn(): void
*
* @throws Exception
*/
public function testQueryMatchNotIn(): void
public function testQueryMatchNin(): void
{
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$..books[?(@.author nin ["J. R. R. Tolkien", "Nigel Rees"])].title');

self::assertEquals(['Sword of Honour', 'Moby Dick'], $result->getData());
}

/**
* $..books[?(@.author nin ["J. R. R. Tolkien", "Nigel Rees"])]
* Filter books that don't have a title in ["...", "..."]
*
* @throws Exception
*/
public function testQueryMatchNotIn(): void
{
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$..books[?(@.author !in ["J. R. R. Tolkien", "Nigel Rees"])].title');

self::assertEquals(['Sword of Honour', 'Moby Dick'], $result->getData());
}

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

0 comments on commit 79da94b

Please sign in to comment.