Skip to content

Commit 3fe7570

Browse files
committed
Dumper + DumperTest: Fix TaggedValue in top level.
1 parent 02e26f9 commit 3fe7570

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,20 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
6060
{
6161
$prefix = $indent ? str_repeat(' ', $indent) : '';
6262

63+
$tag = null;
64+
if ($input instanceof TaggedValue) {
65+
$tag = $input->getTag();
66+
$input = $input->getValue();
67+
if ($input instanceof TaggedValue) {
68+
throw new \InvalidArgumentException('Nested tags are not supported.');
69+
}
70+
}
71+
6372
if ($this->shouldDumpAsInline($inline, $input, $flags)) {
64-
return $prefix.Inline::dump($input, $flags);
73+
return $prefix
74+
.(null !== $tag ? '!'.$tag.' ' : '')
75+
.Inline::dump($input, $flags)
76+
;
6577
}
6678

6779
$dumpAsMap = Inline::isHash($input);
@@ -122,6 +134,10 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
122134
;
123135
}
124136

137+
if (null !== $tag) {
138+
$output = '!'.$tag."\n".$output;
139+
}
140+
125141
return $output;
126142
}
127143

@@ -131,7 +147,7 @@ private function shouldDumpAsInline(int $inline, $value, int $flags): bool
131147
return true;
132148
}
133149

134-
if (\is_array($value) || $value instanceof TaggedValue) {
150+
if (\is_array($value)) {
135151
return false;
136152
}
137153

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,20 +443,20 @@ public function testDumpingTaggedValueTopLevelAssocInline()
443443
public function testDumpingTaggedValueTopLevelAssoc()
444444
{
445445
$data = new TaggedValue('user', ['name' => 'jane']);
446+
$expected = <<<'YAML'
447+
!user
448+
name: jane
446449

447-
// @todo Fix the dumper, the output should not be ''.
448-
$expected = '';
449-
$yaml = $this->dumper->dump($data, 2);
450-
$this->assertSame($expected, $yaml);
450+
YAML;
451+
$this->assertSame($expected, $this->dumper->dump($data, 2));
451452
}
452453

453454
public function testDumpingTaggedValueTopLevelMultiLine()
454455
{
455456
$data = new TaggedValue('text', "a\nb\n");
456-
457-
// @todo Fix the dumper, the output should not be ''.
458-
$expected = '';
457+
$expected = '!text "a\\nb\\n"';
459458
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
459+
$this->assertEquals($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
460460
}
461461

462462
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