[Yaml] search for colons in strings only

Since the parser is able to return `\DateTime` instances when the
`Yaml::PARSE_DATETIME` flag is passed, we need to ensure that the parsed
value actually is a string before passing the parsed value to string
functions.
This commit is contained in:
Christian Flothmann 2016-05-26 23:33:50 +02:00
parent 91a4de32c3
commit 0ea2228228
2 changed files with 14 additions and 1 deletions

View File

@ -539,7 +539,7 @@ class Parser
try {
$parsedValue = Inline::parse($value, $flags, $this->refs);
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
throw new ParseException('A colon cannot be used in an unquoted mapping value.');
}

View File

@ -1230,6 +1230,19 @@ EOT
),
);
}
public function testParseDateAsMappingValue()
{
$yaml = <<<EOT
date: 2002-12-14
EOT;
$expectedDate = new \DateTime();
$expectedDate->setTimeZone(new \DateTimeZone('UTC'));
$expectedDate->setDate(2002, 12, 14);
$expectedDate->setTime(0, 0, 0);
$this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
}
}
class B