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 $accessor |
28
|
|
|
*/ |
29
|
|
|
public function __construct($accessor, string $format = 'c') |
30
|
6 |
|
{ |
31
|
|
|
$this->format = $format; |
32
|
6 |
|
|
33
|
|
|
if ($accessor instanceof AccessorInterface) { |
34
|
6 |
|
$this->accessor = $accessor; |
35
|
4 |
|
|
36
|
|
|
return; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
if ($accessor instanceof FieldNormalizerInterface) { |
|
|
|
|
40
|
2 |
|
@trigger_error( |
41
|
1 |
|
sprintf( |
42
|
1 |
|
'Use "%s" instead of "%s" as __construct argument', |
43
|
1 |
|
AccessorInterface::class, |
44
|
1 |
|
FieldNormalizerInterface::class |
45
|
1 |
|
), |
46
|
|
|
E_USER_DEPRECATED |
47
|
1 |
|
); |
48
|
|
|
|
49
|
|
|
$this->fieldNormalizer = $accessor; |
50
|
1 |
|
|
51
|
|
|
return; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
throw new \TypeError( |
55
|
1 |
|
sprintf( |
56
|
1 |
|
'%s::__construct() expects parameter 1 to be %s|%s, %s given', |
57
|
1 |
|
self::class, |
58
|
1 |
|
AccessorInterface::class, |
59
|
1 |
|
FieldNormalizerInterface::class, |
60
|
1 |
|
is_object($accessor) ? get_class($accessor) : gettype($accessor) |
61
|
1 |
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param object $object |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function normalizeField( |
71
|
|
|
string $path, |
72
|
|
|
$object, |
73
|
|
|
NormalizerContextInterface $context, |
74
|
5 |
|
NormalizerInterface $normalizer = null |
75
|
|
|
) { |
76
|
|
|
$value = $this->getValue($path, $object, $context, $normalizer); |
77
|
|
|
|
78
|
|
|
if (is_string($value)) { |
79
|
|
|
try { |
80
|
5 |
|
$value = new \DateTime($value); |
81
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
82
|
5 |
|
} |
83
|
|
|
} |
84
|
4 |
|
|
85
|
1 |
|
if (!$value instanceof \DateTimeInterface) { |
86
|
|
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
return $value->format($this->format); |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
3 |
|
* @param object $object |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function getValue( |
98
|
|
|
string $path, |
99
|
|
|
$object, |
100
|
|
|
NormalizerContextInterface $context, |
101
|
|
|
NormalizerInterface $normalizer = null |
102
|
|
|
) { |
103
|
|
|
if (null !== $this->accessor) { |
104
|
5 |
|
return $this->accessor->getValue($object); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|