From 7e92213b6e254d44a839c8a5f1d1da29c3c39e2a Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 5 Apr 2025 13:54:55 +0200 Subject: [PATCH] [Serializer] Add PRESERVE_CONTEXT_TIMEZONE_KEY to DateTimeNormalizer Adds DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY to the `DateTimeNormalizer` section --- serializer.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/serializer.rst b/serializer.rst index 3c01e067088..55d99545f6a 100644 --- a/serializer.rst +++ b/serializer.rst @@ -1344,6 +1344,12 @@ normalizers (in order of priority): `RFC 3339`_ format. Use ``DateTimeNormalizer::FORMAT_KEY`` and ``DateTimeNormalizer::TIMEZONE_KEY`` to change the format. + To always create :phpclass:`DateTime` and :phpclass:`DateTimeImmutable` + with the timezone specified in the context, set the + ``DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY`` context option to + ``true``. This will preserve the timezone of the context and ignores any + timezone from the input. + To convert the objects to integers or floats, set the serializer context option ``DateTimeNormalizer::CAST_KEY`` to ``int`` or ``float``. @@ -1352,6 +1358,10 @@ normalizers (in order of priority): The ``DateTimeNormalizer::CAST_KEY`` context option was introduced in Symfony 7.1. + .. versionadded:: 7.3 + + The ``DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY`` context option was introduced in Symfony 7.3. + :class:`Symfony\\Component\\Serializer\\Normalizer\\ConstraintViolationListNormalizer` This normalizer converts objects that implement :class:`Symfony\\Component\\Validator\\ConstraintViolationListInterface` @@ -1436,7 +1446,7 @@ normalizers (in order of priority): $propertyInfo = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); $normalizers = [new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()), null, null, $propertyInfo), new ArrayDenormalizer()]; - + $this->serializer = new Serializer($normalizers, [new JsonEncoder()]); :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`