Skip to content

Commit a08832b

Browse files
Merge branch '4.3' into 4.4
* 4.3: [Cache] fix memory leak when using PhpFilesAdapter [Yaml] Implement multiline string as scalar block for tagged values [HttpFoundation] Use `Cache-Control: must-revalidate` only if explicit lifetime has been given [FrameworkBundle] Use UserInterface to @return in getUser method [CI] Replace php7.4snapshot with php7.4 in Travis configuration [ExpressionLanguage][Node][BinaryNode] Process division by zero forward caught exception [Validator][ConstraintValidator] Stop passing unnecessary timezone argument to \DateTime add tags before processing them [MonologBridge] Fix debug processor datetime type
2 parents c286a10 + 13934d9 commit a08832b

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Dumper.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
7070
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
7171
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
7272

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

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

83+
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")) {
84+
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
85+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
86+
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
87+
$output .= sprintf(" |%s\n", $blockIndentationIndicator);
88+
89+
foreach (explode("\n", $value->getValue()) as $row) {
90+
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
91+
}
92+
93+
continue;
94+
}
95+
8396
if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
8497
$output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
8598
} else {

Tests/DumperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,39 @@ public function testDumpingNotInlinedNullTaggedValue()
487487
$this->assertSame($expected, $this->dumper->dump($data, 2));
488488
}
489489

490+
public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
491+
{
492+
$data = [
493+
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
494+
];
495+
$expected = <<<YAML
496+
foo: !bar |
497+
foo
498+
line with trailing spaces:
499+
500+
bar
501+
integer like line:
502+
123456789
503+
empty line:
504+
505+
baz
506+
507+
YAML;
508+
509+
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
510+
}
511+
512+
public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
513+
{
514+
$data = [
515+
'data' => [
516+
'foo' => new TaggedValue('bar', "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
517+
],
518+
];
519+
520+
$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));
521+
}
522+
490523
public function testDumpMultiLineStringAsScalarBlock()
491524
{
492525
$data = [
Lines 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)