Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Better handling of negative indexes (Fixes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenfrank committed Mar 4, 2018
1 parent 5de3d83 commit f022281
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Flow/JSONPath/Filters/QueryMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class QueryMatchFilter extends AbstractFilter
{
const MATCH_QUERY_OPERATORS = '
@(\.(?<key>\w+)|\[["\'](?<keySquare>.*?)["\']\])
@(\.(?<key>\w+)|\[["\']?(?<keySquare>.*?)["\']?\])
(\s*(?<operator>==|=|<>|!==|!=|>|<)\s*(?<comparisonValue>.+))?
';

Expand Down
42 changes: 42 additions & 0 deletions tests/JSONPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,48 @@ public function testNegativeIndex()
$this->assertEquals("Herman Melville", $result[0]['author']);
}

public function testQueryAccessWithNumericalIndexes()
{
$jsonPath = new JSONPath(json_decode('{
"result": {
"list": [
{
"time": 1477526400,
"o": "11.51000"
},
{
"time": 1477612800,
"o": "11.49870"
}
]
}
}'));

$result = $jsonPath->find("$.result.list[?(@.o == \"11.51000\")]");

$this->assertEquals("11.51000", $result[0]->o);

$jsonPath = new JSONPath(json_decode('{
"result": {
"list": [
[
1477526400,
"11.51000"
],
[
1477612800,
"11.49870"
]
]
}
}'));

$result = $jsonPath->find("$.result.list[?(@[1] == \"11.51000\")]");

$this->assertEquals("11.51000", $result[0][1]);

}


public function exampleData($asArray = true)
{
Expand Down

0 comments on commit f022281

Please sign in to comment.