Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61b863b

Browse files
wouterjfabpot
authored andcommitted
Also transform inline mappings to objects
1 parent 494b4a4 commit 61b863b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
240240
if ($isRef) {
241241
$this->refs[$isRef] = $data[$key];
242242
}
243+
244+
if ($objectForMap && !is_object($data)) {
245+
$data = (object) $data;
246+
}
243247
} else {
244248
// multiple documents are not supported
245249
if ('---' === $this->currentLine) {

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,28 @@ public function testObjectSupportDisabledButNoExceptions()
438438
$this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
439439
}
440440

441+
public function testObjectForMapEnabledWithMapping()
442+
{
443+
$yaml = <<<EOF
444+
foo:
445+
fiz: [cat]
446+
EOF;
447+
$result = $this->parser->parse($yaml, false, false, true);
448+
449+
$this->assertInstanceOf('stdClass', $result);
450+
$this->assertInstanceOf('stdClass', $result->foo);
451+
$this->assertEquals(array('cat'), $result->foo->fiz);
452+
}
453+
454+
public function testObjectForMapEnabledWithInlineMapping()
455+
{
456+
$result = $this->parser->parse('{ "foo": "bar", "fiz": "cat" }', false, false, true);
457+
458+
$this->assertInstanceOf('stdClass', $result);
459+
$this->assertEquals('bar', $result->foo);
460+
$this->assertEquals('cat', $result->fiz);
461+
}
462+
441463
/**
442464
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
443465
*/

0 commit comments

Comments
 (0)