Skip to content

Commit 2c24bbc

Browse files
bug #48331 [Yaml] fix dumping top-level tagged values (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Yaml] fix dumping top-level tagged values | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #46725 | License | MIT | Doc PR | Commits ------- cbc616a fix dumping top-level tagged values
2 parents 64d145c + cbc616a commit 2c24bbc

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
5858

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

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

138140
return $output;
139141
}
142+
143+
private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
144+
{
145+
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
146+
147+
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
148+
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
149+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
150+
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
151+
$output .= sprintf(' |%s', $blockIndentationIndicator);
152+
153+
foreach (explode("\n", $value->getValue()) as $row) {
154+
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
155+
}
156+
157+
return $output;
158+
}
159+
160+
if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
161+
return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
162+
}
163+
164+
return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
165+
}
140166
}

src/Symfony/Component/Yaml/Tests/DumperTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,11 @@ public function testDumpingTaggedValueTopLevelAssoc()
444444
{
445445
$data = new TaggedValue('user', ['name' => 'jane']);
446446

447-
// @todo Fix the dumper, the output should not be ''.
448-
$expected = '';
447+
$expected = <<<'YAML'
448+
!user
449+
name: jane
450+
451+
YAML;
449452
$yaml = $this->dumper->dump($data, 2);
450453
$this->assertSame($expected, $yaml);
451454
}
@@ -454,9 +457,7 @@ public function testDumpingTaggedValueTopLevelMultiLine()
454457
{
455458
$data = new TaggedValue('text', "a\nb\n");
456459

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

462463
public function testDumpingTaggedValueSpecialCharsInTag()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy