Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
72 lines (59 sloc) 1.7 KB
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types;
use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
use function date_create;
/**
* Type that maps an SQL DATETIME/TIMESTAMP to a PHP DateTime object.
*/
class DateTimeType extends Type implements PhpDateTimeMappingType
{
/**
* {@inheritdoc}
*/
public function getName() : string
{
return Types::DATETIME_MUTABLE;
}
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string
{
return $platform->getDateTimeTypeDeclarationSQL($fieldDeclaration);
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return $value;
}
if ($value instanceof DateTimeInterface) {
return $value->format($platform->getDateTimeFormatString());
}
throw InvalidType::new($value, $this->getName(), ['null', 'DateTime']);
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof DateTimeInterface) {
return $value;
}
$val = DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
if (! $val) {
$val = date_create($value);
}
if (! $val) {
throw InvalidFormat::new($value, $this->getName(), $platform->getDateTimeFormatString());
}
return $val;
}
}
You can’t perform that action at this time.