-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Remove datetime serializer #48982
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,14 +27,13 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, | |
public const TIMEZONE_KEY = 'datetime_timezone'; | ||
|
||
private $defaultContext = [ | ||
self::FORMAT_KEY => \DateTime::RFC3339, | ||
self::FORMAT_KEY => \DateTimeInterface::RFC3339, | ||
self::TIMEZONE_KEY => null, | ||
]; | ||
|
||
private const SUPPORTED_TYPES = [ | ||
\DateTimeInterface::class => true, | ||
\DateTimeImmutable::class => true, | ||
\DateTime::class => true, | ||
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. Are you sure to drop support of normalization of 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. This part is a no-go. We must still support normalizing |
||
]; | ||
|
||
public function __construct(array $defaultContext = []) | ||
|
@@ -88,29 +87,29 @@ public function denormalize(mixed $data, string $type, string $format = null, ar | |
} | ||
|
||
if (null !== $dateTimeFormat) { | ||
$object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $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. As |
||
$object = \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); | ||
|
||
if (false !== $object) { | ||
return $object; | ||
} | ||
|
||
$dateTimeErrors = \DateTime::class === $type ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors(); | ||
$dateTimeErrors = \DateTimeImmutable::getLastErrors(); | ||
|
||
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Parsing datetime string "%s" using format "%s" resulted in %d errors: ', $data, $dateTimeFormat, $dateTimeErrors['error_count'])."\n".implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors'])), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true); | ||
} | ||
|
||
$defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null; | ||
|
||
if (null !== $defaultDateTimeFormat) { | ||
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone); | ||
$object = \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone); | ||
|
||
if (false !== $object) { | ||
return $object; | ||
} | ||
} | ||
|
||
try { | ||
return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone); | ||
return new \DateTimeImmutable($data, $timezone); | ||
} catch (\Exception $e) { | ||
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, false, $e->getCode(), $e); | ||
} | ||
|
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.
you shouldn't change composer.json, not the scope of this PR. BTW, what was your intention by adding psalm?