From eff69028e7e8081ff7bf5b681eba7ba8d54b2da2 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 27 Feb 2016 10:37:11 +0100 Subject: [PATCH] option to dump multi line strings as scalar blocks --- src/Symfony/Component/Yaml/CHANGELOG.md | 2 ++ src/Symfony/Component/Yaml/Dumper.php | 10 ++++++++++ src/Symfony/Component/Yaml/Tests/DumperTest.php | 15 +++++++++++++++ .../Fixtures/multiple_lines_as_literal_block.yml | 14 ++++++++++++++ src/Symfony/Component/Yaml/Yaml.php | 1 + 5 files changed, 42 insertions(+) create mode 100644 src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml 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.