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 negative DateInterval #33066

Merged
merged 1 commit into from Aug 9, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,7 +29,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
/**
* @param string $format
*/
public function __construct($format = 'P%yY%mM%dDT%hH%iM%sS')
public function __construct($format = '%rP%yY%mM%dDT%hH%iM%sS')
{
$this->format = $format;
}
Expand Down Expand Up @@ -76,12 +76,34 @@ public function denormalize($data, $class, $format = null, array $context = [])

$dateIntervalFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format;

$valuePattern = '/^'.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $dateIntervalFormat).'$/';
$signPattern = '';
switch (substr($dateIntervalFormat, 0, 2)) {
case '%R':
$signPattern = '[-+]';
$dateIntervalFormat = substr($dateIntervalFormat, 2);
break;
case '%r':
$signPattern = '-?';
$dateIntervalFormat = substr($dateIntervalFormat, 2);
break;
}
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $dateIntervalFormat).'$/';
if (!preg_match($valuePattern, $data)) {
throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
}

try {
if ('-' === $data[0]) {
$interval = new \DateInterval(substr($data, 1));
$interval->invert = 1;

return $interval;
}

if ('+' === $data[0]) {
return new \DateInterval(substr($data, 1));
}

return new \DateInterval($data);
} catch (\Exception $e) {
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -98,6 +120,6 @@ public function supportsDenormalization($data, $type, $format = null)

private function isISO8601($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);
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);
}
}
Expand Up @@ -29,6 +29,11 @@ public function dataProviderISO()
['P%yY%mM%dDT%hH%iM', 'P10Y2M3DT16H5M', 'P10Y2M3DT16H5M'],
['P%yY%mM%dDT%hH', 'P10Y2M3DT16H', 'P10Y2M3DT16H'],
['P%yY%mM%dD', 'P10Y2M3D', 'P10Y2M3DT0H'],
['%RP%yY%mM%dD', '-P10Y2M3D', '-P10Y2M3DT0H'],
['%RP%yY%mM%dD', '+P10Y2M3D', '+P10Y2M3DT0H'],
['%RP%yY%mM%dD', '+P10Y2M3D', 'P10Y2M3DT0H'],
['%rP%yY%mM%dD', '-P10Y2M3D', '-P10Y2M3DT0H'],
['%rP%yY%mM%dD', 'P10Y2M3D', 'P10Y2M3DT0H'],
];

return $data;
Expand All @@ -50,15 +55,15 @@ public function testNormalize()
*/
public function testNormalizeUsingFormatPassedInContext($format, $output, $input)
{
$this->assertEquals($output, $this->normalizer->normalize(new \DateInterval($input), null, [DateIntervalNormalizer::FORMAT_KEY => $format]));
$this->assertEquals($output, $this->normalizer->normalize($this->getInterval($input), null, [DateIntervalNormalizer::FORMAT_KEY => $format]));
}

/**
* @dataProvider dataProviderISO
*/
public function testNormalizeUsingFormatPassedInConstructor($format, $output, $input)
{
$this->assertEquals($output, (new DateIntervalNormalizer($format))->normalize(new \DateInterval($input)));
$this->assertEquals($output, (new DateIntervalNormalizer($format))->normalize($this->getInterval($input)));
}

public function testNormalizeInvalidObjectThrowsException()
Expand All @@ -84,15 +89,15 @@ public function testDenormalize()
*/
public function testDenormalizeUsingFormatPassedInContext($format, $input, $output)
{
$this->assertDateIntervalEquals(new \DateInterval($output), $this->normalizer->denormalize($input, \DateInterval::class, null, [DateIntervalNormalizer::FORMAT_KEY => $format]));
$this->assertDateIntervalEquals($this->getInterval($input), $this->normalizer->denormalize($input, \DateInterval::class, null, [DateIntervalNormalizer::FORMAT_KEY => $format]));
}

/**
* @dataProvider dataProviderISO
*/
public function testDenormalizeUsingFormatPassedInConstructor($format, $input, $output)
{
$this->assertDateIntervalEquals(new \DateInterval($output), (new DateIntervalNormalizer($format))->denormalize($input, \DateInterval::class));
$this->assertDateIntervalEquals($this->getInterval($input), (new DateIntervalNormalizer($format))->denormalize($input, \DateInterval::class));
}

public function testDenormalizeExpectsString()
Expand Down Expand Up @@ -124,4 +129,20 @@ private function assertDateIntervalEquals(\DateInterval $expected, \DateInterval
{
$this->assertEquals($expected->format('%RP%yY%mM%dDT%hH%iM%sS'), $actual->format('%RP%yY%mM%dDT%hH%iM%sS'));
}

private function getInterval($data)
{
if ('-' === $data[0]) {
$interval = new \DateInterval(substr($data, 1));
$interval->invert = 1;

return $interval;
}

if ('+' === $data[0]) {
return new \DateInterval(substr($data, 1));
}

return new \DateInterval($data);
}
}