1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection\Stdlib\Scalar; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\Definition\ScalarDefinition; |
13
|
|
|
use Railt\Reflection\Document; |
14
|
|
|
use Railt\Reflection\Exception\TypeConflictException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DateTimeScalar |
18
|
|
|
*/ |
19
|
|
|
final class DateTimeScalar extends ScalarDefinition |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public const TYPE_NAME = 'DateTime'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public const TYPE_DESCRIPTION = <<<Description |
30
|
|
|
The DateTime scalar conforms to the **RFC 3339** |
31
|
|
|
profile of the **ISO 8601** standard. |
32
|
|
|
Description; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private const DEFINITION_LINE = 31; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* BooleanScalar constructor. |
41
|
|
|
* @param Document $document |
42
|
|
|
*/ |
43
|
9 |
|
public function __construct(Document $document) |
44
|
|
|
{ |
45
|
9 |
|
parent::__construct($document, self::TYPE_NAME); |
46
|
|
|
|
47
|
9 |
|
$this->withDescription(self::TYPE_DESCRIPTION); |
48
|
9 |
|
$this->withLine(self::DEFINITION_LINE); |
49
|
9 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $value |
53
|
|
|
* @return \DateTimeInterface |
54
|
|
|
* @throws TypeConflictException |
55
|
|
|
*/ |
56
|
|
|
public function parse($value): \DateTimeInterface |
57
|
|
|
{ |
58
|
|
|
if ($value instanceof \DateTimeInterface) { |
59
|
|
|
return $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (! \is_scalar($value)) { |
63
|
|
|
throw new TypeConflictException(\sprintf('Could not parse %s type', \gettype($value))); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->parseDateTime((string)parent::parse($value)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $value |
71
|
|
|
* @return string |
72
|
|
|
* @throws TypeConflictException |
73
|
|
|
*/ |
74
|
|
|
public function serialize($value): string |
75
|
|
|
{ |
76
|
|
|
if ($value instanceof \DateTimeInterface) { |
77
|
|
|
return $value->format(\DateTime::RFC3339); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (! \is_scalar($value)) { |
81
|
|
|
throw new TypeConflictException(\sprintf('Could not serialize %s type', \gettype($value))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->parseDateTime((string)$value) |
85
|
|
|
->format(\DateTime::RFC3339); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $value |
90
|
|
|
* @return \DateTimeInterface |
91
|
|
|
* @throws TypeConflictException |
92
|
|
|
*/ |
93
|
|
|
private function parseDateTime(string $value): \DateTimeInterface |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
return new \DateTime((string)$value, new \DateTimeZone('UTC')); |
97
|
|
|
} catch (\Throwable $e) { |
98
|
|
|
$message = \str_replace('DateTime::__construct(): ', '', $e->getMessage()); |
99
|
|
|
|
100
|
|
|
throw new TypeConflictException($message, $e->getCode()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isBuiltin(): bool |
108
|
|
|
{ |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|