Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: symfony/yaml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.2.7
Choose a base ref
...
head repository: symfony/yaml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.2.10
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Feb 21, 2023

  1. Fix phpdocs in components

    nicolas-grekas authored and alamirault committed Feb 21, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3713e20 View commit details

Commits on Apr 23, 2023

  1. Copy the full SHA
    4cd2e3e View commit details

Commits on Apr 28, 2023

  1. Merge branch '5.4' into 6.2

    * 5.4:
      trim(): Argument #1 () must be of type string, bool given
      [Dumper] Trim leading newlines when checking if value begins with a space
      Fix the list of supported shells for completions in a phar
    nicolas-grekas committed Apr 28, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    61916f3 View commit details
Showing with 55 additions and 9 deletions.
  1. +19 −9 Dumper.php
  2. +36 −0 Tests/DumperTest.php
28 changes: 19 additions & 9 deletions Dumper.php
Original file line number Diff line number Diff line change
@@ -67,9 +67,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
}

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
// 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 = str_starts_with($value, ' ') ? (string) $this->indentation : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value);

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

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($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 = str_starts_with($value->getValue(), ' ') ? (string) $this->indentation : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
$output .= sprintf(' |%s', $blockIndentationIndicator);

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

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($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 : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
$output .= sprintf(' |%s', $blockIndentationIndicator);

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

return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
}

private function getBlockIndentationIndicator(string $value): string
{
$lines = explode("\n", $value);

// If the first line (that is neither empty nor contains only spaces)
// starts with a space character, the spec requires a block indentation indicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
foreach ($lines as $line) {
if ('' !== trim($line, ' ')) {
return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
}
}

return '';
}
}
36 changes: 36 additions & 0 deletions Tests/DumperTest.php
Original file line number Diff line number Diff line change
@@ -710,6 +710,42 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
$this->assertSame($data, $this->parser->parse($yml));
}

public function testDumpMultiLineStringAsScalarBlockWhenFirstLineIsEmptyAndSecondLineHasLeadingSpace()
{
$data = [
'data' => [
'multi_line' => "\n the second line has leading spaces\nThe third line does not.",
],
];

$expected = "data:\n multi_line: |4-\n\n the second line has leading spaces\n The third line does not.";

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

public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasOnlySpaces()
{
$data = [
'data' => [
'multi_line' => " \nthe second line\nThe third line.",
],
];

$expectedData = [
'data' => [
'multi_line' => "\nthe second line\nThe third line.",
],
];

$expectedYml = "data:\n multi_line: |-\n \n the second line\n The third line.";

$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->assertSame($expectedYml, $yml);
$this->assertSame($expectedData, $this->parser->parse($yml));
}

public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
{
$data = ["a\r\nb\nc"];