/
Serializer.php
150 lines (139 loc) · 4.45 KB
/
Serializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types = 1);
namespace OpenEuropa\EPoetry\Serializer;
use Doctrine\Common\Annotations\AnnotationReader;
use OpenEuropa\EPoetry\Serializer\Normalizer\ContactsDenormalizer;
use OpenEuropa\EPoetry\Serializer\Normalizer\DateTimeNormalizer;
use OpenEuropa\EPoetry\Serializer\Normalizer\ObjectNormalizer;
use OpenEuropa\EPoetry\Serializer\Normalizer\LinguisticSectionsDenormalizer;
use OpenEuropa\EPoetry\Serializer\Normalizer\ProductsDenormalizer;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
/**
* Class Serializer.
*
* This class has basic definitions for serializers.
* By default you can serialize request from XML or YAML files.
*/
class Serializer implements SerializerInterface
{
/**
* {@inheritdoc}
*/
public function deserialize($data, $type, $format, array $context = []): object|array
{
return $this->getSerializer()->deserialize($data, $type, $format, $context);
}
/**
* {@inheritdoc}
*/
public function serialize($data, $format, array $context = []): string
{
return $this->getSerializer()->serialize($data, $format, $context);
}
/**
* Convert a PHP array into an object.
*
* @param array $input
* The PHP array
* @param string $type
* The object type
*
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*
* @return object
* The new object
*/
public function fromArray(array $input, string $type): object
{
return $this->getSerializer()->denormalize($input, $type);
}
/**
* Convert an object into PHP array.
*
* @param object $data
*/
public function toArray(object $data): array
{
return $this->getSerializer()->normalize($data);
}
/**
* Convert a string into an object.
*
* @param string $data
* The input string
* @param string $type
* The object type
* @param string $format
* The string format (yml, json, xml...)
*
* @return object
* The new object
*/
public function fromString(string $data, string $type, string $format): object
{
return $this->deserialize($data, $type, $format);
}
/**
* Convert object to the string of specific format.
*
* @param object $data
* The object type
* @param string $format
* Format of the string
*
* @return string
* Result string
*/
public function toString(object $data, string $format): string
{
return $this->serialize($data, $format);
}
/**
* Serializer constructor.
*
* @param \Symfony\Component\Serializer\Encoder\EncoderInterface[] $encoders
*
* @return \Symfony\Component\Serializer\Serializer
*/
protected function getSerializer(array $encoders = []): SymfonySerializer
{
if ($encoders === []) {
$encoders = [
new XmlEncoder(),
new JsonEncoder(),
new YamlEncoder(),
];
}
// Setup serializer service.
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$context = [
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true, // Allow to set integer values from strings.
AbstractObjectNormalizer::SKIP_NULL_VALUES => true, // Null values won't be generated.
];
return new SymfonySerializer([
new DateTimeNormalizer(),
new ArrayDenormalizer(),
new ProductsDenormalizer(),
new LinguisticSectionsDenormalizer(),
new ContactsDenormalizer(),
new ObjectNormalizer(
$classMetadataFactory,
null,
null,
new PhpDocExtractor(),
null,
null,
$context
)
], $encoders);
}
}