Skip to content

Commit 79da94b

Browse files
committed
Release 0.6.4
- Added support for `nin` operator - Added support for greater than or equal operator (`>=`) - Added support for less or equal operator (`<=`)
1 parent 6676bff commit 79da94b

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Script expressions are not supported as the original author intended because:
107107

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

110-
[?(@._KEY_ _OPERATOR_ _VALUE_)] // <, >, !=, ==, in and nin
110+
[?(@._KEY_ _OPERATOR_ _VALUE_)] // <, >, <=, >=, !=, ==, in and nin
111111
Eg.
112112
[?(@.title == "A string")] //
113113
[?(@.title = "A string")]
@@ -138,8 +138,14 @@ The original JsonPath implementations is available at [http://code.google.com/p/
138138
Changelog
139139
---------
140140

141+
### 0.6.4
142+
- Removed unnecessary type casting, that caused problems under certain circumstances
143+
- Added support for `nin` operator
144+
- Added support for greater than or equal operator (`>=`)
145+
- Added support for less or equal operator (`<=`)
146+
141147
### 0.6.3
142-
- Added support for `IN` expressions
148+
- Added support for `in` operator
143149
- Fixed evaluation on indexed object
144150

145151
### 0.6.x

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "softcreatr/jsonpath",
33
"description": "JSONPath implementation for parsing, searching and flattening arrays",
4-
"version": "0.6.3",
4+
"version": "0.6.4",
55
"license": "MIT",
66
"authors": [
77
{

src/Filters/QueryMatchFilter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class QueryMatchFilter extends AbstractFilter
2020
{
2121
public const MATCH_QUERY_OPERATORS = '
2222
@(\.(?<key>[^ =]+)|\[["\']?(?<keySquare>.*?)["\']?\])
23-
(\s*(?<operator>==|=|<>|!==|!=|>|<|in|nin)\s*(?<comparisonValue>.+))?
23+
(\s*(?<operator>==|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))?
2424
';
2525

2626
/**
@@ -93,15 +93,23 @@ public function filter($collection): array
9393
$return[] = $value;
9494
}
9595

96+
if ($operator === '>=' && $value1 >= $comparisonValue) {
97+
$return[] = $value;
98+
}
99+
96100
if ($operator === '<' && $value1 < $comparisonValue) {
97101
$return[] = $value;
98102
}
99103

104+
if ($operator === '<=' && $value1 <= $comparisonValue) {
105+
$return[] = $value;
106+
}
107+
100108
if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue, true)) {
101109
$return[] = $value;
102110
}
103111

104-
if ($operator === 'nin' && is_array($comparisonValue) && !in_array($value1, $comparisonValue, true)) {
112+
if (($operator === 'nin' || $operator === '!in') && is_array($comparisonValue) && !in_array($value1, $comparisonValue, true)) {
105113
$return[] = $value;
106114
}
107115
}

tests/JSONPathSliceAccessTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class JSONPathSliceAccessTest extends TestCase
1717
{
1818
/**
19-
* @return array[]
19+
* @return array
2020
*/
2121
public function sliceDataProvider(): array
2222
{

tests/JSONPathTest.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,45 @@ public function testQueryMatchLessThan(): void
215215
self::assertEquals(['Sayings of the Century', 'Moby Dick'], $result->getData());
216216
}
217217

218+
/**
219+
* $.store.books[?(@.price > 10)].title
220+
* Filter books that have a price more than 10
221+
*
222+
* @throws Exception
223+
*/
224+
public function testQueryMatchMoreThan(): void
225+
{
226+
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price > 10)].title');
227+
228+
self::assertEquals(['Sword of Honour', 'The Lord of the Rings'], $result->getData());
229+
}
230+
231+
/**
232+
* $.store.books[?(@.price <= 12.99)].title
233+
* Filter books that have a price less or equal to 12.99
234+
*
235+
* @throws Exception
236+
*/
237+
public function testQueryMatchLessOrEqual(): void
238+
{
239+
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price <= 12.99)].title');
240+
241+
self::assertEquals(['Sayings of the Century', 'Sword of Honour', 'Moby Dick'], $result->getData());
242+
}
243+
244+
/**
245+
* $.store.books[?(@.price >= 12.99)].title
246+
* Filter books that have a price less or equal to 12.99
247+
*
248+
* @throws Exception
249+
*/
250+
public function testQueryMatchEqualOrMore(): void
251+
{
252+
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$.store.books[?(@.price >= 12.99)].title');
253+
254+
self::assertEquals(['Sword of Honour', 'The Lord of the Rings'], $result->getData());
255+
}
256+
218257
/**
219258
* $..books[?(@.author == "J. R. R. Tolkien")]
220259
* Filter books that have a title equal to "..."
@@ -282,13 +321,26 @@ public function testQueryMatchIn(): void
282321
*
283322
* @throws Exception
284323
*/
285-
public function testQueryMatchNotIn(): void
324+
public function testQueryMatchNin(): void
286325
{
287326
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$..books[?(@.author nin ["J. R. R. Tolkien", "Nigel Rees"])].title');
288327

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

331+
/**
332+
* $..books[?(@.author nin ["J. R. R. Tolkien", "Nigel Rees"])]
333+
* Filter books that don't have a title in ["...", "..."]
334+
*
335+
* @throws Exception
336+
*/
337+
public function testQueryMatchNotIn(): void
338+
{
339+
$result = (new JSONPath($this->exampleData(random_int(0, 1))))->find('$..books[?(@.author !in ["J. R. R. Tolkien", "Nigel Rees"])].title');
340+
341+
self::assertEquals(['Sword of Honour', 'Moby Dick'], $result->getData());
342+
}
343+
292344
/**
293345
* $.store.books[*].author
294346
*

0 commit comments

Comments
 (0)