|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime as CoreDateTime; |
|
22
|
|
|
use DateTimeZone; |
|
23
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
24
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
25
|
|
|
use Doctrine\DBAL\Types\Type; |
|
26
|
|
|
use Surfnet\Stepup\DateTime\UtcDateTime; |
|
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Custom Type for the Surfnet\Stepup\DateTime\DateTime Object |
|
31
|
|
|
*/ |
|
32
|
|
|
class UtcDateTimeType extends Type |
|
33
|
|
|
{ |
|
34
|
|
|
const NAME = 'stepup_utc_datetime'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $fieldDeclaration |
|
38
|
|
|
* @param AbstractPlatform $platform |
|
39
|
|
|
* @return string |
|
40
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
43
|
|
|
{ |
|
44
|
|
|
return $platform->getDateTimeTypeDeclarationSQL($fieldDeclaration); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $value |
|
49
|
|
|
* @param AbstractPlatform $platform |
|
50
|
|
|
* @return null|string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($value === null) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!$value instanceof UtcDateTime) { |
|
59
|
|
|
throw new RuntimeException('Value given is not a Stepup DateTime'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $value->format($platform->getDateTimeFormatString()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* @param AbstractPlatform $platform |
|
68
|
|
|
* @return null|UtcDateTime |
|
69
|
|
|
* @throws ConversionException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_null($value)) { |
|
74
|
|
|
return $value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$dateTime = CoreDateTime::createFromFormat($platform->getDateTimeFormatString(), $value, new DateTimeZone('UTC')); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if (!$dateTime) { |
|
80
|
|
|
throw ConversionException::conversionFailedFormat( |
|
81
|
|
|
$value, |
|
82
|
|
|
$this->getName(), |
|
83
|
|
|
$platform->getDateTimeFormatString() |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new UtcDateTime($dateTime); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getName() |
|
91
|
|
|
{ |
|
92
|
|
|
return self::NAME; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.