Skip to content

Commit

Permalink
Also transform inline mappings to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj authored and fabpot committed Dec 26, 2015
1 parent 494b4a4 commit 61b863b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -240,6 +240,10 @@ 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) {
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -438,6 +438,28 @@ public function testObjectSupportDisabledButNoExceptions()
$this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
}

public function testObjectForMapEnabledWithMapping()
{
$yaml = <<<EOF
foo:
fiz: [cat]
EOF;
$result = $this->parser->parse($yaml, false, false, true);

$this->assertInstanceOf('stdClass', $result);
$this->assertInstanceOf('stdClass', $result->foo);
$this->assertEquals(array('cat'), $result->foo->fiz);
}

public function testObjectForMapEnabledWithInlineMapping()
{
$result = $this->parser->parse('{ "foo": "bar", "fiz": "cat" }', false, false, true);

$this->assertInstanceOf('stdClass', $result);
$this->assertEquals('bar', $result->foo);
$this->assertEquals('cat', $result->fiz);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
Expand Down

0 comments on commit 61b863b

Please sign in to comment.