Skip to content

Commit 0aa75d8

Browse files
committed
Fix compatibility for pre-2.7 DateIntervalType format
1 parent d4a96de commit 0aa75d8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Doctrine/DBAL/Types/DateIntervalType.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
5656
return $value;
5757
}
5858

59+
$negative = false;
60+
61+
if (isset($value[0]) && ($value[0] === '+' || $value[0] === '-')) {
62+
$negative = $value[0] === '-';
63+
$value = substr($value, 1);
64+
}
65+
5966
try {
60-
$interval = new \DateInterval(substr($value, 1));
67+
$interval = new \DateInterval($value);
6168

62-
if (substr($value, 0, 1) === '-') {
69+
if ($negative) {
6370
$interval->invert = 1;
6471
}
6572

tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function testNegativeDateIntervalConvertsToPHPValue() : void
6767
self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
6868
}
6969

70+
public function testDateIntervalFormatWithoutSignConvertsToPHPValue() : void
71+
{
72+
$interval = $this->type->convertToPHPValue('P02Y00M01DT01H02M03S', $this->platform);
73+
74+
self::assertInstanceOf(\DateInterval::class, $interval);
75+
self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
76+
}
77+
7078
public function testInvalidDateIntervalFormatConversion() : void
7179
{
7280
$this->expectException(ConversionException::class);
@@ -79,6 +87,13 @@ public function testDateIntervalNullConversion() : void
7987
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
8088
}
8189

90+
public function testDateIntervalEmptyStringConversion() : void
91+
{
92+
$this->expectException(ConversionException::class);
93+
94+
$this->type->convertToPHPValue('', $this->platform);
95+
}
96+
8297
/**
8398
* @group DBAL-1288
8499
*/

0 commit comments

Comments
 (0)