1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CCT\Component\Rest\Serializer; |
4
|
|
|
|
5
|
|
|
use CCT\Component\Rest\Config; |
6
|
|
|
use CCT\Component\Rest\Serializer\Mapping\Loader\DirectoryLoader; |
7
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
8
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
9
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
10
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain; |
11
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizableInterface; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
14
|
|
|
use Symfony\Component\Serializer\Serializer; |
15
|
|
|
|
16
|
|
|
class SymfonySerializerBuilder implements SerializerBuilderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Config |
20
|
|
|
*/ |
21
|
|
|
private $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array|EncoderInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $encoders; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array|NormalizableInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $normalizers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SymfonySerializerBuilder constructor. |
35
|
|
|
* |
36
|
|
|
* @param Config $config |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct(Config $config) |
39
|
|
|
{ |
40
|
1 |
|
$this->config = $config; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create instance by static method |
45
|
|
|
* |
46
|
|
|
* @param Config $config |
47
|
|
|
* |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
1 |
|
public static function createByConfig(Config $config) |
51
|
|
|
{ |
52
|
1 |
|
return new static($config); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set default configuration |
57
|
|
|
* |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
* @throws \Symfony\Component\Serializer\Exception\RuntimeException |
60
|
|
|
* @throws \Symfony\Component\Serializer\Exception\MappingException |
61
|
|
|
*/ |
62
|
1 |
|
public function configureDefaults() |
63
|
|
|
{ |
64
|
1 |
|
$this->encoders = array(new JsonEncoder()); |
65
|
|
|
|
66
|
1 |
|
$loaders = $this->generateLoaders(); |
67
|
|
|
|
68
|
1 |
|
$classMetadataFactory = new ClassMetadataFactory(new LoaderChain($loaders)); |
69
|
|
|
|
70
|
1 |
|
$normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); |
71
|
1 |
|
$this->normalizers = array($normalizer); |
72
|
|
|
|
73
|
1 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generates loaders from directory config |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
* |
81
|
|
|
* @throws \Symfony\Component\Serializer\Exception\MappingException |
82
|
|
|
* @throws \InvalidArgumentException |
83
|
|
|
*/ |
84
|
1 |
|
protected function generateLoaders(): array |
85
|
|
|
{ |
86
|
1 |
|
$loadersCollection = []; |
87
|
1 |
|
$directoryLoader = new DirectoryLoader(); |
88
|
|
|
|
89
|
1 |
|
$metadataDirs = $this->config->get(Config::METADATA_DIRS, []); |
90
|
1 |
|
foreach ($metadataDirs as $metadataDir) { |
91
|
1 |
|
$loaders = $directoryLoader->load($metadataDir['dir']); |
92
|
1 |
|
if (empty($loaders)) { |
93
|
1 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
array_push($loadersCollection, ...$loaders); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $loadersCollection; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Builds the Symfony Serializer object. |
104
|
|
|
* |
105
|
|
|
* @return SerializerInterface |
106
|
|
|
*/ |
107
|
1 |
|
public function build(): SerializerInterface |
108
|
|
|
{ |
109
|
1 |
|
$serializer = new Serializer($this->normalizers, $this->encoders); |
110
|
|
|
|
111
|
1 |
|
return new SymfonySerializerAdapter($serializer); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array|EncoderInterface[] |
116
|
|
|
*/ |
117
|
|
|
public function getEncoders(): array |
118
|
|
|
{ |
119
|
|
|
return $this->encoders; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $encoders |
124
|
|
|
*/ |
125
|
|
|
public function setEncoders(array $encoders): void |
126
|
|
|
{ |
127
|
|
|
$this->encoders = $encoders; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array|NormalizableInterface[] |
132
|
|
|
*/ |
133
|
|
|
public function getNormalizers(): array |
134
|
|
|
{ |
135
|
|
|
return $this->normalizers; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $normalizers |
140
|
|
|
*/ |
141
|
|
|
public function setNormalizers(array $normalizers): void |
142
|
|
|
{ |
143
|
|
|
$this->normalizers = $normalizers; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add directory for metadata config |
148
|
|
|
* |
149
|
|
|
* @param $dir |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
* @throws \InvalidArgumentException |
153
|
|
|
*/ |
154
|
|
|
public function addMetadataDir($dir): self |
155
|
|
|
{ |
156
|
|
|
if (!is_dir($dir)) { |
157
|
|
|
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|