Skip to content

Commit c88d49e

Browse files
committed
[Serializer] Fix denormalizing date intervals having both weeks and days
1 parent 4d140c3 commit c88d49e

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)