Commit 9f72045a authored by Marco Pivetta's avatar Marco Pivetta

#869 - DBAL-1293 - simplified conversion logic for readability

parent bd142a58
...@@ -49,12 +49,15 @@ class DateTimeType extends Type ...@@ -49,12 +49,15 @@ class DateTimeType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateTimeFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), 'DateTime');
? $value->format($platform->getDateTimeFormatString()) : null;
} }
/** /**
......
...@@ -67,12 +67,15 @@ class DateTimeTzType extends Type ...@@ -67,12 +67,15 @@ class DateTimeTzType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateTimeTzFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), 'DateTime');
? $value->format($platform->getDateTimeTzFormatString()) : null;
} }
/** /**
......
...@@ -49,12 +49,15 @@ class DateType extends Type ...@@ -49,12 +49,15 @@ class DateType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), 'DateTime');
? $value->format($platform->getDateFormatString()) : null;
} }
/** /**
......
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