Skip to content

Commit a285f47

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: Specify that we run CI on Ubuntu-20.04 zero parts can be omitted in date interval input
2 parents c93f6de + 9fa3632 commit a285f47

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Normalizer/DateIntervalNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function denormalize($data, string $type, string $format = null, array $c
9696
$dateIntervalFormat = substr($dateIntervalFormat, 2);
9797
break;
9898
}
99-
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $dateIntervalFormat).'$/';
99+
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?:(?P<$1>\d+)$2)?', preg_replace('/(T.*)$/', '($1)?', $dateIntervalFormat)).'$/';
100100
if (!preg_match($valuePattern, $data)) {
101101
throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
102102
}

Tests/Normalizer/DateIntervalNormalizerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ public function testDenormalizeUsingFormatPassedInConstructor($format, $input, $
104104
$this->assertDateIntervalEquals($this->getInterval($output), $normalizer->denormalize($input, \DateInterval::class));
105105
}
106106

107+
public function testDenormalizeIntervalsWithOmittedPartsBeingZero()
108+
{
109+
$normalizer = new DateIntervalNormalizer();
110+
111+
$this->assertDateIntervalEquals($this->getInterval('P3Y2M4DT0H0M0S'), $normalizer->denormalize('P3Y2M4D', \DateInterval::class));
112+
$this->assertDateIntervalEquals($this->getInterval('P0Y0M0DT12H34M0S'), $normalizer->denormalize('PT12H34M', \DateInterval::class));
113+
}
114+
107115
public function testDenormalizeExpectsString()
108116
{
109117
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)