|
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 = 'The DateTime scalar conforms to the RFC 3339 profile of the ISO 8601 standard.'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* BooleanScalar constructor. |
|
33
|
|
|
* @param Document $document |
|
34
|
|
|
*/ |
|
35
|
9 |
|
public function __construct(Document $document) |
|
36
|
|
|
{ |
|
37
|
9 |
|
parent::__construct($document, self::TYPE_NAME); |
|
38
|
|
|
|
|
39
|
9 |
|
$this->withDescription(self::TYPE_DESCRIPTION); |
|
40
|
9 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param mixed $value |
|
44
|
|
|
* @return \DateTimeInterface |
|
45
|
|
|
* @throws TypeConflictException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function parse($value): \DateTimeInterface |
|
48
|
|
|
{ |
|
49
|
|
|
if ($value instanceof \DateTimeInterface) { |
|
50
|
|
|
return $value; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (! \is_scalar($value)) { |
|
54
|
|
|
throw new TypeConflictException(\sprintf('Could not parse %s type', \gettype($value))); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->parseDateTime((string)parent::parse($value)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
* @return string |
|
63
|
|
|
* @throws TypeConflictException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function serialize($value): string |
|
66
|
|
|
{ |
|
67
|
|
|
if ($value instanceof \DateTimeInterface) { |
|
68
|
|
|
return $value->format(\DateTime::RFC3339); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (! \is_scalar($value)) { |
|
72
|
|
|
throw new TypeConflictException(\sprintf('Could not serialize %s type', \gettype($value))); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->parseDateTime((string)$value) |
|
76
|
|
|
->format(\DateTime::RFC3339); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $value |
|
81
|
|
|
* @return \DateTimeInterface |
|
82
|
|
|
* @throws TypeConflictException |
|
83
|
|
|
*/ |
|
84
|
|
|
private function parseDateTime(string $value): \DateTimeInterface |
|
85
|
|
|
{ |
|
86
|
|
|
try { |
|
87
|
|
|
return new \DateTime((string)$value, new \DateTimeZone('UTC')); |
|
88
|
|
|
} catch (\Throwable $e) { |
|
89
|
|
|
$message = \str_replace('DateTime::__construct(): ', '', $e->getMessage()); |
|
90
|
|
|
|
|
91
|
|
|
throw new TypeConflictException($message, $e->getCode()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getLine(): int |
|
99
|
|
|
{ |
|
100
|
|
|
return 31; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function isBuiltin(): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|