Removed regex. DateInterval now stored as P0002-00-01T01:02:03

parent 30ca3a42
...@@ -9,8 +9,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -9,8 +9,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/ */
class DateIntervalType extends Type class DateIntervalType extends Type
{ {
const DATEINTERVAL_PATTERN = '#(?P<date>\d{4}-\d{2}-\d{2}).(?P<time>\d{2}:\d{2}:\d{2})#';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -35,9 +33,10 @@ class DateIntervalType extends Type ...@@ -35,9 +33,10 @@ class DateIntervalType extends Type
$spec = null; $spec = null;
if ($value !== null) { if ($value !== null) {
/** @var \DateInterval $value */ /** @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('%M') . '-'
. $value->format('%D') . ' ' . $value->format('%D') . 'T'
. $value->format('%H') . ':' . $value->format('%H') . ':'
. $value->format('%I') . ':' . $value->format('%I') . ':'
. $value->format('%S') . $value->format('%S')
...@@ -56,11 +55,8 @@ class DateIntervalType extends Type ...@@ -56,11 +55,8 @@ class DateIntervalType extends Type
return $value; return $value;
} }
if (preg_match(self::DATEINTERVAL_PATTERN, $value, $parts) !== 1) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Y-m-d H:i:s');
}
try { try {
$interval = new \DateInterval('P' . $parts['date'] . 'T' . $parts['time']); $interval = new \DateInterval($value);
} catch (\Exception $e) { } catch (\Exception $e) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s'); throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s');
......
...@@ -21,7 +21,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase ...@@ -21,7 +21,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
{ {
$interval = new \DateInterval('P2Y1DT1H2M3S'); $interval = new \DateInterval('P2Y1DT1H2M3S');
$expected = '0002-00-01 01:02:03'; $expected = 'P0002-00-01T01:02:03';
$actual = $this->_type->convertToDatabaseValue($interval, $this->_platform); $actual = $this->_type->convertToDatabaseValue($interval, $this->_platform);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
...@@ -29,7 +29,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase ...@@ -29,7 +29,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public function testDateIntervalConvertsToPHPValue() 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->assertInstanceOf('DateInterval', $date);
$this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS')); $this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment