mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
[dateinterval doctrine type] parse correctly native date interval
This commit is contained in:
parent
36c3e33cfa
commit
06bff38701
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user