diff --git a/Doctrine/Type/NativeDateIntervalType.php b/Doctrine/Type/NativeDateIntervalType.php index 06143f229..fc341b3ef 100644 --- a/Doctrine/Type/NativeDateIntervalType.php +++ b/Doctrine/Type/NativeDateIntervalType.php @@ -4,6 +4,7 @@ namespace Chill\MainBundle\Doctrine\Type; use Doctrine\DBAL\Types\DateIntervalType; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\ConversionException; /** * Re-declare the date interval to use the implementation of date interval in @@ -23,5 +24,71 @@ class NativeDateIntervalType extends DateIntervalType { return 'INTERVAL'; } + + public function convertToPHPValue($value, AbstractPlatform $platform) + { + if ($value === null || $value instanceof \DateInterval) { + return $value; + } + dump($value); + try { + $strings = explode(' ', $value); + + if (count($strings) === 0) { + return null; + } + $intervalSpec = 'P'; + \reset($strings); + + do { + $intervalSpec .= $this->convertEntry($strings); + } while (next($strings) !== FALSE); + + dump($intervalSpec); + + return new \DateInterval($intervalSpec); + } catch (\Exception $exception) { + throw $this->createConversionException($value); + } + } + + private function convertEntry(&$strings) + { + $current = \current($strings); + + if (is_numeric($current)) { + $next = \next($strings); + switch($next) { + case 'year': + $unit = 'Y'; + break; + case 'mon': + case 'mons': + $unit = 'M'; + break; + case 'days': + $unit = 'D'; + break; + default: + throw $this->createConversionException($value); + } + + return $current.$unit; + + } elseif (\preg_match('/([0-9]{2}\:[0-9]{2}:[0-9]{2})/', $v) === 1) { + $tExploded = explode(':', $v); + $intervalSpec = 'T'; + $intervalSpec.= $tExploded[0].'H'; + $intervalSpec.= $tExploded[1].'M'; + $intervalSpec.= $tExploded[2].'S'; + + return $intervalSpec; + } + } + + protected function createConversionException($value) + { + return ConversionException::conversionFailedFormat($value, $this->getName(), 'xx year xx mons xx days 01:02:03', $exception); + } }