Closed
Description
Symfony version(s) affected
5.4.12,6.1.4
Description
Since PHP 8.0 DateInterval::construct supports periods containing both D and W period designators
echo (new \DateInterval('P3W2D'))->format('%d');
// outputs '2' prior PHP 8.0
// outputs '23' in PHP >= 8.0
The problem is the built-in DateIntervalNormalizer does not support it, so it throws exception if both weeks and date exist in source period string as current regular expression in method isISO8601 does not allow it
if (!$this->isISO8601($data)) {
throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
}
How to reproduce
// PHP >= 8.0
$normalizer = new \Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer();
$object = $normalizer->denormalize('P2W6D', \DateInterval::class, null, [
'dateinterval_format' => '%rP%yY%mM%wW%dDT%hH%iM%sS',
]);
// expected: $object instanceof \DateInterval
// actual: \Symfony\Component\Serializer\Exception\UnexpectedValueException thrown
Possible Solution
I believe a possible solution would be to update regular expression used in isISO8601 method for PHP versions >= 8.0, like this:
private function isISO8601(string $string): bool
{
if (PHP_VERSION_ID >= 80000) {
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);
}
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);
}
Additional Context
No response