|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Tebru\Gson\Internal\TypeAdapter; |
|
8
|
|
|
|
|
9
|
|
|
use DateTime; |
|
10
|
|
|
use DateTimeZone; |
|
11
|
|
|
use Tebru\Gson\JsonWritable; |
|
12
|
|
|
use Tebru\Gson\PhpType; |
|
13
|
|
|
use Tebru\Gson\JsonReadable; |
|
14
|
|
|
use Tebru\Gson\JsonToken; |
|
15
|
|
|
use Tebru\Gson\TypeAdapter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class DateTimeTypeAdapter |
|
19
|
|
|
* |
|
20
|
|
|
* @author Nate Brunette <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class DateTimeTypeAdapter extends TypeAdapter |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PhpType |
|
26
|
|
|
*/ |
|
27
|
|
|
private $phpType; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param PhpType $phpType |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function __construct(PhpType $phpType) |
|
35
|
|
|
{ |
|
36
|
8 |
|
$this->phpType = $phpType; |
|
37
|
8 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Read the next value, convert it to its type and return it |
|
41
|
|
|
* |
|
42
|
|
|
* @param JsonReadable $reader |
|
43
|
|
|
* @return DateTime|null |
|
|
|
|
|
|
44
|
|
|
* @throws \OutOfRangeException if the key doesn't exist |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function read(JsonReadable $reader): ?DateTime |
|
47
|
|
|
{ |
|
48
|
5 |
|
if ($reader->peek() === JsonToken::NULL) { |
|
49
|
1 |
|
return $reader->nextNull(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
$formattedDateTime = $reader->nextString(); |
|
53
|
4 |
|
$format = $this->phpType->getOptions()['format'] ?? null; |
|
54
|
4 |
|
$timezone = $this->phpType->getOptions()['timezone'] ?? null; |
|
55
|
|
|
|
|
56
|
4 |
|
if (null === $format) { |
|
57
|
1 |
|
$format = DateTime::ATOM; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
if (null !== $timezone) { |
|
61
|
2 |
|
$timezone = new DateTimeZone($timezone); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var DateTime $class */ |
|
65
|
4 |
|
$class = $this->phpType->getClass(); |
|
66
|
|
|
|
|
67
|
4 |
|
return $class::createFromFormat($format, $formattedDateTime, $timezone); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Write the value to the writer for the type |
|
72
|
|
|
* |
|
73
|
|
|
* @param JsonWritable $writer |
|
74
|
|
|
* @param DateTime $value |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function write(JsonWritable $writer, $value): void |
|
78
|
|
|
{ |
|
79
|
3 |
|
if (null === $value) { |
|
80
|
1 |
|
$writer->writeNull(); |
|
81
|
|
|
|
|
82
|
1 |
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
$format = $this->phpType->getOptions()['format'] ?? null; |
|
86
|
|
|
|
|
87
|
2 |
|
if (null === $format) { |
|
88
|
1 |
|
$format = DateTime::ATOM; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$dateTime = $value->format($format); |
|
92
|
2 |
|
$writer->writeString($dateTime); |
|
93
|
2 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.