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

Skip to content
Merged
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
[Yaml] Add compact nested mapping support to Dumper
  • Loading branch information
gr8b authored and fabpot committed Jan 2, 2025
commit f67e6366100482bd068b26fd34f8ea1219c2f0c0
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add compact nested mapping support by using the `Yaml::DUMP_COMPACT_NESTED_MAPPING` flag

7.2
---

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix, $nestingLevel);
} else {
$dumpAsMap = Inline::isHash($input);
$compactNestedMapping = Yaml::DUMP_COMPACT_NESTED_MAPPING & $flags && !$dumpAsMap;

foreach ($input as $key => $value) {
if ('' !== $output && "\n" !== $output[-1]) {
Expand Down Expand Up @@ -134,8 +135,8 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla
$output .= \sprintf('%s%s%s%s',
$prefix,
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1)
$willBeInlined || ($compactNestedMapping && \is_array($value)) ? ' ' : "\n",
$compactNestedMapping && \is_array($value) ? substr($this->doDump($value, $inline - 1, $indent + 2, $flags, $nestingLevel + 1), $indent + 2) : $this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1)
).($willBeInlined ? "\n" : '');
}
}
Expand Down
91 changes: 91 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,97 @@ public static function getDateTimeData()
];
}

public static function getDumpCompactNestedMapping()
{
$data = [
'planets' => [
[
'name' => 'Mercury',
'distance' => 57910000,
'properties' => [
['name' => 'size', 'value' => 4879],
['name' => 'moons', 'value' => 0],
[[[]]],
],
],
[
'name' => 'Jupiter',
'distance' => 778500000,
'properties' => [
['name' => 'size', 'value' => 139820],
['name' => 'moons', 'value' => 79],
[[]],
],
],
],
];
$expected = <<<YAML
planets:
\t- name: Mercury
\t distance: 57910000
\t properties:
\t\t - name: size
\t\t value: 4879
\t\t - name: moons
\t\t value: 0
\t\t - - - { }
\t- name: Jupiter
\t distance: 778500000
\t properties:
\t\t - name: size
\t\t value: 139820
\t\t - name: moons
\t\t value: 79
\t\t - - { }

YAML;

for ($indentation = 1; $indentation < 5; ++$indentation) {
yield \sprintf('Compact nested mapping %d', $indentation) => [
$data,
strtr($expected, ["\t" => str_repeat(' ', $indentation)]),
$indentation,
];
}

$indentation = 2;
$inline = 4;
$expected = <<<YAML
planets:
- name: Mercury
distance: 57910000
properties:
- { name: size, value: 4879 }
- { name: moons, value: 0 }
- [[{ }]]
- name: Jupiter
distance: 778500000
properties:
- { name: size, value: 139820 }
- { name: moons, value: 79 }
- [{ }]

YAML;

yield \sprintf('Compact nested mapping %d and inline %d', $indentation, $inline) => [
$data,
$expected,
$indentation,
$inline,
];
}

/**
* @dataProvider getDumpCompactNestedMapping
*/
public function testDumpCompactNestedMapping(array $data, string $expected, int $indentation, int $inline = 10)
{
$dumper = new Dumper($indentation);
$actual = $dumper->dump($data, $inline, 0, Yaml::DUMP_COMPACT_NESTED_MAPPING);
$this->assertSame($expected, $actual);
$this->assertSameData($data, $this->parser->parse($actual));
}

private function assertSameData($expected, $actual)
{
$this->assertEquals($expected, $actual);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Yaml
public const DUMP_NULL_AS_TILDE = 2048;
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
public const DUMP_NULL_AS_EMPTY = 8192;
public const DUMP_COMPACT_NESTED_MAPPING = 16384;

/**
* Parses a YAML file into a PHP value.
Expand Down