8000 [Yaml] respect inline level when dumping objects as maps by goetas · Pull Request #22419 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] respect inline level when dumping objects as maps #22419

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
respect inline level when dumping objects as maps
  • Loading branch information
xabbuh committed Apr 13, 2017
commit 44adf72860f4365efc1e6ebc0bb99a4204526c79
17 changes: 17 additions & 0 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
$input = $this->castObjectToArray($input);
}

if ($inline <= 0 || !is_array($input) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
} else {
Expand Down Expand Up @@ -111,4 +115,17 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)

return $output;
}

private function castObjectToArray($object)
{
$array = (array) $object;

foreach ($array as $key => $value) {
if ($value instanceof \ArrayObject || $value instanceof \stdClass) {
$array[$key] = $this->castObjectToArray($value);
}
}

return $array;
}
}
4 changes: 0 additions & 4 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ public static function dump($value, $flags = 0)
return '!php/object:'.serialize($value);
}

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
return self::dumpArray((array) $value, $flags);
}

if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
throw new DumpException('Object support when dumping a YAML file has been disabled.');
}
Expand Down
76 changes: 57 additions & 19 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
5558
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,6 @@ public function testInlineLevel()
$this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
}

public function testArrayObjectAsMapNotInLined()
{
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
$outer = new \ArrayObject(array('outer1' => 'a', 'outer1' => $inner));

$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);

$expected = <<<YAML
outer1: a
outer2:
inner1: b
inner2: c
inner3: { deep1: d, deep2: e }

YAML;
$this->assertEquals($expected, $yaml);
}

public function testObjectSupportEnabled()
{
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
Expand Down Expand Up @@ -352,6 +333,63 @@ public function objectAsMapProvider()
return $tests;
}

public function testDumpEmptyArrayObjectInstanceAsMap()
{
$this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
}

public function testDumpingArrayObjectInstancesRespectsInlineLevel()
{
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
$outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner));

$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);

$expected = <<<YAML
outer1: a
outer2:
inner1: b
inner2: c
inner3: { deep1: d, deep2: e }

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpEmptyStdClassInstanceAsMap()
{
$this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
}

public function testDumpingStdClassInstancesRespectsInlineLevel()
{
$deep = new \stdClass();
$deep->deep1 = 'd';
$deep->deep2 = 'e';

$inner = new \stdClass();
$inner->inner1 = 'b';
$inner->inner2 = 'c';
$inner->inner3 = $deep;

$outer = new \stdClass();
$outer->outer1 = 'a';
$outer->outer2 = $inner;

$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);

$expected = <<<YAML
outer1: a
outer2:
inner1: b
inner2: c
inner3: { deep1: d, deep2: e }

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpMultiLineStringAsScalarBlock()
{
$data = array(
Expand Down
0