Skip to content

Commit 34d29c2

Browse files
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
2 parents c60ecf5 + 9468fef commit 34d29c2

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

Dumper.php

Lines changed: 16 additions & 1 deletion
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
*
@@ -56,7 +58,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
5658
$dumpObjectAsInlineMap = empty((array) $input);
5759
}
5860

59-
if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
61+
if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
6062
$output .= $prefix.Inline::dump($input, $flags);
6163
} else {
6264
$dumpAsMap = Inline::isHash($input);
@@ -75,6 +77,19 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
7577
continue;
7678
}
7779

80+
if ($value instanceof TaggedValue) {
81+
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
82+
83+
if ($inline - 1 <= 0) {
84+
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
85+
} else {
86+
$output .= "\n";
87+
$output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
88+
}
89+
90+
continue;
91+
}
92+
7893
$dumpObjectAsInlineMap = true;
7994

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

Tests/DumperTest.php

Lines changed: 89 additions & 0 deletions
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
@@ -368,6 +369,94 @@ public function testDumpingStdClassInstancesRespectsInlineLevel()
368369
inner2: c
369370
inner3: { deep1: d, deep2: e }
370371
372+
YAML;
373+
$this->assertSame($expected, $yaml);
374+
}
375+
376+
public function testDumpingTaggedValueSequenceRespectsInlineLevel()
377+
{
378+
$data = [
379+
new TaggedValue('user', [
380+
'username' => 'jane',
381+
]),
382+
new TaggedValue('user', [
383+
'username' => 'john',
384+
]),
385+
];
386+
387+
$yaml = $this->dumper->dump($data, 2);
388+
389+
$expected = <<<YAML
390+
- !user
391+
username: jane
392+
- !user
393+
username: john
394+
395+
YAML;
396+
$this->assertSame($expected, $yaml);
397+
}
398+
399+
public function testDumpingTaggedValueSequenceWithInlinedTagValues()
400+
{
401+
$data = [
402+
new TaggedValue('user', [
403+
'username' => 'jane',
404+
]),
405+
new TaggedValue('user', [
406+
'username' => 'john',
407+
]),
408+
];
409+
410+
$yaml = $this->dumper->dump($data, 1);
411+
412+
$expected = <<<YAML
413+
- !user { username: jane }
414+
- !user { username: john }
415+
416+
YAML;
417+
$this->assertSame($expected, $yaml);
418+
}
419+
420+
public function testDumpingTaggedValueMapRespectsInlineLevel()
421+
{
422+
$data = [
423+
'user1' => new TaggedValue('user', [
424+
'username' => 'jane',
425+
]),
426+
'user2' => new TaggedValue('user', [
427+
'username' => 'john',
428+
]),
429+
];
430+
431+
$yaml = $this->dumper->dump($data, 2);
432+
433+
$expected = <<<YAML
434+
user1: !user
435+
username: jane
436+
user2: !user
437+
username: john
438+
439+
YAML;
440+
$this->assertSame($expected, $yaml);
441+
}
442+
443+
public function testDumpingTaggedValueMapWithInlinedTagValues()
444+
{
445+
$data = [
446+
'user1' => new TaggedValue('user', [
447+
'username' => 'jane',
448+
]),
449+
'user2' => new TaggedValue('user', [
450+
'username' => 'john',
451+
]),
452+
];
453+
454+
$yaml = $this->dumper->dump($data, 1);
455+
456+
$expected = <<<YAML
457+
user1: !user { username: jane }
458+
user2: !user { username: john }
459+
371460
YAML;
372461
$this->assertSame($expected, $yaml);
373462
}

0 commit comments

Comments
 (0)