diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 9e4da1766431e..176ee3f0bd058 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -301,8 +301,17 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = mb_internal_encoding($mbEncoding); } - if ($objectForMap && !is_object($data)) { - $data = (object) $data; + if ($objectForMap && is_array($data)) { + foreach (array_keys($data) as $index => $key) { + if ($index !== $key) { + $object = new \stdClass(); + foreach ($data as $key => $value) { + $object->$key = $value; + } + + return $object; + } + } } return empty($data) ? null : $data; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 579cf8ce7d37e..ff231a1c329bc 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -479,6 +479,50 @@ public function testObjectForMapIsAppliedAfterParsing() $this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true)); } + public function testWillObjectForMapOptionWillIgnoreArrays() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + + $this->assertInternalType('array', $actual->array); + $this->assertInternalType('object', $actual->array[0]); + $this->assertInternalType('object', $actual->array[1]); + $this->assertSame('one', $actual->array[0]->key); + $this->assertSame('two', $actual->array[1]->key); + } + + public function testWillObjectForMapOptionWillIgnoreEmptyArrays() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + + $this->assertInternalType('array', $actual->array); + } + + public function testCanParseNumericMap() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + $this->assertInternalType('object', $actual->map); + $this->assertTrue(property_exists($actual->map, '1')); + $this->assertTrue(property_exists($actual->map, '2')); + $this->assertSame('one', $actual->map->{'1'}); + $this->assertSame('two', $actual->map->{'2'}); + } + /** * @dataProvider invalidDumpedObjectProvider * @expectedException \Symfony\Component\Yaml\Exception\ParseException