Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Fix denormalizing date intervals having both weeks and days #52626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public function supportsDenormalization($data, string $type, string $format = nu

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public function testDenormalizeIntervalsWithOmittedPartsBeingZero()
$this->assertDateIntervalEquals($this->getInterval('P0Y0M0DT12H34M0S'), $normalizer->denormalize('PT12H34M', \DateInterval::class));
}

/**
* Since PHP 8.0 DateInterval::construct supports periods containing both D and W period designators.
*
* @requires PHP 8
*/
public function testDenormalizeIntervalWithBothWeeksAndDays()
{
$input = 'P1W1D';
$interval = $this->normalizer->denormalize($input, \DateInterval::class, null, [
DateIntervalNormalizer::FORMAT_KEY => '%rP%yY%mM%wW%dDT%hH%iM%sS',
]);
$this->assertDateIntervalEquals($this->getInterval($input), $interval);
$this->assertSame(8, $interval->d);
}

public function testDenormalizeExpectsString()
{
$this->expectException(NotNormalizableValueException::class);
Expand Down
Loading