Skip to content

Commit

Permalink
Removed regex. DateInterval now stored as P0002-00-01T01:02:03
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentinas Bartusevičius committed Jul 16, 2015
1 parent 30ca3a4 commit 7158534
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
12 changes: 4 additions & 8 deletions lib/Doctrine/DBAL/Types/DateIntervalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
class DateIntervalType extends Type
{
const DATEINTERVAL_PATTERN = '#(?P<date>\d{4}-\d{2}-\d{2}).(?P<time>\d{2}:\d{2}:\d{2})#';

/**
* {@inheritdoc}
*/
Expand All @@ -35,9 +33,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
$spec = null;
if ($value !== null) {
/** @var \DateInterval $value */
$spec = str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-'
$spec = 'P'
. str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-'
. $value->format('%M') . '-'
. $value->format('%D') . ' '
. $value->format('%D') . 'T'
. $value->format('%H') . ':'
. $value->format('%I') . ':'
. $value->format('%S')
Expand All @@ -56,11 +55,8 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return $value;
}

if (preg_match(self::DATEINTERVAL_PATTERN, $value, $parts) !== 1) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Y-m-d H:i:s');
}
try {
$interval = new \DateInterval('P' . $parts['date'] . 'T' . $parts['time']);
$interval = new \DateInterval($value);
} catch (\Exception $e) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s');

Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function testDateIntervalConvertsToDatabaseValue()
{
$interval = new \DateInterval('P2Y1DT1H2M3S');

$expected = '0002-00-01 01:02:03';
$expected = 'P0002-00-01T01:02:03';
$actual = $this->_type->convertToDatabaseValue($interval, $this->_platform);

$this->assertEquals($expected, $actual);
}

public function testDateIntervalConvertsToPHPValue()
{
$date = $this->_type->convertToPHPValue('0002-00-01 01:02:03', $this->_platform);
$date = $this->_type->convertToPHPValue('P0002-00-01T01:02:03', $this->_platform);
$this->assertInstanceOf('DateInterval', $date);
$this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
}
Expand Down

0 comments on commit 7158534

Please sign in to comment.