Skip to content

Commit b8a08ef

Browse files
committed
feature #17943 [Yaml] option to dump multi line strings as scalar blocks (xabbuh)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Yaml] option to dump multi line strings as scalar blocks | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16236, #16604, #17912, #17391 | License | MIT | Doc PR | TODO Commits ------- eff6902 option to dump multi line strings as scalar blocks
2 parents fce909a + eff6902 commit b8a08ef

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.1.0
55
-----
66

7+
* Added support for dumping multi line strings as literal blocks.
8+
79
* Added support for parsing base64 encoded binary data when they are tagged
810
with the `!!binary` tag.
911

src/Symfony/Component/Yaml/Dumper.php

+10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8484
$isAHash = array_keys($input) !== range(0, count($input) - 1);
8585

8686
foreach ($input as $key => $value) {
87+
if ($inline > 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
88+
$output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', '');
89+
90+
foreach (preg_split('/\n|\r\n/', $value) as $row) {
91+
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
92+
}
93+
94+
continue;
95+
}
96+
8797
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
8898

8999
$output .= sprintf('%s%s%s%s',

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

+15
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@ public function objectAsMapProvider()
332332

333333
return $tests;
334334
}
335+
336+
public function testDumpMultiLineStringAsScalarBlock()
337+
{
338+
$data = array(
339+
'data' => array(
340+
'single_line' => 'foo bar baz',
341+
'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
342+
'nested_inlined_multi_line_string' => array(
343+
'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
344+
),
345+
),
346+
);
347+
348+
$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 3, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
349+
}
335350
}
336351

337352
class A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
data:
2+
single_line: 'foo bar baz'
3+
multi_line: |
4+
foo
5+
line with trailing spaces:
6+
7+
bar
8+
integer like line:
9+
123456789
10+
empty line:
11+
12+
baz
13+
nested_inlined_multi_line_string:
14+
inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz"

src/Symfony/Component/Yaml/Yaml.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Yaml
2828
const PARSE_DATETIME = 32;
2929
const DUMP_BASE64_BINARY_DATA = 64;
3030
const DUMP_OBJECT_AS_MAP = 128;
31+
const DUMP_MULTI_LINE_LITERAL_BLOCK = 256;
3132

3233
/**
3334
* Parses YAML into a PHP value.

0 commit comments

Comments
 (0)