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

8000
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 all commits
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
29 changes: 26 additions & 3 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';

if ($inline <= 0 || !is_array($input) || empty($input)) {
$isObjectMap = self::isNotEmptyMap($input, $flags);

if ($inline <= 0 || (!is_array($input) && !$isObjectMap) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
} else {
$isAHash = Inline::isHash($input);
$isAHash = $isObjectMap || Inline::isHash($input, $flags);

if ($isObjectMap) {
$input = (array) $input;
}

foreach ($input as $key => $value) {
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
Expand All @@ -98,7 +104,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
continue;
}

$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
$willBeInlined = $inline - 1 <= 0 || (!is_array($value) && !self::isNotEmptyMap($value, $flags)) || empty($value);

$output .= sprintf('%s%s%s%s',
$prefix,
Expand All @@ -111, 10000 4 +117,21 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)

return $output;
}

private static function isNotEmptyMap($input, $flags)
{
if ($flags & ~Yaml::DUMP_OBJECT_AS_MAP) {
return false;
}

if ($input instanceof \ArrayObject && 0 < count($input)) {
return true;
}

if ($input instanceof \stdClass && 0 < count((array) $input)) {
return true;
}

return false;
}
}
15 changes: 14 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function dump($value, $flags = 0)
}

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

if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
Expand Down Expand Up @@ -261,6 +261,19 @@ private static function dumpArray($value, $flags)
return sprintf('[%s]', implode(', ', $output));
}

return self::dumpMap($value, $flags);
}

/**
* Dumps a PHP array to a YAML string as map.
*
* @param array $value The PHP array to dump
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
*
* @return string The YAML string representing the PHP array as map
*/
private static function dumpMap($value, $flags)
{
// hash
$output = array();
foreach ($value as $key => $val) {
Expand Down
91 changes: 90 additions & 1 deletion src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Component\Yaml\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

class DumperTest extends TestCase
Expand Down Expand Up @@ -333,6 +333,95 @@ 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 testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
{
$deep = new \ArrayObject(array('d', 'e'));
$inner = new \ArrayObject(array('b', 'c', $deep));
$outer = new \ArrayObject(array('a', $inner));

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

$expected = <<<YAML
0: a
1:
0: b
1: c
2: { 0: d, 1: e }

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

public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
{
$deep = new \ArrayObject(array('d', 'e'));
$inner = new \ArrayObject(array('b', 'c', $deep));
$outer = new \ArrayObject(array('a', $inner));

$yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
$expected = <<<YAML
{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
YAML;
$this->assertEquals($expected, $yaml);
}

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