Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed list handing when dealing with object maps #17711

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -479,6 +479,50 @@ public function testObjectForMapIsAppliedAfterParsing()
$this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true));
}

public function testWillObjectForMapOptionWillIgnoreArrays()
{
$yaml = <<<YAML
array:
- key: one
- key: two
YAML;
$actual = $this->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 = <<<YAML
array: []
YAML;
$actual = $this->parser->parse($yaml, true, false, true);
$this->assertInternalType('object', $actual);

$this->assertInternalType('array', $actual->array);
}

public function testCanParseNumericMap()
{
$yaml = <<<YAML
map:
1: one
2: two
YAML;
$actual = $this->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
Expand Down