Skip to content

Commit

Permalink
bug #34449 [Yaml] Implement multiline string as scalar block for tagg…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
nicolas-grekas committed Dec 10, 2019
2 parents 8aefe97 + 84241d4 commit 035d8a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Symfony/Component/Yaml/Dumper.php
Expand Up @@ -105,7 +105,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);

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

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

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")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(" |%s\n", $blockIndentationIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
}

continue;
}

if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
} else {
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Expand Up @@ -553,6 +553,39 @@ public function testDumpingNotInlinedNullTaggedValue()
$this->assertSame($expected, $this->dumper->dump($data, 2));
}

public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
{
$data = [
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
];
$expected = <<<YAML
foo: !bar |
foo
line with trailing spaces:
bar
integer like line:
123456789
empty line:
baz
YAML;

$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
{
$data = [
'data' => [
'foo' => new TaggedValue('bar', "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
],
];

$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));
}

public function testDumpMultiLineStringAsScalarBlock()
{
$data = [
Expand Down
@@ -0,0 +1,2 @@
data:
foo: !bar "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"

0 comments on commit 035d8a3

Please sign in to comment.