Skip to content

Commit cc64a2f

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Messenger] Fix function name in TriggerSql on postgresql bridge to support table name with schema [HttpClient] Fix the CS fix Workaround disabled "var_dump" [Serializer] Respect default context in DateTimeNormalizer::denormalize
2 parents ab2ad88 + f030259 commit cc64a2f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Normalizer/DateTimeNormalizer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
105105
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);
106106
}
107107

108+
$defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null;
109+
110+
if (null !== $defaultDateTimeFormat) {
111+
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone);
112+
113+
if (false !== $object) {
114+
return $object;
115+
}
116+
}
117+
108118
try {
109119
return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
110120
} catch (\Exception $e) {

Tests/Normalizer/DateTimeNormalizerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,28 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex
271271
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
272272
}
273273

274+
public function testDenormalizeDateTimeStringWithDefaultContextFormat()
275+
{
276+
$format = 'd/m/Y';
277+
$string = '01/10/2018';
278+
279+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
280+
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);
281+
282+
$this->assertSame('01/10/2018', $denormalizedDate->format($format));
283+
}
284+
285+
public function testDenormalizeDateTimeStringWithDefaultContextAllowsErrorFormat()
286+
{
287+
$format = 'd/m/Y'; // the default format
288+
$string = '2020-01-01'; // the value which is in the wrong format, but is accepted because of `new \DateTime` in DateTimeNormalizer::denormalize
289+
290+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
291+
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);
292+
293+
$this->assertSame('2020-01-01', $denormalizedDate->format('Y-m-d'));
294+
}
295+
274296
public function testDenormalizeFormatMismatchThrowsException()
275297
{
276298
$this->expectException(UnexpectedValueException::class);

0 commit comments

Comments
 (0)