1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | namespace Application\DoctrineExtensions\DBAL\Types; |
||
5 | |||
6 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
7 | use Doctrine\DBAL\Types\ConversionException; |
||
8 | use Doctrine\DBAL\Types\DateTimeType; |
||
9 | |||
10 | /** |
||
11 | * Save datetime values in UTC in the database |
||
12 | * |
||
13 | * @package Chamilo\CoreBundle\DoctrineExtensions\DBAL\Types |
||
14 | */ |
||
15 | class UTCDateTimeType extends DateTimeType |
||
16 | { |
||
17 | static private $utc = null; |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
||
23 | { |
||
24 | if ($value === null) { |
||
25 | return null; |
||
26 | } |
||
27 | |||
28 | if (is_null(self::$utc)) { |
||
29 | self::$utc = new \DateTimeZone('UTC'); |
||
30 | } |
||
31 | |||
32 | $value->setTimeZone(self::$utc); |
||
33 | |||
34 | return $value->format($platform->getDateTimeFormatString()); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function convertToPHPValue($value, AbstractPlatform $platform) |
||
41 | { |
||
42 | if ($value === null) { |
||
43 | return null; |
||
44 | } |
||
45 | |||
46 | if (is_null(self::$utc)) { |
||
47 | self::$utc = new \DateTimeZone('UTC'); |
||
48 | } |
||
49 | |||
50 | $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc); |
||
51 | |||
52 | if (!$val) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
53 | throw ConversionException::conversionFailed($value, $this->getName()); |
||
54 | } |
||
55 | |||
56 | return $val; |
||
57 | } |
||
58 | } |
||
59 |