Skip to content

Commit d88ec8c

Browse files
committed
Fix empty array parsing
Closes #21.
1 parent 15f8403 commit d88ec8c

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/Internal/ArrayParser.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ public function parse(string $data, callable $cast = null, string $delimiter = '
4242
*/
4343
private function parser(string $data, callable $cast = null, string $delimiter = ','): \Generator
4444
{
45-
if ($data[0] !== '{' || \substr($data, -1) !== '}') {
46-
throw new ParseException("Missing opening or closing brackets");
45+
if ($data === '') {
46+
throw new ParseException("Unexpected end of data");
47+
}
48+
49+
if ($data[0] !== '{') {
50+
throw new ParseException("Missing opening bracket");
4751
}
4852

4953
$data = \ltrim(\substr($data, 1));
5054

5155
do {
5256
if ($data === '') {
53-
throw new ParseException("Missing closing bracket");
57+
throw new ParseException("Unexpected end of data");
58+
}
59+
60+
if ($data[0] === '}') { // Empty array
61+
return \ltrim(\substr($data, 1));
5462
}
5563

5664
if ($data[0] === '{') { // Array

test/ArrayParserTest.php

+65-1
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,79 @@ public function testEscapedBackslashesInQuotedValue()
128128
$this->assertSame($array, $this->parser->parse($string));
129129
}
130130

131+
public function testEmptyArray()
132+
{
133+
$array = [];
134+
$string = '{}';
135+
136+
$this->assertSame($array, $this->parser->parse($string));
137+
}
138+
139+
public function testArrayContainingEmptyArray()
140+
{
141+
$array = [[], [1], []];
142+
$string = '{{},{1},{}}';
143+
144+
$cast = function (string $value): int {
145+
return (int) $value;
146+
};
147+
148+
$this->assertSame($array, $this->parser->parse($string, $cast));
149+
}
150+
151+
public function testArrayWithEmptyString()
152+
{
153+
$array = [''];
154+
$string = '{""}';
155+
156+
$this->assertSame($array, $this->parser->parse($string));
157+
}
158+
159+
public function testMalformedNestedArray()
160+
{
161+
$this->expectException(ParseException::class);
162+
$this->expectExceptionMessage('Unexpected end of data');
163+
164+
$string = '{{}';
165+
$this->parser->parse($string);
166+
}
167+
168+
public function testEmptyString()
169+
{
170+
$this->expectException(ParseException::class);
171+
$this->expectExceptionMessage('Unexpected end of data');
172+
173+
$string = ' ';
174+
$this->parser->parse($string);
175+
}
176+
177+
public function testNoOpeningBracket()
178+
{
179+
$this->expectException(ParseException::class);
180+
$this->expectExceptionMessage('Missing opening bracket');
181+
182+
$string = '"one", "two"}';
183+
$this->parser->parse($string);
184+
}
185+
131186
public function testNoClosingBracket()
132187
{
133188
$this->expectException(ParseException::class);
134-
$this->expectExceptionMessage('Missing opening or closing brackets');
189+
$this->expectExceptionMessage('Unexpected end of data');
135190

136191
$string = '{"one", "two"';
137192
$this->parser->parse($string);
138193
}
139194

195+
public function testExtraClosingBracket()
196+
{
197+
$this->expectException(ParseException::class);
198+
$this->expectExceptionMessage('Data left in buffer after parsing');
199+
200+
$string = '{"one", "two"}}';
201+
$this->parser->parse($string);
202+
}
203+
140204
public function testTrailingData()
141205
{
142206
$this->expectException(ParseException::class);

0 commit comments

Comments
 (0)