From 7025b964f123bbf1896d7563db6ec7f1f63e918a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 16 Sep 2024 14:33:33 +0200 Subject: [PATCH] parse empty sequence elements as null --- Inline.php | 8 ++++++++ Tests/InlineTest.php | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/Inline.php b/Inline.php index 94d3a5cd..5e4e5f7a 100644 --- a/Inline.php +++ b/Inline.php @@ -355,11 +355,18 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0, ++$i; // [foo, bar, ...] + $lastToken = null; while ($i < $len) { if (']' === $sequence[$i]) { return $output; } if (',' === $sequence[$i] || ' ' === $sequence[$i]) { + if (',' === $sequence[$i] && (null === $lastToken || 'separator' === $lastToken)) { + $output[] = null; + } elseif (',' === $sequence[$i]) { + $lastToken = 'separator'; + } + ++$i; continue; @@ -403,6 +410,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0, $output[] = $value; + $lastToken = 'value'; ++$i; } diff --git a/Tests/InlineTest.php b/Tests/InlineTest.php index 4e8d324a..c4e1eb1b 100644 --- a/Tests/InlineTest.php +++ b/Tests/InlineTest.php @@ -1004,4 +1004,11 @@ public function testParseQuotedReferenceLikeStringsInSequence() $this->assertSame(['&foo', '&bar', '&baz'], Inline::parse($yaml)); } + + public function testParseSequenceWithEmptyElement() + { + $this->assertSame(['foo', null, 'bar'], Inline::parse('[foo, , bar]')); + $this->assertSame([null, 'foo', 'bar'], Inline::parse('[, foo, bar]')); + $this->assertSame(['foo', 'bar'], Inline::parse('[foo, bar, ]')); + } }