Skip to content

Commit

Permalink
fix dumping top-level tagged values
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Nov 25, 2022
1 parent e83fe9a commit 12402e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
26 changes: 26 additions & 0 deletions Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):

if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
} elseif ($input instanceof TaggedValue) {
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
} else {
$dumpAsMap = Inline::isHash($input);

Expand Down Expand Up @@ -137,4 +139,28 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):

return $output;
}

private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
{
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());

if (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', $blockIndentationIndicator);

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

return $output;
}

if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
}

return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
}
}
13 changes: 7 additions & 6 deletions Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,11 @@ public function testDumpingTaggedValueTopLevelAssoc()
{
$data = new TaggedValue('user', ['name' => 'jane']);

// @todo Fix the dumper, the output should not be ''.
$expected = '';
$expected = <<<'YAML'
!user
name: jane
YAML;
$yaml = $this->dumper->dump($data, 2);
$this->assertSame($expected, $yaml);
}
Expand All @@ -454,9 +457,7 @@ public function testDumpingTaggedValueTopLevelMultiLine()
{
$data = new TaggedValue('text', "a\nb\n");

// @todo Fix the dumper, the output should not be ''.
$expected = '';
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
$this->assertSame("!text |\n a\n b\n ", $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testDumpingTaggedValueSpecialCharsInTag()
Expand Down Expand Up @@ -696,7 +697,7 @@ public function testDumpMultiLineStringAsScalarBlock()
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
YAML
);
);
$this->assertSame($expected, $yml);
$this->assertSame($data, $this->parser->parse($yml));
}
Expand Down

0 comments on commit 12402e1

Please sign in to comment.