Skip to content

Commit 78fee13

Browse files
committed
[Serializer] Add PRESERVE_CONTEXT_TIMEZONE_KEY to DateTimeNormalizer
Add `withPreserveContextTimezone` to `DateTimeNormalizerContextBuilder`
1 parent 5f2bf53 commit 78fee13

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Symfony/Component/Serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,13 @@ public function withCast(?string $cast): static
6969
{
7070
return $this->with(DateTimeNormalizer::CAST_KEY, $cast);
7171
}
72+
73+
/**
74+
* Configures if the timezone should be used from the context and ignore any timezone
75+
* defined in date strings.
76+
*/
77+
public function withPreserveContextTimezone(?bool $preserveContextTimezone): static
78+
{
79+
return $this->with(DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY, $preserveContextTimezone);
80+
}
7281
}

src/Symfony/Component/Serializer/Tests/Context/Normalizer/DateTimeNormalizerContextBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testWithers(array $values)
3939
->withFormat($values[DateTimeNormalizer::FORMAT_KEY])
4040
->withTimezone($values[DateTimeNormalizer::TIMEZONE_KEY])
4141
->withCast($values[DateTimeNormalizer::CAST_KEY])
42+
->withPreserveContextTimezone($values[DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY])
4243
->toArray();
4344

4445
$this->assertEquals($values, $context);
@@ -53,12 +54,14 @@ public static function withersDataProvider(): iterable
5354
DateTimeNormalizer::FORMAT_KEY => 'format',
5455
DateTimeNormalizer::TIMEZONE_KEY => new \DateTimeZone('GMT'),
5556
DateTimeNormalizer::CAST_KEY => 'int',
57+
DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY => true,
5658
]];
5759

5860
yield 'With null values' => [[
5961
DateTimeNormalizer::FORMAT_KEY => null,
6062
DateTimeNormalizer::TIMEZONE_KEY => null,
6163
DateTimeNormalizer::CAST_KEY => null,
64+
DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE_KEY => null,
6265
]];
6366
}
6467

0 commit comments

Comments
 (0)