Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 84241d4

Browse filesBrowse files
natepagenicolas-grekas
authored andcommitted
[Yaml] Implement multiline string as scalar block for tagged values
1 parent b104760 commit 84241d4
Copy full SHA for 84241d4

File tree

Expand file treeCollapse file tree

3 files changed

+49
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+49
-1
lines changed

‎src/Symfony/Component/Yaml/Dumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+14-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+33Lines 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 = [
+2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.