10000 bug #34449 [Yaml] Implement multiline string as scalar block for tagg… · symfony/symfony@035d8a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 035d8a3

Browse files
bug #34449 [Yaml] Implement multiline string as scalar block for tagged values (natepage)
This PR was squashed before being merged into the 3.4 branch. Discussion ---------- [Yaml] Implement multiline string as scalar block for tagged values | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", if any --> | License | MIT At the moment you can parse a tagged value defined as a scalar block. But you can't actually dump a multiline string as scalar block when using a tagged value. This PR implements the multiline string as scalar block for tagged values as well. Commits ------- 84241d4 [Yaml] Implement multiline string as scalar block for tagged values
2 parents 8aefe97 + 84241d4 commit 035d8a3

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
105105
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
106106
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
107107

108-
foreach (preg_split('/\n|\r\n/', $value) as $row) {
108+
foreach (explode("\n", $value) as $row) {
109109
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
110110
}
111111

@@ -115,6 +115,19 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
115115
if ($value instanceof TaggedValue) {
116116
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
117117

118+
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
119+
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
120+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
121+
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
122+
$output .= sprintf(" |%s\n", $blockIndentationIndicator);
123+
124+
foreach (explode("\n", $value->getValue()) as $row) {
125+
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
126+
}
127+
128+
continue;
129+
}
130+
118131
if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
119132
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
120133
} else {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,39 @@ public function testDumpingNotInlinedNullTaggedValue()
553553
$this->assertSame($expected, $this->dumper->dump($data, 2));
554554
}
555555

556+
public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
557+
{
558+
$data = [
559+
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
560+
];
561+
$expected = <<<YAML
562+
foo: !bar |
563+
foo
564+
line with trailing spaces:
565+
566+
bar
567+
integer like line:
568+
123456789
569+
empty line:
570+
571+
baz
572+
573+
YAML;
574+
575+
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
576+
}
577+
578+
public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
579+
{
580+
$data = [
581+
'data' => [
582+
'foo' => new TaggedValue('bar', "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
583+
],
584+
];
585+
586+
$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
587+
}
588+
556589
public function testDumpMultiLineStringAsScalarBlock()
557590
{
558591
$data = [
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
data:
2+
foo: !bar "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"

0 commit comments

Comments
 (0)
0