Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f05632

Browse files
committed
bug #19029 [YAML] Fixed parsing problem with nested DateTime lists (jkphl)
This PR was squashed before being merged into the 3.2-dev branch (closes #19029). Discussion ---------- [YAML] Fixed parsing problem with nested DateTime lists | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | ___ Without this fix, DateTime-aware parsing of a YAML source containing nested lists of dates result in an error. Consider this: ```php $data = ['date' => ['annivesary' => new \DateTime('now')]]; $yaml = Yaml::dump($data); var_dump($yaml); $parsed = Yaml::parse($yaml, Yaml::PARSE_DATETIME); print_r($parsed); ``` Everything is fine, result is: ``` string(48) "date: annivesary: 2016-06-11T11:26:30+02:00 " Array ( [date] => Array ( [annivesary] => DateTime Object ( [date] => 2016-06-11 11:26:30.000000 [timezone_type] => 1 [timezone] => +02:00 ) ) ) ``` But making the `anniversary` a list of dates ```php $data = ['date' => ['annivesary' => [new \DateTime('now')]]]; $yaml = Yaml::dump($data); var_dump($yaml); $parsed = Yaml::parse($yaml, Yaml::PARSE_DATETIME); print_r($parsed); ``` will result in: ``` string(50) "date: annivesary: [2016-06-11T12:00:05+02:00] " PHP Warning: strpos() expects parameter 1 to be string, object given in [...]\vendor\symfony\yaml\Inline.php on line 382 PHP Catchable fatal error: Object of class DateTime could not be converted to string in [...]\vendor\symfony\yaml\Inline.php on line 386 ``` (I didn't capture the error messages with the most recent master branch, so line numbers differ somewhat) Commits ------- 52384cf [YAML] Fixed parsing problem with nested DateTime lists
2 parents 61e5ddc + 52384cf commit 6f05632

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
398398
$value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references);
399399

400400
// the value can be an array if a reference has been resolved to an array var
401-
if (!is_array($value) && !$isQuoted && false !== strpos($value, ': ')) {
401+
if (!is_array($value) && !$value instanceof \DateTimeInterface && !$isQuoted && false !== strpos($value, ': ')) {
402402
// embedded mapping?
403403
try {
404404
$pos = 0;

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ public function getTimestampTests()
535535
);
536536
}
537537

538+
/**
539+
* @dataProvider getTimestampTests
540+
*/
541+
public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second)
542+
{
543+
$expected = new \DateTime($yaml);
544+
$expected->setTimeZone(new \DateTimeZone('UTC'));
545+
$expected->setDate($year, $month, $day);
546+
$expected->setTime($hour, $minute, $second);
547+
548+
$expectedNested = array('nested' => array($expected));
549+
$yamlNested = "{nested: [$yaml]}";
550+
551+
$this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
552+
}
553+
538554
/**
539555
* @dataProvider getDateTimeDumpTests
540556
*/

0 commit comments

Comments
 (0)