Completed
Pull Request — master (#2579)
by Luís
04:33
created

DateIntervalType::requiresSQLCommentHint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
7
/**
8
 * Type that maps interval string to a PHP DateInterval Object.
9
 */
10
class DateIntervalType extends Type
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 32
    public function getName()
16
    {
17 32
        return Type::DATEINTERVAL;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 1
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
24
    {
25 1
        $fieldDeclaration['length'] = 255;
26 1
        $fieldDeclaration['fixed']  = true;
27
28 1
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 16
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
35
    {
36 16
        if (null === $value) {
37
            return null;
38
        }
39
40 16
        if ($value instanceof \DateInterval) {
41 2
            return $value->format('%RP%YY%MM%DDT%HH%IM%SS');
42
        }
43
44 14
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    public function convertToPHPValue($value, AbstractPlatform $platform)
51
    {
52 4
        if ($value === null || $value instanceof \DateInterval) {
53 1
            return $value;
54
        }
55
56
        try {
57 3
            $interval = new \DateInterval(substr($value, 1));
58 2
            if (substr($value, 0, 1) === '-') {
59 1
                $interval->invert = 1;
60
            }
61 2
            return $interval;
62 1
        } catch (\Exception $exception) {
63 1
            throw ConversionException::conversionFailedFormat($value, $this->getName(), '%RP%YY%MM%DDT%HH%IM%SS', $exception);
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 749
    public function requiresSQLCommentHint(AbstractPlatform $platform)
71
    {
72 749
        return true;
73
    }
74
}
75