Skip to content

Commit 07590ae

Browse files
committed
fix inline handling when dumping tagged values
1 parent 789c330 commit 07590ae

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/Symfony/Component/Yaml/Dumper.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Yaml;
1313

14+
use Symfony\Component\Yaml\Tag\TaggedValue;
15+
1416
/**
1517
* Dumper dumps PHP variables to YAML strings.
1618
*
@@ -91,7 +93,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
9193
$dumpObjectAsInlineMap = empty((array) $input);
9294
}
9395

94-
if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
96+
if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
9597
$output .= $prefix.Inline::dump($input, $flags);
9698
} else {
9799
$dumpAsMap = Inline::isHash($input);
@@ -110,6 +112,19 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
110112
continue;
111113
}
112114

115+
if ($value instanceof TaggedValue) {
116+
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
117+
118+
if ($inline - 1 <= 0) {
119+
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
120+
} else {
121+
$output .= "\n";
122+
$output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
123+
}
124+
125+
continue;
126+
}
127+
113128
$dumpObjectAsInlineMap = true;
114129

115130
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {

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

+89
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Yaml\Dumper;
1616
use Symfony\Component\Yaml\Parser;
17+
use Symfony\Component\Yaml\Tag\TaggedValue;
1718
use Symfony\Component\Yaml\Yaml;
1819

1920
class DumperTest extends TestCase
@@ -434,6 +435,94 @@ public function testDumpingStdClassInstancesRespectsInlineLevel()
434435
inner2: c
435436
inner3: { deep1: d, deep2: e }
436437
438+
YAML;
439+
$this->assertSame($expected, $yaml);
440+
}
441+
442+
public function testDumpingTaggedValueSequenceRespectsInlineLevel()
443+
{
444+
$data = [
445+
new TaggedValue('user', [
446+
'username' => 'jane',
447+
]),
448+
new TaggedValue('user', [
449+
'username' => 'john',
450+
]),
451+
];
452+
453+
$yaml = $this->dumper->dump($data, 2);
454+
455+
$expected = <<<YAML
456+
- !user
457+
username: jane
458+
- !user
459+
username: john
460+
461+
YAML;
462+
$this->assertSame($expected, $yaml);
463+
}
464+
465+
public function testDumpingTaggedValueSequenceWithInlinedTagValues()
466+
{
467+
$data = [
468+
new TaggedValue('user', [
469+
'username' => 'jane',
470+
]),
471+
new TaggedValue('user', [
472+
'username' => 'john',
473+
]),
474+
];
475+
476+
$yaml = $this->dumper->dump($data, 1);
477+
478+
$expected = <<<YAML
479+
- !user { username: jane }
480+
- !user { username: john }
481+
482+
YAML;
483+
$this->assertSame($expected, $yaml);
484+
}
485+
486+
public function testDumpingTaggedValueMapRespectsInlineLevel()
487+
{
488+
$data = [
489+
'user1' => new TaggedValue('user', [
490+
'username' => 'jane',
491+
]),
492+
'user2' => new TaggedValue('user', [
493+
'username' => 'john',
494+
]),
495+
];
496+
497+
$yaml = $this->dumper->dump($data, 2);
498+
499+
$expected = <<<YAML
500+
user1: !user
501+
username: jane
502+
user2: !user
503+
username: john
504+
505+
YAML;
506+
$this->assertSame($expected, $yaml);
507+
}
508+
509+
public function testDumpingTaggedValueMapWithInlinedTagValues()
510+
{
511+
$data = [
512+
'user1' => new TaggedValue('user', [
513+
'username' => 'jane',
514+
]),
515+
'user2' => new TaggedValue('user', [
516+
'username' => 'john',
517+
]),
518+
];
519+
520+
$yaml = $this->dumper->dump($data, 1);
521+
522+
$expected = <<<YAML
523+
user1: !user { username: jane }
524+
user2: !user { username: john }
525+
437526
YAML;
438527
$this->assertSame($expected, $yaml);
439528
}

0 commit comments

Comments
 (0)