[Yaml] parse multi-line strings

This commit is contained in:
Christian Flothmann 2016-12-29 12:24:11 +01:00
parent 64e1da0581
commit ec593b923b
2 changed files with 63 additions and 1 deletions

View File

@ -295,6 +295,42 @@ class Parser
return $value;
}
// try to parse the value as a multi-line string as a last resort
if (0 === $this->currentLineNb) {
$parseError = false;
$previousLineWasNewline = false;
$value = '';
foreach ($this->lines as $line) {
try {
$parsedLine = Inline::parse($line, $flags, $this->refs);
if (!is_string($value)) {
$parseError = true;
break;
}
if ('' === trim($parsedLine)) {
$value .= "\n";
$previousLineWasNewline = true;
} elseif ($previousLineWasNewline) {
$value .= trim($parsedLine);
$previousLineWasNewline = false;
} else {
$value .= ' '.trim($parsedLine);
$previousLineWasNewline = false;
}
} catch (ParseException $e) {
$parseError = true;
break;
}
}
if (!$parseError) {
return trim($value);
}
}
switch (preg_last_error()) {
case PREG_INTERNAL_ERROR:
$error = 'Internal PCRE error.';
@ -462,7 +498,7 @@ class Parser
$previousLineIndentation = $indent;
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
$this->moveToPreviousLine();
break;
}

View File

@ -1449,6 +1449,32 @@ EOT;
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}
public function testParseMultiLineString()
{
$this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz"));
}
public function testParseMultiLineMappingValue()
{
$yaml = <<<'EOF'
foo:
- bar:
one
two
three
EOF;
$expected = array(
'foo' => array(
array(
'bar' => "one\ntwo three",
),
),
);
$this->assertEquals($expected, $this->parser->parse($yaml));
}
}
class B