8000 [Serializer] Remove datetime serializer by Marion-Valls · Pull Request #48982 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
minor #47580 [Serializer] Remove DateTime object from Serializer
  • Loading branch information
Marion-Valls committed Jan 14, 2023
commit 0b0c442e2d9f813cd4cb33404c9cba69fcda2771
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"symfony/security-acl": "~2.8|~3.0",
"twig/cssinliner-extra": "^2.12|^3",
"twig/inky-extra": "^2.12|^3",
"twig/markdown-extra": "^2.12|^3"
"twig/markdown-extra": "^2.12|^3",
"vimeo/psalm": "^5.0@dev"
Copy link

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?

},
"conflict": {
"ext-psr": "<1.1|>=2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure to drop support of normalization of \DateTime?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a no-go. We must still support normalizing DateTime objects. Otherwise, we force dropping usages of DateTime in all projects instead (and this is a BC break)

];

public function __construct(array $defaultContext = [])
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As $type is an argument, be careful to still support this way for users to decide of final normalization.

$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);
}
Expand Down
0