1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Accessor\AccessorInterface; |
8
|
|
|
use Chubbyphp\Deserialization\DeserializerRuntimeException; |
9
|
|
|
|
10
|
|
|
final class DateTimeFieldDenormalizer implements FieldDenormalizerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @deprecated |
14
|
|
|
* |
15
|
|
|
* @var FieldDenormalizerInterface |
16
|
|
|
*/ |
17
|
|
|
private $fieldDenormalizer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var AccessorInterface |
21
|
|
|
*/ |
22
|
|
|
private $accessor; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $emptyToNull; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DateTimeZone|null |
31
|
|
|
*/ |
32
|
|
|
private $dateTimeZone; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param AccessorInterface|FieldDenormalizerInterface $accessor |
36
|
|
|
*/ |
37
|
|
|
public function __construct($accessor, bool $emptyToNull = false, \DateTimeZone $dateTimeZone = null) |
38
|
|
|
{ |
39
|
17 |
|
$this->emptyToNull = $emptyToNull; |
40
|
|
|
$this->dateTimeZone = $dateTimeZone; |
41
|
17 |
|
|
42
|
17 |
|
if ($accessor instanceof AccessorInterface) { |
43
|
|
|
$this->accessor = $accessor; |
44
|
17 |
|
|
45
|
15 |
|
return; |
46
|
|
|
} |
47
|
15 |
|
|
48
|
|
|
if ($accessor instanceof FieldDenormalizerInterface) { |
|
|
|
|
49
|
|
|
@trigger_error( |
50
|
2 |
|
sprintf( |
51
|
1 |
|
'Use "%s" instead of "%s" as __construct argument', |
52
|
1 |
|
AccessorInterface::class, |
53
|
1 |
|
FieldDenormalizerInterface::class |
54
|
1 |
|
), |
55
|
1 |
|
E_USER_DEPRECATED |
56
|
|
|
); |
57
|
1 |
|
|
58
|
|
|
$this->fieldDenormalizer = $accessor; |
|
|
|
|
59
|
|
|
|
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
1 |
|
|
63
|
|
|
throw new \TypeError( |
64
|
|
|
sprintf( |
65
|
1 |
|
'%s::__construct() expects parameter 1 to be %s|%s, %s given', |
66
|
1 |
|
self::class, |
67
|
1 |
|
AccessorInterface::class, |
68
|
1 |
|
FieldDenormalizerInterface::class, |
69
|
1 |
|
is_object($accessor) ? get_class($accessor) : gettype($accessor) |
70
|
1 |
|
) |
71
|
1 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param object $object |
76
|
|
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @throws DeserializerRuntimeException |
79
|
|
|
*/ |
80
|
|
|
public function denormalizeField( |
81
|
|
|
string $path, |
82
|
|
|
$object, |
83
|
|
|
$value, |
84
|
|
|
DenormalizerContextInterface $context, |
85
|
16 |
|
DenormalizerInterface $denormalizer = null |
86
|
|
|
): void { |
87
|
|
|
if ('' === $value && $this->emptyToNull) { |
88
|
|
|
$this->setValue($path, $object, null, $context, $denormalizer); |
89
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
16 |
|
|
93
|
1 |
|
if (!is_string($value) || '' === $trimmedValue = trim($value)) { |
94
|
|
|
$this->setValue($path, $object, $value, $context, $denormalizer); |
95
|
1 |
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
15 |
|
|
99
|
7 |
|
try { |
100
|
|
|
$dateTime = new \DateTime($trimmedValue); |
101
|
7 |
|
|
102
|
|
|
if (null !== $this->dateTimeZone) { |
103
|
|
|
$dateTime->setTimezone($this->dateTimeZone); |
104
|
|
|
} |
105
|
8 |
|
|
106
|
|
|
$errors = \DateTime::getLastErrors(); |
107
|
5 |
|
|
108
|
1 |
|
if (0 === $errors['warning_count'] && 0 === $errors['error_count']) { |
109
|
|
|
$value = $dateTime; |
110
|
|
|
} |
111
|
5 |
|
} catch (\Exception $exception) { |
112
|
|
|
error_clear_last(); |
113
|
5 |
|
} |
114
|
5 |
|
|
115
|
|
|
$this->setValue($path, $object, $value, $context, $denormalizer); |
116
|
3 |
|
} |
117
|
3 |
|
|
118
|
|
|
/** |
119
|
|
|
* @param object $object |
120
|
8 |
|
* @param mixed $value |
121
|
8 |
|
*/ |
122
|
|
|
private function setValue( |
123
|
|
|
string $path, |
124
|
|
|
$object, |
125
|
|
|
$value, |
126
|
|
|
DenormalizerContextInterface $context, |
127
|
|
|
DenormalizerInterface $denormalizer = null |
128
|
|
|
): void { |
129
|
|
|
if (null !== $this->accessor) { |
130
|
16 |
|
$this->accessor->setValue($object, $value); |
131
|
|
|
|
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|