diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 75f46e9f6fba7..a2da8ad964bba 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 3.1.0 ----- + * Added support for dumping multi line strings as literal blocks. + * Added support for parsing base64 encoded binary data when they are tagged with the `!!binary` tag. diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 6516e4cb22d6a..576e622a62ae0 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -84,6 +84,16 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0) $isAHash = array_keys($input) !== range(0, count($input) - 1); foreach ($input as $key => $value) { + if ($inline > 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) { + $output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', ''); + + foreach (preg_split('/\n|\r\n/', $value) as $row) { + $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); + } + + continue; + } + $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value); $output .= sprintf('%s%s%s%s', diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 12c67b07c0b89..8df0a980ca0a8 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -332,6 +332,21 @@ public function objectAsMapProvider() return $tests; } + + public function testDumpMultiLineStringAsScalarBlock() + { + $data = array( + 'data' => array( + 'single_line' => 'foo bar baz', + 'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz", + 'nested_inlined_multi_line_string' => array( + 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz", + ), + ), + ); + + $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)); + } } class A diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml new file mode 100644 index 0000000000000..5994f323062e3 --- /dev/null +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml @@ -0,0 +1,14 @@ +data: + single_line: 'foo bar baz' + multi_line: | + foo + line with trailing spaces: + + bar + integer like line: + 123456789 + empty line: + + baz + nested_inlined_multi_line_string: + inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 2199c07848566..be21f2c3b7cc3 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -28,6 +28,7 @@ class Yaml const PARSE_DATETIME = 32; const DUMP_BASE64_BINARY_DATA = 64; const DUMP_OBJECT_AS_MAP = 128; + const DUMP_MULTI_LINE_LITERAL_BLOCK = 256; /** * Parses YAML into a PHP value. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy