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

Commit c38cd7f

Browse files
authored
Merge pull request #36 from oleg-andreyev/dashed-index
#16 added `Negative Lookahead` to MATCH_INDEX
2 parents 3cd3948 + 5066467 commit c38cd7f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Flow/JSONPath/JSONPathLexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class JSONPathLexer
99
* Match within bracket groups
1010
* Matches are whitespace insensitive
1111
*/
12-
const MATCH_INDEX = '\w+ | \*'; // Eg. foo
12+
const MATCH_INDEX = '(?!\-)[\-\w]+ | \*'; // Eg. foo, 40f35757-2563-4790-b0b1-caa904be455f
1313
const MATCH_INDEXES = '\s* -?\d+ [-?\d,\s]+'; // Eg. 0,1,2
1414
const MATCH_SLICE = '[-\d:]+ | :'; // Eg. [0:2:1]
1515
const MATCH_QUERY_RESULT = '\s* \( .+? \) \s*'; // Eg. ?(@.length - 1)

tests/JSONPathDashedIndexTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Flow\JSONPath\Test;
4+
5+
6+
use Flow\JSONPath\JSONPath;
7+
8+
class JSONPathDashedIndexTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function indexDataProvider()
11+
{
12+
return [
13+
// path, data, expected
14+
[
15+
'$.data[test-test-test]',
16+
[
17+
'data' => [
18+
'test-test-test' => 'foo'
19+
]
20+
],
21+
[
22+
'foo'
23+
]
24+
],
25+
[
26+
'$.data[40f35757-2563-4790-b0b1-caa904be455f]',
27+
[
28+
'data' => [
29+
'40f35757-2563-4790-b0b1-caa904be455f' => 'bar'
30+
]
31+
],
32+
[
33+
'bar'
34+
]
35+
]
36+
];
37+
}
38+
39+
/** @dataProvider indexDataProvider */
40+
public function testSlice($path, $data, $expected)
41+
{
42+
$jsonPath = new JSONPath($data);
43+
$result = $jsonPath->find($path)->data();
44+
$this->assertEquals($expected, $result);
45+
}
46+
}

0 commit comments

Comments
 (0)