1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Serializer; |
4
|
|
|
|
5
|
|
|
use Bdf\Serializer\Metadata\Driver\AnnotationsDriver; |
6
|
|
|
use Bdf\Serializer\Metadata\Driver\DriverInterface; |
7
|
|
|
use Bdf\Serializer\Metadata\Driver\StaticMethodDriver; |
8
|
|
|
use Bdf\Serializer\Metadata\MetadataFactory; |
9
|
|
|
use Bdf\Serializer\Normalizer\DateTimeNormalizer; |
10
|
|
|
use Bdf\Serializer\Normalizer\NormalizerInterface; |
11
|
|
|
use Bdf\Serializer\Normalizer\NormalizerLoader; |
12
|
|
|
use Bdf\Serializer\Normalizer\PropertyNormalizer; |
13
|
|
|
use Bdf\Serializer\Normalizer\TraversableNormalizer; |
14
|
|
|
use Psr\SimpleCache\CacheInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Builder for serializer. |
18
|
|
|
* |
19
|
|
|
* @author seb |
20
|
|
|
* |
21
|
|
|
* @psalm-consistent-constructor |
22
|
|
|
*/ |
23
|
|
|
class SerializerBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The cache for normalizer |
27
|
|
|
* |
28
|
|
|
* @var CacheInterface|null |
29
|
|
|
*/ |
30
|
|
|
private $cache; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The normalizers |
34
|
|
|
* |
35
|
|
|
* @var NormalizerInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $normalizers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The metadata drivers |
41
|
|
|
* |
42
|
|
|
* @var DriverInterface[] |
43
|
|
|
*/ |
44
|
|
|
private $drivers = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new builder |
48
|
|
|
* |
49
|
|
|
* @return self |
50
|
|
|
*/ |
51
|
190 |
|
public static function create() |
52
|
|
|
{ |
53
|
190 |
|
return new static(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the cache |
58
|
|
|
* |
59
|
|
|
* @param CacheInterface $cache The cache driver. |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setCache(CacheInterface $cache) |
64
|
|
|
{ |
65
|
|
|
$this->cache = $cache; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the normalizers |
72
|
|
|
* |
73
|
|
|
* @param NormalizerInterface[] $normalizers |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
26 |
|
public function setNormalizers(array $normalizers) |
78
|
|
|
{ |
79
|
26 |
|
$this->normalizers = $normalizers; |
80
|
|
|
|
81
|
26 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the metadata drivers |
86
|
|
|
* |
87
|
|
|
* @param DriverInterface[] $drivers |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setDrivers(array $drivers) |
92
|
|
|
{ |
93
|
|
|
$this->drivers = $drivers; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Build the serializer |
100
|
|
|
* |
101
|
|
|
* @return Serializer |
102
|
|
|
*/ |
103
|
190 |
|
public function build() |
104
|
|
|
{ |
105
|
190 |
|
if (!$this->normalizers) { |
|
|
|
|
106
|
164 |
|
if (!$this->drivers) { |
|
|
|
|
107
|
164 |
|
$this->drivers = [ |
108
|
164 |
|
new StaticMethodDriver(), |
109
|
164 |
|
new AnnotationsDriver(), |
110
|
164 |
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
164 |
|
$this->normalizers = [ |
114
|
164 |
|
new DateTimeNormalizer(), |
115
|
164 |
|
new TraversableNormalizer(), |
116
|
164 |
|
new PropertyNormalizer(new MetadataFactory($this->drivers, $this->cache)) |
117
|
164 |
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
190 |
|
return new Serializer(new NormalizerLoader($this->normalizers)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.