diff --git a/lib/Doctrine/DBAL/Types/DateIntervalType.php b/lib/Doctrine/DBAL/Types/DateIntervalType.php index fee21477d8b..7208da97cbc 100644 --- a/lib/Doctrine/DBAL/Types/DateIntervalType.php +++ b/lib/Doctrine/DBAL/Types/DateIntervalType.php @@ -56,10 +56,17 @@ public function convertToPHPValue($value, AbstractPlatform $platform) return $value; } + $negative = false; + + if (isset($value[0]) && ($value[0] === '+' || $value[0] === '-')) { + $negative = $value[0] === '-'; + $value = substr($value, 1); + } + try { - $interval = new \DateInterval(substr($value, 1)); + $interval = new \DateInterval($value); - if (substr($value, 0, 1) === '-') { + if ($negative) { $interval->invert = 1; } diff --git a/tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php b/tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php index c348dad2313..61919ec6c87 100644 --- a/tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php @@ -67,6 +67,14 @@ public function testNegativeDateIntervalConvertsToPHPValue() : void self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT)); } + public function testDateIntervalFormatWithoutSignConvertsToPHPValue() : void + { + $interval = $this->type->convertToPHPValue('P02Y00M01DT01H02M03S', $this->platform); + + self::assertInstanceOf(\DateInterval::class, $interval); + self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT)); + } + public function testInvalidDateIntervalFormatConversion() : void { $this->expectException(ConversionException::class); @@ -79,6 +87,13 @@ public function testDateIntervalNullConversion() : void self::assertNull($this->type->convertToPHPValue(null, $this->platform)); } + public function testDateIntervalEmptyStringConversion() : void + { + $this->expectException(ConversionException::class); + + $this->type->convertToPHPValue('', $this->platform); + } + /** * @group DBAL-1288 */