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
|
|
|
const FORMAT = '%RP%YY%MM%DDT%HH%IM%SS'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
32 |
|
public function getName() |
18
|
|
|
{ |
19
|
32 |
|
return Type::DATEINTERVAL; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
1 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
26
|
|
|
{ |
27
|
1 |
|
$fieldDeclaration['length'] = 255; |
28
|
1 |
|
$fieldDeclaration['fixed'] = true; |
29
|
|
|
|
30
|
1 |
|
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
16 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
37
|
|
|
{ |
38
|
16 |
|
if (null === $value) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
16 |
|
if ($value instanceof \DateInterval) { |
43
|
2 |
|
return $value->format(self::FORMAT); |
44
|
|
|
} |
45
|
|
|
|
46
|
14 |
|
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
4 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
53
|
|
|
{ |
54
|
4 |
|
if ($value === null || $value instanceof \DateInterval) { |
55
|
1 |
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
3 |
|
$interval = new \DateInterval(substr($value, 1)); |
60
|
|
|
|
61
|
2 |
|
if (substr($value, 0, 1) === '-') { |
62
|
1 |
|
$interval->invert = 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $interval; |
66
|
1 |
|
} catch (\Exception $exception) { |
67
|
1 |
|
throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT, $exception); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
749 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
75
|
|
|
{ |
76
|
749 |
|
return true; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|