1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Serialization\Normalizer; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Accessor\AccessorInterface; |
8
|
|
|
|
9
|
|
|
final class DateTimeFieldNormalizer implements FieldNormalizerInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FieldNormalizerInterface |
13
|
|
|
*/ |
14
|
|
|
private $fieldNormalizer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var AccessorInterface |
18
|
|
|
*/ |
19
|
|
|
private $accessor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $format; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param AccessorInterface|FieldNormalizerInterface $fieldNormalizer |
|
|
|
|
28
|
|
|
* @param string $format |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct($accessor, string $format = 'c') |
31
|
|
|
{ |
32
|
|
|
|
33
|
6 |
|
$this->format = $format; |
34
|
|
|
|
35
|
6 |
|
if ($accessor instanceof AccessorInterface) { |
36
|
4 |
|
$this->accessor = $accessor; |
37
|
|
|
|
38
|
4 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
if ($accessor instanceof FieldNormalizerInterface) { |
42
|
1 |
|
@trigger_error( |
|
|
|
|
43
|
1 |
|
sprintf( |
44
|
1 |
|
'Use "%s" instead of "%s" as __construct argument', |
45
|
1 |
|
AccessorInterface::class, |
46
|
1 |
|
FieldNormalizerInterface::class |
47
|
|
|
), |
48
|
1 |
|
E_USER_DEPRECATED |
49
|
|
|
); |
50
|
|
|
|
51
|
1 |
|
$this->fieldNormalizer = $accessor; |
52
|
|
|
|
53
|
1 |
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
throw new \TypeError( |
57
|
1 |
|
sprintf( |
|
|
|
|
58
|
1 |
|
'%s::__construct() expects parameter 1 to be %s|%s, %s given', |
59
|
1 |
|
self::class, |
60
|
1 |
|
AccessorInterface::class, |
61
|
1 |
|
FieldNormalizerInterface::class, |
62
|
1 |
|
is_object($accessor) ? get_class($accessor) : gettype($accessor) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $path |
69
|
|
|
* @param object $object |
70
|
|
|
* @param NormalizerContextInterface $context |
71
|
|
|
* @param NormalizerInterface|null $normalizer |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
5 |
|
public function normalizeField( |
76
|
|
|
string $path, |
77
|
|
|
$object, |
78
|
|
|
NormalizerContextInterface $context, |
79
|
|
|
NormalizerInterface $normalizer = null |
80
|
|
|
) { |
81
|
5 |
|
$value = $this->getValue($path, $object, $context, $normalizer); |
82
|
|
|
|
83
|
5 |
|
if (is_string($value)) { |
84
|
|
|
try { |
85
|
2 |
|
$value = new \DateTime($value); |
86
|
1 |
|
} catch (\Exception $exception) { |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
if (!$value instanceof \DateTimeInterface) { |
91
|
2 |
|
return $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
return $value->format($this->format); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $path |
99
|
|
|
* @param $object |
100
|
|
|
* @param NormalizerContextInterface $context |
101
|
|
|
* @param NormalizerInterface|null $normalizer |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
5 |
|
private function getValue( |
105
|
|
|
string $path, |
106
|
|
|
$object, |
107
|
|
|
NormalizerContextInterface $context, |
108
|
|
|
NormalizerInterface $normalizer = null |
109
|
|
|
) { |
110
|
5 |
|
if (null !== $this->accessor) { |
111
|
4 |
|
return $this->accessor->getValue($object); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.