|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Serializer\Context\DenormalizationContext; |
|
6
|
|
|
use Bdf\Serializer\Context\NormalizationContext; |
|
7
|
|
|
use Bdf\Serializer\Metadata\Driver\AnnotationsDriver; |
|
8
|
|
|
use Bdf\Serializer\Metadata\Driver\DriverInterface; |
|
9
|
|
|
use Bdf\Serializer\Metadata\Driver\StaticMethodDriver; |
|
10
|
|
|
use Bdf\Serializer\Metadata\MetadataFactory; |
|
11
|
|
|
use Bdf\Serializer\Normalizer\DateTimeNormalizer; |
|
12
|
|
|
use Bdf\Serializer\Normalizer\NormalizerInterface; |
|
13
|
|
|
use Bdf\Serializer\Normalizer\NormalizerLoader; |
|
14
|
|
|
use Bdf\Serializer\Normalizer\PropertyNormalizer; |
|
15
|
|
|
use Bdf\Serializer\Normalizer\TraversableNormalizer; |
|
16
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Builder for serializer. |
|
20
|
|
|
* |
|
21
|
|
|
* @author seb |
|
22
|
|
|
* |
|
23
|
|
|
* @psalm-consistent-constructor |
|
24
|
|
|
*/ |
|
25
|
|
|
class SerializerBuilder |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The cache for normalizer |
|
29
|
|
|
* |
|
30
|
|
|
* @var CacheInterface|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $cache; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The normalizers |
|
36
|
|
|
* |
|
37
|
|
|
* @var NormalizerInterface[] |
|
38
|
|
|
*/ |
|
39
|
|
|
private $normalizers = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The metadata drivers |
|
43
|
|
|
* |
|
44
|
|
|
* @var DriverInterface[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private $drivers = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Default options to use when denormalizing (i.e. convert serialized data to PHP data). |
|
50
|
|
|
* |
|
51
|
|
|
* @var array<string, mixed>|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private $defaultDenormalizationOptions; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Default options to use when normalizing (i.e. convert PHP data to serialized data). |
|
57
|
|
|
* |
|
58
|
|
|
* @var array<string, mixed>|null |
|
59
|
|
|
*/ |
|
60
|
|
|
private $defaultNormalizationOptions; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create a new builder |
|
64
|
|
|
* |
|
65
|
|
|
* @return self |
|
66
|
|
|
*/ |
|
67
|
198 |
|
public static function create() |
|
68
|
|
|
{ |
|
69
|
198 |
|
return new static(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the cache |
|
74
|
|
|
* |
|
75
|
|
|
* @param CacheInterface $cache The cache driver. |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setCache(CacheInterface $cache) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->cache = $cache; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set the normalizers |
|
88
|
|
|
* |
|
89
|
|
|
* @param NormalizerInterface[] $normalizers |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
26 |
|
public function setNormalizers(array $normalizers) |
|
94
|
|
|
{ |
|
95
|
26 |
|
$this->normalizers = $normalizers; |
|
96
|
|
|
|
|
97
|
26 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the metadata drivers |
|
102
|
|
|
* |
|
103
|
|
|
* @param DriverInterface[] $drivers |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setDrivers(array $drivers) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->drivers = $drivers; |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Configure default option to use when denormalizing (i.e. convert serialized data to PHP data). |
|
116
|
|
|
* |
|
117
|
|
|
* @param array<string, mixed>|null $defaultDenormalizationOptions |
|
118
|
|
|
* @return $this |
|
119
|
|
|
* |
|
120
|
|
|
* @see DenormalizationContext |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function setDefaultDenormalizationOptions(?array $defaultDenormalizationOptions) |
|
123
|
|
|
{ |
|
124
|
4 |
|
$this->defaultDenormalizationOptions = $defaultDenormalizationOptions; |
|
125
|
4 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Configure default option to use when normalizing (i.e. convert PHP data to serialized data). |
|
130
|
|
|
* |
|
131
|
|
|
* @param array<string, mixed>|null $defaultNormalizationOptions |
|
132
|
|
|
* @return $this |
|
133
|
|
|
* |
|
134
|
|
|
* @see NormalizationContext |
|
135
|
|
|
*/ |
|
136
|
4 |
|
public function setDefaultNormalizationOptions(?array $defaultNormalizationOptions) |
|
137
|
|
|
{ |
|
138
|
4 |
|
$this->defaultNormalizationOptions = $defaultNormalizationOptions; |
|
139
|
4 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Build the serializer |
|
144
|
|
|
* |
|
145
|
|
|
* @return Serializer |
|
146
|
|
|
*/ |
|
147
|
198 |
|
public function build() |
|
148
|
|
|
{ |
|
149
|
198 |
|
if (!$this->normalizers) { |
|
|
|
|
|
|
150
|
172 |
|
if (!$this->drivers) { |
|
|
|
|
|
|
151
|
172 |
|
$this->drivers = [ |
|
152
|
172 |
|
new StaticMethodDriver(), |
|
153
|
172 |
|
new AnnotationsDriver(), |
|
154
|
172 |
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
172 |
|
$this->normalizers = [ |
|
158
|
172 |
|
new DateTimeNormalizer(), |
|
159
|
172 |
|
new TraversableNormalizer(), |
|
160
|
172 |
|
new PropertyNormalizer(new MetadataFactory($this->drivers, $this->cache)) |
|
161
|
172 |
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
198 |
|
return new Serializer( |
|
165
|
198 |
|
new NormalizerLoader($this->normalizers), |
|
166
|
198 |
|
$this->defaultDenormalizationOptions, |
|
167
|
198 |
|
$this->defaultNormalizationOptions |
|
168
|
198 |
|
); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.