mirror of
https://github.com/danog/postgres.git
synced 2024-11-26 20:15:02 +01:00
parent
15f840335d
commit
d88ec8cd35
@ -42,15 +42,23 @@ final class ArrayParser
|
||||
*/
|
||||
private function parser(string $data, callable $cast = null, string $delimiter = ','): \Generator
|
||||
{
|
||||
if ($data[0] !== '{' || \substr($data, -1) !== '}') {
|
||||
throw new ParseException("Missing opening or closing brackets");
|
||||
if ($data === '') {
|
||||
throw new ParseException("Unexpected end of data");
|
||||
}
|
||||
|
||||
if ($data[0] !== '{') {
|
||||
throw new ParseException("Missing opening bracket");
|
||||
}
|
||||
|
||||
$data = \ltrim(\substr($data, 1));
|
||||
|
||||
do {
|
||||
if ($data === '') {
|
||||
throw new ParseException("Missing closing bracket");
|
||||
throw new ParseException("Unexpected end of data");
|
||||
}
|
||||
|
||||
if ($data[0] === '}') { // Empty array
|
||||
return \ltrim(\substr($data, 1));
|
||||
}
|
||||
|
||||
if ($data[0] === '{') { // Array
|
||||
|
@ -128,15 +128,79 @@ class ArrayParserTest extends TestCase
|
||||
$this->assertSame($array, $this->parser->parse($string));
|
||||
}
|
||||
|
||||
public function testEmptyArray()
|
||||
{
|
||||
$array = [];
|
||||
$string = '{}';
|
||||
|
||||
$this->assertSame($array, $this->parser->parse($string));
|
||||
}
|
||||
|
||||
public function testArrayContainingEmptyArray()
|
||||
{
|
||||
$array = [[], [1], []];
|
||||
$string = '{{},{1},{}}';
|
||||
|
||||
$cast = function (string $value): int {
|
||||
return (int) $value;
|
||||
};
|
||||
|
||||
$this->assertSame($array, $this->parser->parse($string, $cast));
|
||||
}
|
||||
|
||||
public function testArrayWithEmptyString()
|
||||
{
|
||||
$array = [''];
|
||||
$string = '{""}';
|
||||
|
||||
$this->assertSame($array, $this->parser->parse($string));
|
||||
}
|
||||
|
||||
public function testMalformedNestedArray()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
$this->expectExceptionMessage('Unexpected end of data');
|
||||
|
||||
$string = '{{}';
|
||||
$this->parser->parse($string);
|
||||
}
|
||||
|
||||
public function testEmptyString()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
$this->expectExceptionMessage('Unexpected end of data');
|
||||
|
||||
$string = ' ';
|
||||
$this->parser->parse($string);
|
||||
}
|
||||
|
||||
public function testNoOpeningBracket()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
$this->expectExceptionMessage('Missing opening bracket');
|
||||
|
||||
$string = '"one", "two"}';
|
||||
$this->parser->parse($string);
|
||||
}
|
||||
|
||||
public function testNoClosingBracket()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
$this->expectExceptionMessage('Missing opening or closing brackets');
|
||||
$this->expectExceptionMessage('Unexpected end of data');
|
||||
|
||||
$string = '{"one", "two"';
|
||||
$this->parser->parse($string);
|
||||
}
|
||||
|
||||
public function testExtraClosingBracket()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
$this->expectExceptionMessage('Data left in buffer after parsing');
|
||||
|
||||
$string = '{"one", "two"}}';
|
||||
$this->parser->parse($string);
|
||||
}
|
||||
|
||||
public function testTrailingData()
|
||||
{
|
||||
$this->expectException(ParseException::class);
|
||||
|
Loading…
Reference in New Issue
Block a user