-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Support parsing YAML timestamps as DateTime #14420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
238589d
3daae64
9db5c6f
663a9ca
41c5f12
dd4798e
1f4269e
028f2d7
5d75dd0
72abdee
1b970f3
422d838
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
UPGRADE FROM 2.7 to 2.8 | ||
======================= | ||
|
||
Yaml | ||
----- | ||
|
||
* The ability to pass $timestampAsDateTime = false to the Yaml::parse method is | ||
deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. | ||
* The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated | ||
since version 2.8. The argument will be removed in 3.0. Pass true instead. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ class Inline | |
private static $exceptionOnInvalidType = false; | ||
private static $objectSupport = false; | ||
private static $objectForMap = false; | ||
private static $timestampAsDateTime = false; | ||
|
||
/** | ||
* Converts a YAML string to a PHP array. | ||
|
@@ -35,16 +36,18 @@ class Inline | |
* @param bool $objectSupport true if object support is enabled, false otherwise | ||
* @param bool $objectForMap true if maps should return a stdClass instead of array() | ||
* @param array $references Mapping of variable names to values | ||
* @param bool $timestampAsDateTime true if timestamps must be parsed as DateTime objects rather than Unix timestamps (integers) | ||
* | ||
* @return array A PHP array representing the YAML string | ||
* | ||
* @throws ParseException | ||
*/ | ||
public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $references = array()) | ||
public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $references = array(), $timestampAsDateTime = false) | ||
{ | ||
self::$exceptionOnInvalidType = $exceptionOnInvalidType; | ||
self::$objectSupport = $objectSupport; | ||
self::$objectForMap = $objectForMap; | ||
self::$timestampAsDateTime = $timestampAsDateTime; | ||
|
||
$value = trim($value); | ||
|
||
|
@@ -89,13 +92,16 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup | |
* @param mixed $value The PHP variable to convert | ||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise | ||
* @param bool $objectSupport true if object support is enabled, false otherwise | ||
* @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need this |
||
* | ||
* @return string The YAML string representing the PHP array | ||
* | ||
* @throws DumpException When trying to dump PHP resource | ||
*/ | ||
public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false) | ||
public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) | ||
{ | ||
self::$timestampAsDateTime = $timestampAsDateTime; | ||
|
||
switch (true) { | ||
case is_resource($value): | ||
if ($exceptionOnInvalidType) { | ||
|
@@ -104,6 +110,18 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp | |
|
||
return 'null'; | ||
case is_object($value): | ||
if (self::$timestampAsDateTime && ($value instanceof \DateTime || $value instanceof \DateTimeImmutable)) { | ||
if ($value->getTimezone()->getName() === date_default_timezone_get()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this. The code parsing the date might have a different default timezone. It is better to always include all info IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, if the TimeZone has been intentionally defined as for exemple +1 (in the YAML file parsed or in the PHP datas), it dumps it (because the server server timestamp is for example Europe/Paris and not +1). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it was explicitly defined as UTC and the server default timezone is also UTC ? You have no way to know whether the UTC timezone was explicitly chosen or no. But even worse, you have no idea how this will be parsed. So IMO, you should not drop information when dumping, assuming that the server parsing your YAML will have the same config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
if ('000000' === $value->format('His')) { | ||
return $value->format('Y-m-d'); | ||
} | ||
|
||
return $value->format('Y-m-d H:i:s'); | ||
} | ||
|
||
return $value->format(\DateTime::W3C); | ||
} | ||
|
||
if ($objectSupport) { | ||
return '!!php/object:'.serialize($value); | ||
} | ||
|
@@ -502,6 +520,10 @@ private static function evaluateScalar($scalar, $references = array()) | |
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar): | ||
return (float) str_replace(',', '', $scalar); | ||
case preg_match(self::getTimestampRegex(), $scalar): | ||
if (self::$timestampAsDateTime) { | ||
return new \DateTime($scalar); | ||
} | ||
|
||
return strtotime($scalar); | ||
} | ||
default: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need such parameter ? I suggest dumping DateTime objects as Yaml timestamps all the time (instead of rejecting them as done currently)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently DateTime are handled as other objects.
I think we need this parameter to keep consistency with parsing.
If $timestampAsDateTime, timestamps are parsed as DateTime and DateTime dumped as timestamps,
else timestamps are parsed as integer and DateTime dumped as datetime objects (if $objectSupport is enabled), so reparsing as DateTime.