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
|
|
|
* @param AccessorInterface|FieldDenormalizerInterface $accessor |
26
|
|
|
*/ |
27
|
14 |
|
public function __construct($accessor) |
28
|
|
|
{ |
29
|
14 |
|
if ($accessor instanceof AccessorInterface) { |
30
|
12 |
|
$this->accessor = $accessor; |
31
|
|
|
|
32
|
12 |
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
if ($accessor instanceof FieldDenormalizerInterface) { |
36
|
1 |
|
@trigger_error( |
|
|
|
|
37
|
1 |
|
sprintf( |
38
|
1 |
|
'Use "%s" instead of "%s" as __construct argument', |
39
|
1 |
|
AccessorInterface::class, |
40
|
1 |
|
FieldDenormalizerInterface::class |
41
|
|
|
), |
42
|
1 |
|
E_USER_DEPRECATED |
43
|
|
|
); |
44
|
|
|
|
45
|
1 |
|
$this->fieldDenormalizer = $accessor; |
|
|
|
|
46
|
|
|
|
47
|
1 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
throw new \TypeError( |
51
|
1 |
|
sprintf( |
|
|
|
|
52
|
1 |
|
'%s::__construct() expects parameter 1 to be %s|%s, %s given', |
53
|
1 |
|
self::class, |
54
|
1 |
|
AccessorInterface::class, |
55
|
1 |
|
FieldDenormalizerInterface::class, |
56
|
1 |
|
is_object($accessor) ? get_class($accessor) : gettype($accessor) |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $path |
63
|
|
|
* @param object $object |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @param DenormalizerContextInterface $context |
66
|
|
|
* @param DenormalizerInterface|null $denormalizer |
67
|
|
|
* |
68
|
|
|
* @throws DeserializerRuntimeException |
69
|
|
|
*/ |
70
|
13 |
|
public function denormalizeField( |
71
|
|
|
string $path, |
72
|
|
|
$object, |
73
|
|
|
$value, |
74
|
|
|
DenormalizerContextInterface $context, |
75
|
|
|
DenormalizerInterface $denormalizer = null |
76
|
|
|
) { |
77
|
13 |
|
if (!is_string($value) || '' === $trimmedValue = trim($value)) { |
78
|
6 |
|
$this->setValue($path, $object, $value, $context, $denormalizer); |
79
|
|
|
|
80
|
6 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
try { |
84
|
7 |
|
$dateTime = new \DateTime($trimmedValue); |
85
|
4 |
|
$errors = \DateTime::getLastErrors(); |
86
|
|
|
|
87
|
4 |
|
if (0 === $errors['warning_count'] && 0 === $errors['error_count']) { |
88
|
4 |
|
$value = $dateTime; |
89
|
|
|
} |
90
|
3 |
|
} catch (\Exception $exception) { |
91
|
3 |
|
error_clear_last(); |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
$this->setValue($path, $object, $value, $context, $denormalizer); |
95
|
7 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $path |
99
|
|
|
* @param object $object |
100
|
|
|
* @param mixed $value |
101
|
|
|
* @param DenormalizerContextInterface $context |
102
|
|
|
* @param DenormalizerInterface|null $denormalizer |
103
|
|
|
*/ |
104
|
13 |
|
private function setValue( |
105
|
|
|
string $path, |
106
|
|
|
$object, |
107
|
|
|
$value, |
108
|
|
|
DenormalizerContextInterface $context, |
109
|
|
|
DenormalizerInterface $denormalizer = null |
110
|
|
|
) { |
111
|
13 |
|
if (null !== $this->accessor) { |
112
|
12 |
|
$this->accessor->setValue($object, $value); |
113
|
|
|
|
114
|
12 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer); |
|
|
|
|
118
|
1 |
|
} |
119
|
|
|
} |
120
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: