Skip to content

Commit

Permalink
Merge branch 'fix/#3093-dateinterval-legacy-format' into 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Apr 7, 2018
2 parents 2cc12a1 + 46fab66 commit f62711a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/Doctrine/DBAL/Types/DateIntervalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down

0 comments on commit f62711a

Please sign in to comment.