1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Andi\GraphQL\Type; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
8
|
|
|
use GraphQL\Error\InvariantViolation; |
9
|
|
|
use GraphQL\Language\AST\Node; |
10
|
|
|
use GraphQL\Language\AST\StringValueNode; |
11
|
|
|
use GraphQL\Language\Printer; |
12
|
|
|
use GraphQL\Utils\Utils; |
13
|
|
|
|
14
|
|
|
final class DateTime extends AbstractScalarType |
15
|
|
|
{ |
16
|
|
|
protected string $name = 'DateTime'; |
17
|
|
|
|
18
|
|
|
protected string $description = <<<'STR' |
19
|
|
|
The `DateTime` scalar type represents time data, |
20
|
|
|
represented as an ISO-8601 encoded UTC date string.'; |
21
|
|
|
STR; |
22
|
|
|
|
23
|
3 |
|
public function serialize(mixed $value): string |
24
|
|
|
{ |
25
|
3 |
|
if (! $value instanceof \DateTimeInterface) { |
26
|
1 |
|
throw new InvariantViolation( |
27
|
1 |
|
'DateTime is not an instance of DateTimeInterface: ' . Utils::printSafe($value) |
28
|
1 |
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
return $value->format(\DateTimeInterface::ATOM); |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
public function parseValue(mixed $value): ?\DateTimeImmutable |
35
|
|
|
{ |
36
|
8 |
|
if (null === $value) { |
37
|
1 |
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
7 |
|
if ($value instanceof \DateTimeImmutable) { |
41
|
1 |
|
return $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
$str = (string) $value; |
45
|
|
|
|
46
|
6 |
|
$dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $str, new \DateTimeZone('UTC')) |
47
|
4 |
|
?: \DateTimeImmutable::createFromFormat('!Y-m-d\\TH:i:s', $str, new \DateTimeZone('UTC')) |
48
|
3 |
|
?: \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $str); |
49
|
|
|
|
50
|
6 |
|
$errors = \DateTimeImmutable::getLastErrors() ?: ['error_count' => 0, 'warnings' => []]; |
51
|
|
|
|
52
|
6 |
|
if ($errors['error_count'] > 0 || \count($errors['warnings'])) { |
53
|
3 |
|
throw new Error(\sprintf( |
54
|
3 |
|
'The DateTime value must be a string value in "%s" format; given: %s', |
55
|
3 |
|
\DateTimeInterface::ATOM, |
56
|
3 |
|
Utils::printSafeJson($value), |
57
|
3 |
|
)); |
58
|
|
|
} |
59
|
3 |
|
\assert($dateTime instanceof \DateTimeImmutable); |
60
|
3 |
|
return $dateTime; |
61
|
|
|
} |
62
|
|
|
|
63
|
7 |
|
public function parseLiteral(Node $valueNode, ?array $variables = null): \DateTimeImmutable |
64
|
|
|
{ |
65
|
7 |
|
if ($valueNode instanceof StringValueNode) { |
66
|
6 |
|
$dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $valueNode->value, new \DateTimeZone('UTC')) |
67
|
4 |
|
?: \DateTimeImmutable::createFromFormat('!Y-m-d\\TH:i:s', $valueNode->value, new \DateTimeZone('UTC')) |
68
|
3 |
|
?: \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $valueNode->value); |
69
|
|
|
|
70
|
6 |
|
$errors = \DateTimeImmutable::getLastErrors() ?: ['error_count' => 0, 'warnings' => []]; |
71
|
|
|
|
72
|
6 |
|
if ($errors['error_count'] > 0 || \count($errors['warnings'])) { |
73
|
3 |
|
throw new Error( |
74
|
3 |
|
\sprintf('Invalid DateTime value; given: %s', Printer::doPrint($valueNode)), |
75
|
3 |
|
$valueNode, |
|
|
|
|
76
|
3 |
|
); |
77
|
|
|
} |
78
|
3 |
|
\assert($dateTime instanceof \DateTimeImmutable); |
79
|
3 |
|
return $dateTime; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
throw new Error(\sprintf( |
83
|
1 |
|
'The DateTime value must be a string value in "%s" format; given: %s', |
84
|
1 |
|
\DateTimeInterface::ATOM, |
85
|
1 |
|
Printer::doPrint($valueNode), |
86
|
1 |
|
)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|