-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Provide option to always apply timezone of the DateTimeNormalizer context. #31256
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
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 |
---|---|---|
|
@@ -25,10 +25,17 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, | |
const FORMAT_KEY = 'datetime_format'; | ||
const TIMEZONE_KEY = 'datetime_timezone'; | ||
|
||
private $defaultContext = [ | ||
self::FORMAT_KEY => \DateTime::RFC3339, | ||
self::TIMEZONE_KEY => null, | ||
]; | ||
|
||
/** | ||
* In PHP, the $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). | ||
* | ||
* The denormalizer assumes that all DateTimeInterface object returned will have the timezone returned by the getTimezone() method. | ||
* Default PHP behavior will occur if the getTimezone() method returns null or context[self::PRESERVE_CONTEXT_TIMEZONE] is set to false. | ||
* This flag will be ignored in Symfony 5.0+ | ||
*/ | ||
const PRESERVE_CONTEXT_TIMEZONE = 'preserve_context_timezone'; | ||
crayner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private $defaultContext; | ||
|
||
private static $supportedTypes = [ | ||
\DateTimeInterface::class => true, | ||
|
@@ -38,6 +45,24 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, | |
|
||
public function __construct(array $defaultContext = []) | ||
{ | ||
$this->defaultContext = [ | ||
self::FORMAT_KEY => \DateTime::RFC3339, | ||
self::TIMEZONE_KEY => null, | ||
]; | ||
|
||
if (!\is_array($defaultContext)) { | ||
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.
|
||
@trigger_error('Passing the date time format directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); | ||
|
||
$defaultContext = [self::FORMAT_KEY => (string) $defaultContext]; | ||
$defaultContext[self::TIMEZONE_KEY] = $timezone; | ||
} | ||
|
||
if (!isset($defaultContext[self::TIMEZONE_KEY]) && null !== $timezone) { | ||
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. There is no 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. There was a $timezone and $defaultContext was not always an array when this was written. |
||
@trigger_error('Passing the time zone directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); | ||
|
||
$defaultContext[self::TIMEZONE_KEY] = $timezone; | ||
} | ||
|
||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | ||
} | ||
|
||
|
@@ -80,16 +105,21 @@ public function denormalize($data, $class, string $format = null, array $context | |
{ | ||
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null; | ||
$timezone = $this->getTimezone($context); | ||
$preserveContextTimezone = $this->isPreserveContextTimezone($context); | ||
|
||
if ('' === $data || null === $data) { | ||
throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.'); | ||
} | ||
|
||
if (null !== $dateTimeFormat) { | ||
$object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); | ||
$object = \DateTime::createFromFormat($dateTimeFormat, $data, $timezone); | ||
|
||
if (false !== $object) { | ||
return $object; | ||
if ($preserveContextTimezone) { | ||
$object->setTimezone($timezone); | ||
} | ||
|
||
return \DateTime::class === $class ? $object : new \DateTimeImmutable($object->format(\DATE_RFC3339)); | ||
} | ||
|
||
$dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors(); | ||
|
@@ -104,7 +134,13 @@ public function denormalize($data, $class, string $format = null, array $context | |
} | ||
|
||
try { | ||
return \DateTime::class === $class ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone); | ||
$object = new \DateTime($data, $timezone); | ||
|
||
if ($preserveContextTimezone) { | ||
$object->setTimezone($timezone); | ||
} | ||
|
||
return \DateTime::class === $class ? $object : new \DateTimeImmutable($object->format(\DATE_RFC3339)); | ||
} catch (\Exception $e) { | ||
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
@@ -152,4 +188,22 @@ private function getTimezone(array $context) | |
|
||
return $dateTimeZone instanceof \DateTimeZone ? $dateTimeZone : new \DateTimeZone($dateTimeZone); | ||
} | ||
|
||
private function isPreserveContextTimezone(array $context): bool | ||
{ | ||
// Version 5.0 of Symfony/Serializer will always preserve the context timezone, so this method always will return true, unless the timezone equals null. | ||
if (null === $this->getTimezone($context)) { | ||
return false; | ||
} | ||
|
||
if (!isset($context[self::PRESERVE_CONTEXT_TIMEZONE]) && !isset($this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE])) { | ||
crayner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@trigger_error('Not setting the boolean "PRESERVE_CONTEXT_TIMEZONE" flag is deprecated. Set the flag to "true" to apply the context timezone consistently, otherwise setting the flag to "false" will preserve default PHP behavior.', E_USER_DEPRECATED); | ||
} | ||
|
||
if (!isset($context[self::PRESERVE_CONTEXT_TIMEZONE])) { | ||
crayner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (bool) isset($this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE]) ? $this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE] : false; | ||
} | ||
|
||
return (bool) isset($context[self::PRESERVE_CONTEXT_TIMEZONE]) ? $context[self::PRESERVE_CONTEXT_TIMEZONE] : false; | ||
} | ||
} |
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.
6.0?