Skip to content

Commit a9a8337

Browse files
Merge branch '6.2' into 6.3
* 6.2: Fix test class name trim(): Argument #1 () must be of type string, bool given Check if trace.curlCommand is defined in profiler [Dumper] Trim leading newlines when checking if value begins with a space [FrameworkBundle] Make service edges unique Fix the list of supported shells for completions in a phar Fix the usage of the zsh completion through the fpath discovery
2 parents 9c50aaf + 61916f3 commit a9a8337

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

Dumper.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
7171
}
7272

7373
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
74-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
75-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
76-
$blockIndentationIndicator = str_starts_with($value, ' ') ? (string) $this->indentation : '';
74+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
7775

7876
if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
7977
$blockChompingIndicator = '+';
@@ -100,9 +98,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
10098
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
10199

102100
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
103-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
104-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
105-
$blockIndentationIndicator = str_starts_with($value->getValue(), ' ') ? (string) $this->indentation : '';
101+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
106102
$output .= sprintf(' |%s', $blockIndentationIndicator);
107103

108104
foreach (explode("\n", $value->getValue()) as $row) {
@@ -147,9 +143,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
147143
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
148144

149145
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
150-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
151-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
152-
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
146+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
153147
$output .= sprintf(' |%s', $blockIndentationIndicator);
154148

155149
foreach (explode("\n", $value->getValue()) as $row) {
@@ -165,4 +159,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
165159

166160
return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
167161
}
162+
163+
private function getBlockIndentationIndicator(string $value): string
164+
{
165+
$lines = explode("\n", $value);
166+
167+
// If the first line (that is neither empty nor contains only spaces)
168+
// starts with a space character, the spec requires a block indentation indicator
169+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
170+
foreach ($lines as $line) {
171+
if ('' !== trim($line, ' ')) {
172+
return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
173+
}
174+
}
175+
176+
return '';
177+
}
168178
}

Tests/DumperTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,42 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
710710
$this->assertSame($data, $this->parser->parse($yml));
711711
}
712712

713+
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineIsEmptyAndSecondLineHasLeadingSpace()
714+
{
715+
$data = [
716+
'data' => [
717+
'multi_line' => "\n the second line has leading spaces\nThe third line does not.",
718+
],
719+
];
720+
721+
$expected = "data:\n multi_line: |4-\n\n the second line has leading spaces\n The third line does not.";
722+
723+
$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
724+
$this->assertSame($expected, $yml);
725+
$this->assertSame($data, $this->parser->parse($yml));
726+
}
727+
728+
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasOnlySpaces()
729+
{
730+
$data = [
731+
'data' => [
732+
'multi_line' => " \nthe second line\nThe third line.",
733+
],
734+
];
735+
736+
$expectedData = [
737+
'data' => [
738+
'multi_line' => "\nthe second line\nThe third line.",
739+
],
740+
];
741+
742+
$expectedYml = "data:\n multi_line: |-\n \n the second line\n The third line.";
743+
744+
$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
745+
$this->assertSame($expectedYml, $yml);
746+
$this->assertSame($expectedData, $this->parser->parse($yml));
747+
}
748+
713749
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
714750
{
715751
$data = ["a\r\nb\nc"];

0 commit comments

Comments
 (0)