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