Skip to content

Commit

Permalink
bug #32688 [Yaml] fix inline handling when dumping tagged values (xab…
Browse files Browse the repository at this point in the history
…buh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] fix inline handling when dumping tagged values

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32251
| License       | MIT
| Doc PR        |

Commits
-------

07590ae fix inline handling when dumping tagged values
  • Loading branch information
nicolas-grekas committed Jul 24, 2019
2 parents 3f652f1 + 07590ae commit 639041c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
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

0 comments on commit 639041c

Please sign in to comment.