Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: symfony/yaml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.3.2
Choose a base ref
...
head repository: symfony/yaml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.3.3
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jul 24, 2019

  1. Copy the full SHA
    051d045 View commit details
  2. Merge branch '3.4' into 4.2

    * 3.4:
      fix inline handling when dumping tagged values
      [PropertyAccess] Fix PropertyAccessorCollectionTest
      Typo in web profiler
      relax some date parser patterns
      Avoid getting right to left style
    nicolas-grekas committed Jul 24, 2019
    Copy the full SHA
    9468fef View commit details
  3. Merge branch '4.2' into 4.3

    * 4.2:
      fix inline handling when dumping tagged values
      [Messenger] Flatten collection of stamps collected by the traceable middleware
      [PropertyAccess] Fix PropertyAccessorCollectionTest
      Typo in web profiler
      relax some date parser patterns
      Avoid getting right to left style
    nicolas-grekas committed Jul 24, 2019
    Copy the full SHA
    34d29c2 View commit details
Showing with 105 additions and 1 deletion.
  1. +16 −1 Dumper.php
  2. +89 −0 Tests/DumperTest.php
17 changes: 16 additions & 1 deletion Dumper.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@

namespace Symfony\Component\Yaml;

use Symfony\Component\Yaml\Tag\TaggedValue;

/**
* Dumper dumps PHP variables to YAML strings.
*
@@ -56,7 +58,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $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);
@@ -75,6 +77,19 @@ public function dump($input, int $inline = 0, int $indent = 0, int $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)) {
89 changes: 89 additions & 0 deletions Tests/DumperTest.php
Original file line number Diff line number Diff line change
@@ -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
@@ -368,6 +369,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);
}