8000 [Yaml] fix dumping top-level tagged values by xabbuh · Pull Request #48331 · symfony/symfony · GitHub
[go: up one dir, main page]

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
fix dumping top-level tagged values
  • Loading branch information
xabbuh committed Nov 25, 2022
commit cbc616aa89a2a59c6b8b88063119fe2ce82aeaa1
26 changes: 26 additions & 0 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):

if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
} elseif ($input instanceof TaggedValue) {
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
} else {
$dumpAsMap = Inline::isHash($input);

Expand Down Expand Up @@ -137,4 +139,28 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):

return $output;
}

private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
{
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(' |%s', $blockIndentationIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
}

return $output;
}

if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
}

return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
}
}
13 changes: 7 additions & 6 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,11 @@ public function testDumpingTaggedValueTopLevelAssoc()
{
$data = new TaggedValue('user', ['name' => 'jane']);

// @todo Fix the dumper, the output should not be ''.
$expected = '';
$expected = <<<'YAML'
!user
name: jane

YAML;
$yaml = $this->dumper->dump($data, 2);
$this->assertSame($expected, $yaml);
}
Expand All @@ -454,9 +457,7 @@ public function testDumpingTaggedValueTopLevelMultiLine()
{
$data = new TaggedValue('text', "a\nb\n");

// @todo Fix the dumper, the output should not be ''.
$expected = '';
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
$this->assertSame("!text |\n a\n b\n ", $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testDumpingTaggedValueSpecialCharsInTag()
Expand Down Expand Up @@ -696,7 +697,7 @@ public function testDumpMultiLineStringAsScalarBlock()
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }

YAML
);
);
$this->assertSame($expected, $yml);
$this->assertSame($data, $this->parser->parse($yml));
}
Expand Down
0