household list of accompanying periods + upgrade DBAL version to 3.1

This commit is contained in:
2021-11-08 10:57:14 +00:00
committed by Julien Fastré
parent 092ea4d57f
commit 7fabe0214e
49 changed files with 353 additions and 286 deletions

View File

@@ -15,17 +15,17 @@ use Doctrine\DBAL\Types\ConversionException;
class NativeDateIntervalType extends DateIntervalType
{
const FORMAT = '%rP%YY%MM%DDT%HH%IM%SS';
public function getName(): string
{
return \Doctrine\DBAL\Types\Type::DATEINTERVAL;
return \Doctrine\DBAL\Types\Types::DATEINTERVAL;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
return 'INTERVAL';
}
/**
* {@inheritdoc}
*/
@@ -41,36 +41,36 @@ class NativeDateIntervalType extends DateIntervalType
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof \DateInterval) {
return $value;
}
try {
$strings = explode(' ', $value);
if (count($strings) === 0) {
return null;
}
$intervalSpec = 'P';
\reset($strings);
do {
$intervalSpec .= $this->convertEntry($strings);
} while (next($strings) !== FALSE);
return new \DateInterval($intervalSpec);
} catch (\Exception $exception) {
throw $this->createConversionException($value, $exception);
}
}
private function convertEntry(&$strings)
{
$current = \current($strings);
if (is_numeric($current)) {
$next = \next($strings);
switch($next) {
@@ -79,7 +79,7 @@ class NativeDateIntervalType extends DateIntervalType
$unit = 'Y';
break;
case 'mon':
case 'mons':
case 'mons':
$unit = 'M';
break;
case 'day':
@@ -89,20 +89,20 @@ class NativeDateIntervalType extends DateIntervalType
default:
throw $this->createConversionException(implode('', $strings));
}
return $current.$unit;
} elseif (\preg_match('/([0-9]{2}\:[0-9]{2}:[0-9]{2})/', $current) === 1) {
$tExploded = explode(':', $current);
$intervalSpec = 'T';
$intervalSpec.= $tExploded[0].'H';
$intervalSpec.= $tExploded[1].'M';
$intervalSpec.= $tExploded[2].'S';
return $intervalSpec;
}
}
protected function createConversionException($value, $exception = null)
{
return ConversionException::conversionFailedFormat($value, $this->getName(), 'xx year xx mons xx days 01:02:03', $exception);