Skip to content

Commit 468d4f0

Browse files
committed
Add support for variable names starting with numbers in the dot-notation
1 parent 5ac1783 commit 468d4f0

File tree

4 files changed

+10
-18
lines changed

4 files changed

+10
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ JsonPath Language
9797
=================
9898
This library implements the following specification:
9999
```
100-
var_name¹ = /^\.([\p{L}\_\$][\w\-\$]*|\*)(.*)/u
100+
var_name¹ = /^\.([\p{L}\p{N}\_\$][\p{L}\p{N}\_\-\$]*|\*)(.*)/u
101101
number = ([0-9]+(\.[0-9]*) | ([0-9]*\.[0-9]+))
102102
string = ('\''.*?'\'' | '"'.*?'"')
103103
boolean = ('true' | 'false')
@@ -128,7 +128,9 @@ length = (jsonpath | childpath) '.length'
128128
in_array = value 'in' '[' value (',' value)* ']'
129129
```
130130

131-
¹`var_name`: the regexp roughly translates to "any valid JavaScript variable name".
131+
¹`var_name`: the regex roughly translates to "any valid JavaScript variable
132+
name", plus some quirks such as names starting with numbers or containing
133+
dashes (`-`).
132134

133135
### Limitations on the specification:
134136
* The jsonpath inside _value_ cannot contain `or`, `and` or any comparator.

src/Galbar/JsonPath/Language/Regex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Regex
2323
const ROOT_OBJECT = '/^\$(.*)/';
2424

2525
// Child regex
26-
const CHILD_NAME = '/^\.([\p{L}\_\$][\w\-\$]*|\*)(.*)/u';
26+
const CHILD_NAME = '/^\.([\p{L}\p{N}\_\$][\p{L}\p{N}\_\-\$]*|\*)(.*)/u';
2727
const RECURSIVE_SELECTOR = '/^\.\.(.+)/u';
2828

2929
// Array expressions
@@ -32,7 +32,7 @@ class Regex
3232
const LENGTH = '/^(.*)\.length$/';
3333

3434
// Object expression
35-
const CHILD_NAME_LIST = '/^(?:([\w\_\$^\d][\w\-\$]*?|".*?"|\'.*?\')(\s*,\s*([\w\_\$^\d][\w\-\$]*|".*?"|\'.*?\'))*)$/u';
35+
const CHILD_NAME_LIST = '/^(?:([\p{L}\p{N}\_\$][\p{L}\p{N}\_\-\$]*|".*?"|\'.*?\')(\s*,\s*([\p{L}\p{N}\_\$][\p{L}\p{N}\_\-\$]*|".*?"|\'.*?\'))*)$/u';
3636

3737
// Conditional expressions
3838
const EXPR_STRING = '/^(?:\'(.*)\'|"(.*)")$/';

tests/Galbar/JsonPath/JsonObjectIssue37Test.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,12 @@
2727
*/
2828
class JsonObjectIssue37Test extends \PHPUnit_Framework_TestCase
2929
{
30-
/**
31-
* This library explicitly only supports valid JavaScript variable names with the dot notation.
32-
*
33-
* This is not a bug but a feature
34-
*/
3530
public function testCase1()
3631
{
3732
$jsonObject = new JsonObject('{"a": "first", "2": "second", "b": "third"}');
38-
$exception = null;
39-
try {
40-
$jsonObject->get('$.2');
41-
} catch (InvalidJsonPathException $e) {
42-
$exception = $e;
43-
}
44-
$this->assertEquals($exception->getMessage(), "Error in JSONPath near '.2'");
33+
$result = $jsonObject->get('$.2');
34+
$expected = ["second"];
35+
$this->assertEquals($expected, $result);
4536
}
4637

4738
/**

tests/Galbar/JsonPath/JsonObjectTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,7 @@ public function testParsingErrorsProvider()
11721172
array("$.store.book[?(@.title in ['foo')]", "[?(@.title in ['foo')]"),
11731173
array("$.store.book[?(@.title in 'foo'])]", "[?(@.title in 'foo'])]"),
11741174
array("$.store.book[?(@.title in 'foo')]", " in 'foo'"),
1175-
array("$.store.book[?(@.title ['foo'])]", " ['foo']"),
1176-
array("$.2", ".2"),
1175+
array("$.store.book[?(@.title ['foo'])]", " ['foo']")
11771176
);
11781177
}
11791178

0 commit comments

Comments
 (0)