Skip to content

Commit

Permalink
Fix Date and Time types
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed May 23, 2018
1 parent c81f7ca commit dac6d13
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Time/DateType.php
Expand Up @@ -10,6 +10,7 @@
namespace Dogma\Doctrine\Time;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateType as DoctrineDateType;
use Doctrine\DBAL\Types\Type;
use Dogma\Time\Date;
Expand All @@ -24,6 +25,25 @@ public function getName(): string
return self::NAME;
}

/**
* @param mixed $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*
* @return mixed The database representation of the value.
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return $value;
} elseif ($value instanceof Date) {
return $value->format($platform->getDateFormatString());
} elseif ($value instanceof \DateTimeInterface) {
return $value->format($platform->getDateFormatString());
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTimeInterface', 'Dogma\\Time\\Date']);
}

/**
* @param mixed $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
Expand Down
14 changes: 14 additions & 0 deletions src/Time/TimeType.php
Expand Up @@ -10,6 +10,7 @@
namespace Dogma\Doctrine\Time;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\TimeType as DoctrineTimeType;
use Doctrine\DBAL\Types\Type;
use Dogma\Time\Time;
Expand All @@ -24,6 +25,19 @@ public function getName(): string
return self::NAME;
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return $value;
} elseif ($value instanceof Time) {
return $value->format($platform->getTimeFormatString());
} elseif ($value instanceof \DateTimeInterface) {
return $value->format($platform->getTimeFormatString());
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTimeInterface', 'Dogma\\Time\\Time']);
}

/**
* @param mixed $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
Expand Down

0 comments on commit dac6d13

Please sign in to comment.