Skip to content

Commit

Permalink
Merge branch '5.4' into 6.3
Browse files Browse the repository at this point in the history
* 5.4:
  [5.4] Remove unused test fixtures
  [Dotent] Add PHPDoc for `$overrideExistingVars`
  [SecurityBundle] Fix missing login-link element in xsd schema
  [Validator] Add missing Chinese translations #51934
  [Serializer] Fix using `DateIntervalNormalizer` with union types
  [Validator] fix: add missing translations for for Thai (th)
  fix #52273 [doctrine-messenger] DB table locks on messenger_messages with many failures
  [Serializer] Handle defaultContext for DateTimeNormalizer
  [CI] Add step to verify symfony/deprecation-contracts requirements
  • Loading branch information
fabpot committed Oct 26, 2023
2 parents 8c5fb71 + ceadb4e commit 641472d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
13 changes: 6 additions & 7 deletions Normalizer/DateIntervalNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;

/**
* Normalizes an instance of {@see \DateInterval} to an interval string.
Expand Down Expand Up @@ -73,17 +73,16 @@ public function hasCacheableSupportsMethod(): bool
}

/**
* @throws InvalidArgumentException
* @throws UnexpectedValueException
* @throws NotNormalizableValueException
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): \DateInterval
{
if (!\is_string($data)) {
throw new InvalidArgumentException(sprintf('Data expected to be a string, "%s" given.', get_debug_type($data)));
throw NotNormalizableValueException::createForUnexpectedDataType('Data expected to be a string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
}

if (!$this->isISO8601($data)) {
throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
throw NotNormalizableValueException::createForUnexpectedDataType('Expected a valid ISO 8601 interval string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
}

$dateIntervalFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
Expand All @@ -101,7 +100,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
}
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?:(?P<$1>\d+)$2)?', preg_replace('/(T.*)$/', '($1)?', $dateIntervalFormat)).'$/';
if (!preg_match($valuePattern, $data)) {
throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat), $data, ['string'], $context['deserialization_path'] ?? null, false);
}

try {
Expand All @@ -118,7 +117,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

return new \DateInterval($data);
} catch (\Exception $e) {
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, ['string'], $context['deserialization_path'] ?? null, false, $e->getCode(), $e);
}
}

Expand Down
8 changes: 4 additions & 4 deletions Normalizer/DateTimeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,8 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): \DateTimeInterface
{
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context);

if (\is_int($data) || \is_float($data)) {
switch ($dateTimeFormat) {
switch ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) {
case 'U': $data = sprintf('%d', $data); break;
case 'U.u': $data = sprintf('%.6F', $data); break;
}
Expand All @@ -108,6 +105,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
}

try {
$timezone = $this->getTimezone($context);
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;

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

Expand Down
10 changes: 5 additions & 5 deletions Tests/Normalizer/DateIntervalNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;

/**
Expand Down Expand Up @@ -123,26 +123,26 @@ public function testDenormalizeIntervalsWithOmittedPartsBeingZero()

public function testDenormalizeExpectsString()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize(1234, \DateInterval::class);
}

public function testDenormalizeNonISO8601IntervalStringThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('Expected a valid ISO 8601 interval string.');
$this->normalizer->denormalize('10 years 2 months 3 days', \DateInterval::class, null);
}

public function testDenormalizeInvalidDataThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize('invalid interval', \DateInterval::class);
}

public function testDenormalizeFormatMismatchThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class, null, [DateIntervalNormalizer::FORMAT_KEY => 'P%yY%mM%dD']);
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/Normalizer/DateTimeNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
}

public function testDenormalizeTimestampWithFormatInContext()
{
$normalizer = new DateTimeNormalizer();
$denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);

$this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s'));
}

public function testDenormalizeTimestampWithFormatInDefaultContext()
{
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'U']);
$denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class);

$this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s'));
}

public function testDenormalizeDateTimeStringWithDefaultContextFormat()
{
$format = 'd/m/Y';
Expand Down

0 comments on commit 641472d

Please sign in to comment.