Code Duplication    Length = 51-51 lines in 2 locations

lib/Doctrine/DBAL/Types/DateType.php 1 location

@@ 29-79 (lines=51) @@
26
 *
27
 * @since 2.0
28
 */
29
class DateType extends Type
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getName()
35
    {
36
        return Type::DATE;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
43
    {
44
        return $platform->getDateTypeDeclarationSQL($fieldDeclaration);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
51
    {
52
        if (null === $value) {
53
            return $value;
54
        }
55
56
        if ($value instanceof \DateTimeInterface) {
57
            return $value->format($platform->getDateFormatString());
58
        }
59
60
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function convertToPHPValue($value, AbstractPlatform $platform)
67
    {
68
        if ($value === null || $value instanceof \DateTimeInterface) {
69
            return $value;
70
        }
71
72
        $val = \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value);
73
        if ( ! $val) {
74
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateFormatString());
75
        }
76
77
        return $val;
78
    }
79
}
80

lib/Doctrine/DBAL/Types/TimeType.php 1 location

@@ 29-79 (lines=51) @@
26
 *
27
 * @since 2.0
28
 */
29
class TimeType extends Type
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getName()
35
    {
36
        return Type::TIME;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
43
    {
44
        return $platform->getTimeTypeDeclarationSQL($fieldDeclaration);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
51
    {
52
        if (null === $value) {
53
            return $value;
54
        }
55
56
        if ($value instanceof \DateTimeInterface) {
57
            return $value->format($platform->getTimeFormatString());
58
        }
59
60
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function convertToPHPValue($value, AbstractPlatform $platform)
67
    {
68
        if ($value === null || $value instanceof \DateTimeInterface) {
69
            return $value;
70
        }
71
72
        $val = \DateTime::createFromFormat('!' . $platform->getTimeFormatString(), $value);
73
        if ( ! $val) {
74
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getTimeFormatString());
75
        }
76
77
        return $val;
78
    }
79
}
80