Skip to content

Commit 1225eb7

Browse files
amcsinicolas-grekas
authored andcommitted
[Serializer] Support subclasses of DateTime and DateTimeImmutable
1 parent 78de59b commit 1225eb7

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option
1212
* Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method)
1313
* Add `XmlEncoder::CDATA_WRAPPING_PATTERN` context option
14+
* Support subclasses of `\DateTime` and `\DateTimeImmutable` for denormalization
1415

1516
7.0
1617
---

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
138138

139139
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
140140
{
141-
return isset(self::SUPPORTED_TYPES[$type]);
141+
return is_a($type, \DateTimeInterface::class, true);
142142
}
143143

144144
/**

src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ public function testSupportsDenormalization()
232232
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
233233
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
234234
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
235+
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', DateTimeImmutableChild::class));
236+
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', DateTimeChild::class));
235237
$this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
236238
}
237239

@@ -241,6 +243,10 @@ public function testDenormalize()
241243
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
242244
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
243245
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
246+
$this->assertEquals(new DateTimeImmutableChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeImmutableChild::class));
247+
$this->assertEquals(new DateTimeImmutableChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeImmutableChild::class));
248+
$this->assertEquals(new DateTimeChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeChild::class));
249+
$this->assertEquals(new DateTimeChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', DateTimeChild::class));
244250
$this->assertEquals(new \DateTimeImmutable('2023-05-06T17:35:34.000000+0000', new \DateTimeZone('UTC')), $this->normalizer->denormalize(1683394534, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']));
245251
$this->assertEquals(new \DateTimeImmutable('2023-05-06T17:35:34.123400+0000', new \DateTimeZone('UTC')), $this->normalizer->denormalize(1683394534.1234, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U.u']));
246252
}
@@ -387,3 +393,10 @@ public function testDenormalizeFormatMismatchThrowsException()
387393
$this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|']);
388394
}
389395
}
396+
397+
class DateTimeChild extends \DateTime
398+
{
399+
}
400+
class DateTimeImmutableChild extends \DateTimeImmutable
401+
{
402+
}

0 commit comments

Comments
 (0)