Skip to content

Commit

Permalink
[Serializer] Fix denormalizing date intervals having both weeks and days
Browse files Browse the repository at this point in the history
  • Loading branch information
oneNevan committed Nov 20, 2023
1 parent 4d140c3 commit c88d49e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit c88d49e

Please sign in to comment.