|
1
|
|
|
<?php namespace Limoncello\Flute\Types; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
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
|
|
|
use DateTimeImmutable; |
|
20
|
|
|
use DateTimeInterface; |
|
21
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
22
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
23
|
|
|
use Doctrine\DBAL\Types\DateTimeType; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @package Limoncello\Flute |
|
27
|
|
|
*/ |
|
28
|
|
|
class JsonApiDateTimeType extends DateTimeType |
|
29
|
|
|
{ |
|
30
|
|
|
/** Type name */ |
|
31
|
|
|
const NAME = 'limoncelloDateTime'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
37
|
|
|
{ |
|
38
|
|
|
$dateTimeOrNot = parent::convertToPHPValue($value, $platform); |
|
39
|
|
|
|
|
40
|
|
|
return $dateTimeOrNot instanceof DateTimeInterface ? new JsonApiDateTime($dateTimeOrNot) : $dateTimeOrNot; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
* |
|
46
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
47
|
|
|
*/ |
|
48
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
49
|
|
|
{ |
|
50
|
|
|
if (is_string($value) === false || |
|
51
|
|
|
($dateTime = DateTimeImmutable::createFromFormat(DateBaseType::JSON_API_FORMAT, $value)) === false |
|
52
|
|
|
) { |
|
53
|
|
|
throw ConversionException::conversionFailed($value, $this->getName()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return parent::convertToDatabaseValue($dateTime, $platform); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|