Skip to content

Commit 9927b29

Browse files
committed
Fix bug where arrays starting with short syntax are not recognised #1
1 parent e5da491 commit 9927b29

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/vendor/
1+
/vendor/
2+
.idea/

src/parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function parse_simple($value)
4040
{
4141
$tokens = new tokens($value);
4242
$parser = new parser($tokens);
43-
$result = $parser->parse_array();
43+
$result = $parser->parse_array($tokens->does_match('['));
4444

4545
return $result;
4646
}

tests/ArrayParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ public function testCanParseSimpleString()
4747
$this->assertArraySubset($expected_simple_output, $result);
4848
}
4949

50+
/**
51+
* Test starting arrays with a short syntax
52+
* Discovered by qg0 at https://github.com/battye/php-array-parser/issues/1
53+
*/
54+
public function testParseStringStartingWithShortSyntax()
55+
{
56+
// Start with a square bracket
57+
$string = "[0 => array('one' => 1, 'two' => 'two', 3 => [1])];";
58+
$result = parser::parse_simple($string);
59+
60+
// Expected output
61+
$expected_simple_output = array(
62+
0 => array(
63+
'one' => 1,
64+
'two' => 'two',
65+
3 => array(1),
66+
)
67+
);
68+
69+
$this->assertArraySubset($expected_simple_output, $result);
70+
}
71+
5072
/**
5173
* Test that variables can be picked up
5274
*/

0 commit comments

Comments
 (0)