Skip to content

Commit 76c0891

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Mailer] Include all transports' debug messages in RoundRobin transport exception [FrameworkBundle] fix: fix help message Use relative timestamps [Cache] Fix dealing with ext-redis' multi/exec returning a bool [Messenger][Amqp] Added missing rpc_timeout option [Serializer] Prevent GetSetMethodNormalizer from creating invalid magic method call [HttpFoundation] Fix dumping array cookies [WebProfilerBundle] Fix dump header not being displayed TraceableHttpClient: increase decorator's priority Use static methods inside data providers [FrameworkBundle] Allow configuring `framework.exceptions` with a config builder bug #48313 [Mime] Fix MessagePart serialization [ErrorHandler][DebugClassLoader] Fix some new return types support Fix getting the name of closures on PHP 8.1.11+ [Translator] Fix typo "internal" / "interval" fix dumping top-level tagged values
2 parents eb85bd1 + edcdc11 commit 76c0891

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Dumper.php

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

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

@@ -135,4 +137,28 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
135137

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

Tests/DumperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,26 @@ public function testDumpingTaggedValueTopLevelAssocInline()
440440
$this->assertSameData($data, $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS));
441441
}
442442

443+
public function testDumpingTaggedValueTopLevelAssoc()
444+
{
445+
$data = new TaggedValue('user', ['name' => 'jane']);
446+
447+
$expected = <<<'YAML'
448+
!user
449+
name: jane
450+
451+
YAML;
452+
$yaml = $this->dumper->dump($data, 2);
453+
$this->assertSame($expected, $yaml);
454+
}
455+
456+
public function testDumpingTaggedValueTopLevelMultiLine()
457+
{
458+
$data = new TaggedValue('text', "a\nb\n");
459+
460+
$this->assertSame("!text |\n a\n b\n ", $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
461+
}
462+
443463
public function testDumpingTaggedValueSpecialCharsInTag()
444464
{
445465
// @todo Validate the tag name in the TaggedValue constructor.

0 commit comments

Comments
 (0)