VarDateTimeType.php 900 Bytes
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Types;

5
use DateTime;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use function date_create;
8 9

/**
Benjamin Morel's avatar
Benjamin Morel committed
10
 * Variable DateTime Type using date_create() instead of DateTime::createFromFormat().
11 12 13 14 15 16 17 18
 *
 * This type has performance implications as it runs twice as long as the regular
 * {@see DateTimeType}, however in certain PostgreSQL configurations with
 * TIMESTAMP(n) columns where n > 0 it is necessary to use this type.
 */
class VarDateTimeType extends DateTimeType
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
19
     * {@inheritdoc}
20 21 22
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
23
        if ($value === null || $value instanceof DateTime) {
24
            return $value;
25 26 27
        }

        $val = date_create($value);
28
        if ($val === false) {
29 30
            throw ConversionException::conversionFailed($value, $this->getName());
        }
31

32 33
        return $val;
    }
34
}