Skip to content

Commit b2c68ce

Browse files
bug symfony#52626 [Serializer] Fix denormalizing date intervals having both weeks and days (oneNevan)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix denormalizing date intervals having both weeks and days | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#47629 | License | MIT Added support for date periods having both W and D period designators combined together (which is valid duration for ISO 8601-2). Starting from PHP 8 this is supported by \DateTimeInterval, see https://bugs.php.net/bug.php?id=61366 for reference Commits ------- c88d49e [Serializer] Fix denormalizing date intervals having both weeks and days
2 parents b558912 + c88d49e commit b2c68ce

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public function supportsDenormalization($data, string $type, string $format = nu
128128

129129
private function isISO8601(string $string): bool
130130
{
131+
if (\PHP_VERSION_ID >= 80000) {
132+
return preg_match('/^[\-+]?P(?=\w*(?:\d|%\w))(?:\d+Y|%[yY]Y)?(?:\d+M|%[mM]M)?(?:\d+W|%[wW]W)?(?:\d+D|%[dD]D)?(?:T(?:\d+H|[hH]H)?(?:\d+M|[iI]M)?(?:\d+S|[sS]S)?)?$/', $string);
133+
}
134+
131135
return preg_match('/^[\-+]?P(?=\w*(?:\d|%\w))(?:\d+Y|%[yY]Y)?(?:\d+M|%[mM]M)?(?:(?:\d+D|%[dD]D)|(?:\d+W|%[wW]W))?(?:T(?:\d+H|[hH]H)?(?:\d+M|[iI]M)?(?:\d+S|[sS]S)?)?$/', $string);
132136
}
133137
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ public function testDenormalizeIntervalsWithOmittedPartsBeingZero()
119119
$this->assertDateIntervalEquals($this->getInterval('P0Y0M0DT12H34M0S'), $normalizer->denormalize('PT12H34M', \DateInterval::class));
120120
}
121121

122+
/**
123+
* Since PHP 8.0 DateInterval::construct supports periods containing both D and W period designators.
124+
*
125+
* @requires PHP 8
126+
*/
127+
public function testDenormalizeIntervalWithBothWeeksAndDays()
128+
{
129+
$input = 'P1W1D';
130+
$interval = $this->normalizer->denormalize($input, \DateInterval::class, null, [
131+
DateIntervalNormalizer::FORMAT_KEY => '%rP%yY%mM%wW%dDT%hH%iM%sS',
132+
]);
133+
$this->assertDateIntervalEquals($this->getInterval($input), $interval);
134+
$this->assertSame(8, $interval->d);
135+
}
136+
122137
public function testDenormalizeExpectsString()
123138
{
124139
$this->expectException(NotNormalizableValueException::class);

0 commit comments

Comments
 (0)