[Yaml] added support for the end of document marker

This commit is contained in:
Fabien Potencier 2010-06-29 17:51:05 +02:00
parent 1cd5939e9a
commit 04e621a5cd
2 changed files with 24 additions and 2 deletions

View File

@ -503,14 +503,25 @@ class Parser
$value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#su', '', $value, -1, $count);
$this->offset += $count;
// remove leading comments and/or ---
$trimmedValue = preg_replace('#^((\#.*?\n)|(\-\-\-.*?\n))*#su', '', $value, -1, $count);
// remove leading comments
$trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
if ($count == 1) {
// items have been removed, update the offset
$this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
$value = $trimmedValue;
}
// remove start of the document marker (---)
$trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
if ($count == 1) {
// items have been removed, update the offset
$this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
$value = $trimmedValue;
// remove end of the document marker (...)
$value = preg_replace('#\.\.\.\s*$#s', '', $value);
}
return $value;
}
}

View File

@ -89,6 +89,17 @@ class ParserTest extends \PHPUnit_Framework_TestCase
}
}
public function testEndOfTheDocumentMarker()
{
$yaml = <<<EOF
--- %YAML:1.0
foo
...
EOF;
$this->assertEquals('foo', $this->parser->parse($yaml));
}
public function testObjectsSupport()
{
$b = array('foo' => new B(), 'bar' => 1);