Skip to content

Commit

Permalink
Merge branch '4.4' into 5.4
Browse files Browse the repository at this point in the history
* 4.4:
  [HttpClient] Fix the CS fix
  Workaround disabled "var_dump"
  [Serializer] Respect default context in DateTimeNormalizer::denormalize

# Conflicts:
#	src/Symfony/Component/Debug/ErrorHandler.php
#	src/Symfony/Component/HttpClient/HttpClientTrait.php
#	src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php
  • Loading branch information
ogizanagi committed Jul 28, 2022
2 parents 443f8c3 + 375509c commit 412e2a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Normalizer/DateTimeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public function denormalize($data, string $type, string $format = null, array $c
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);
}

$defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null;

if (null !== $defaultDateTimeFormat) {
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone);

if (false !== $object) {
return $object;
}
}

try {
return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
} catch (\Exception $e) {
Expand Down
22 changes: 22 additions & 0 deletions Tests/Normalizer/DateTimeNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,28 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
}

public function testDenormalizeDateTimeStringWithDefaultContextFormat()
{
$format = 'd/m/Y';
$string = '01/10/2018';

$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);

$this->assertSame('01/10/2018', $denormalizedDate->format($format));
}

public function testDenormalizeDateTimeStringWithDefaultContextAllowsErrorFormat()
{
$format = 'd/m/Y'; // the default format
$string = '2020-01-01'; // the value which is in the wrong format, but is accepted because of `new \DateTime` in DateTimeNormalizer::denormalize

$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);

$this->assertSame('2020-01-01', $denormalizedDate->format('Y-m-d'));
}

public function testDenormalizeFormatMismatchThrowsException()
{
$this->expectException(UnexpectedValueException::class);
Expand Down

0 comments on commit 412e2a2

Please sign in to comment.