Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Yaml] fix inline handling when dumping tagged values #32688

Merged
merged 1 commit into from Jul 24, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Symfony/Component/Yaml/Dumper.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Yaml;

use Symfony\Component\Yaml\Tag\TaggedValue;

/**
* Dumper dumps PHP variables to YAML strings.
*
Expand Down Expand Up @@ -91,7 +93,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
$dumpObjectAsInlineMap = empty((array) $input);
}

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

if ($value instanceof TaggedValue) {
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());

if ($inline - 1 <= 0) {
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
} else {
$output .= "\n";
$output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
}

continue;
}

$dumpObjectAsInlineMap = true;

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
Expand Down
89 changes: 89 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;

class DumperTest extends TestCase
Expand Down Expand Up @@ -434,6 +435,94 @@ public function testDumpingStdClassInstancesRespectsInlineLevel()
inner2: c
inner3: { deep1: d, deep2: e }

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpingTaggedValueSequenceRespectsInlineLevel()
{
$data = [
new TaggedValue('user', [
'username' => 'jane',
]),
new TaggedValue('user', [
'username' => 'john',
]),
];

$yaml = $this->dumper->dump($data, 2);

$expected = <<<YAML
- !user
username: jane
- !user
username: john

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpingTaggedValueSequenceWithInlinedTagValues()
{
$data = [
new TaggedValue('user', [
'username' => 'jane',
]),
new TaggedValue('user', [
'username' => 'john',
]),
];

$yaml = $this->dumper->dump($data, 1);

$expected = <<<YAML
- !user { username: jane }
- !user { username: john }

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpingTaggedValueMapRespectsInlineLevel()
{
$data = [
'user1' => new TaggedValue('user', [
'username' => 'jane',
]),
'user2' => new TaggedValue('user', [
'username' => 'john',
]),
];

$yaml = $this->dumper->dump($data, 2);

$expected = <<<YAML
user1: !user
username: jane
user2: !user
username: john

YAML;
$this->assertSame($expected, $yaml);
}

public function testDumpingTaggedValueMapWithInlinedTagValues()
{
$data = [
'user1' => new TaggedValue('user', [
'username' => 'jane',
]),
'user2' => new TaggedValue('user', [
'username' => 'john',
]),
];

$yaml = $this->dumper->dump($data, 1);

$expected = <<<YAML
user1: !user { username: jane }
user2: !user { username: john }

YAML;
$this->assertSame($expected, $yaml);
}
Expand Down