diff --git a/src/Serializer/Normalizer/DateTimeZoneNormalizer.php b/src/Serializer/Normalizer/DateTimeZoneNormalizer.php new file mode 100644 index 000000000..a1d04b317 --- /dev/null +++ b/src/Serializer/Normalizer/DateTimeZoneNormalizer.php @@ -0,0 +1,38 @@ +getName(); + } + + public function denormalize(mixed $value): ?DateTimeZone + { + if ($value === null) { + return null; + } + + if (!is_string($value)) { + throw new InvalidArgument(); + } + + return new DateTimeZone($value); + } +} diff --git a/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php b/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php new file mode 100644 index 000000000..5d6223f71 --- /dev/null +++ b/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php @@ -0,0 +1,56 @@ +assertEquals(null, $normalizer->normalize(null)); + } + + public function testDenormalizeWithNull(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals(null, $normalizer->denormalize(null)); + } + + public function testNormalizeWithInvalidArgument(): void + { + $this->expectException(InvalidArgument::class); + + $normalizer = new DateTimeZoneNormalizer(); + $normalizer->normalize(123); + } + + public function testDenormalizeWithInvalidArgument(): void + { + $this->expectException(InvalidArgument::class); + + $normalizer = new DateTimeZoneNormalizer(); + $normalizer->denormalize(123); + } + + public function testNormalizeWithValue(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals('EDT', $normalizer->normalize(new DateTimeZone('EDT'))); + } + + public function testDenormalizeWithValue(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals(new DateTimeZone('EDT'), $normalizer->denormalize('EDT')); + } +}