From 8251d6711d5efda2806a61c942686700eab7074f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 31 Dec 2015 10:55:37 +0100 Subject: [PATCH] cast arrays to objects after parsing has finished Casting arrays to objects must happen after the complete YAML string has been parsed to avoid errors when parsing subsequent lines. --- src/Symfony/Component/Yaml/Parser.php | 10 +++++----- src/Symfony/Component/Yaml/Tests/ParserTest.php | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index f486aa5fc9f19..1a04453cc132f 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -240,10 +240,6 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = if ($isRef) { $this->refs[$isRef] = $data[$key]; } - - if ($objectForMap && !is_object($data)) { - $data = (object) $data; - } } else { // multiple documents are not supported if ('---' === $this->currentLine) { @@ -307,6 +303,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = mb_internal_encoding($mbEncoding); } + if ($objectForMap && !is_object($data)) { + $data = (object) $data; + } + return empty($data) ? null : $data; } @@ -574,7 +574,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0) $previousLineIndented = false; $previousLineBlank = false; - for ($i = 0; $i < count($blockLines); $i++) { + for ($i = 0; $i < count($blockLines); ++$i) { if ('' === $blockLines[$i]) { $text .= "\n"; $previousLineIndented = false; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index e61fed171a2b6..a1b68ae4e6e9a 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -460,6 +460,15 @@ public function testObjectForMapEnabledWithInlineMapping() $this->assertEquals('cat', $result->fiz); } + public function testObjectForMapIsAppliedAfterParsing() + { + $expected = new \stdClass(); + $expected->foo = 'bar'; + $expected->baz = 'foobar'; + + $this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true)); + } + /** * @expectedException \Symfony\Component\Yaml\Exception\ParseException */