-
Notifications
You must be signed in to change notification settings - Fork 1
/
Factory.php
198 lines (177 loc) · 7.66 KB
/
Factory.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Serializer;
use LastDragon_ru\LaraASP\Core\Application\ContainerResolver;
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializer as SerializerContract;
use LastDragon_ru\LaraASP\Serializer\Normalizers\DateTimeNormalizer;
use LastDragon_ru\LaraASP\Serializer\Normalizers\DateTimeNormalizerContextBuilder;
use LastDragon_ru\LaraASP\Serializer\Normalizers\SerializableNormalizer;
use LastDragon_ru\LaraASP\Serializer\Normalizers\SerializableNormalizerContextBuilder;
use LastDragon_ru\LaraASP\Serializer\Normalizers\UnitEnumNormalizer;
use LastDragon_ru\LaraASP\Serializer\Normalizers\UnitEnumNormalizerContextBuilder;
use Symfony\Component\Serializer\Context\Encoder\JsonEncoderContextBuilder;
use Symfony\Component\Serializer\Context\Normalizer\BackedEnumNormalizerContextBuilder;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
use function array_filter;
use function array_key_exists;
use function array_keys;
use const JSON_BIGINT_AS_STRING;
use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_LINE_TERMINATORS;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
class Factory {
public function __construct(
protected readonly ContainerResolver $container,
protected readonly PackageConfig $config,
) {
// empty
}
/**
* @param array<class-string<EncoderInterface|DecoderInterface>, array<string, mixed>> $encoders
* @param array<class-string<NormalizerInterface|DenormalizerInterface>, array<string, mixed>|null> $normalizers
* The `null` value can be used to remove the built-in normalizer.
* @param array<string, mixed> $context
*/
public function create(
array $encoders = [],
array $normalizers = [],
array $context = [],
?string $format = null,
): SerializerContract {
$config = $this->config->getInstance();
$format ??= $config->default;
$context = $context + $config->context;
$encoders = $this->getEncoders($encoders, $context);
$normalizers = $this->getNormalizers($normalizers, $context);
$serializer = $this->make($encoders, $normalizers, $context, $format);
return $serializer;
}
/**
* @private for tests only
*
* @param list<class-string<EncoderInterface|DecoderInterface>> $encoders
* @param list<class-string<NormalizerInterface|DenormalizerInterface>> $normalizers
* @param array<string, mixed> $context
*/
protected function make(
array $encoders,
array $normalizers,
array $context,
string $format,
): SerializerContract {
$container = $this->container->getInstance();
$encoderInstances = [];
$normalizerInstances = [];
foreach ($encoders as $class) {
$encoderInstances[] = $container->make($class);
}
foreach ($normalizers as $class) {
$normalizerInstances[] = $container->make($class);
}
return new Serializer(
new SymfonySerializer($normalizerInstances, $encoderInstances),
$format,
$context,
);
}
/**
* @param array<class-string<EncoderInterface|DecoderInterface>, array<string, mixed>> $encoders
* @param array<string, mixed> $context
*
* @return list<class-string<EncoderInterface|DecoderInterface>>
*/
protected function getEncoders(array $encoders, array &$context): array {
$groups = [$encoders, $this->config->getInstance()->encoders, $this->getDefaultEncoders()];
$list = [];
foreach ($groups as $group) {
foreach ($group as $encoder => $options) {
if (!isset($list[$encoder])) {
$list[$encoder] = true;
}
$context += $options;
}
}
return array_keys($list);
}
/**
* @return array<class-string<EncoderInterface|DecoderInterface>, array<string, mixed>>
*/
protected function getDefaultEncoders(): array {
return [
JsonEncoder::class => (new JsonEncoderContextBuilder())
->withEncodeOptions(
JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_LINE_TERMINATORS
| JSON_BIGINT_AS_STRING
| JSON_PRESERVE_ZERO_FRACTION
| JSON_THROW_ON_ERROR,
)
->withDecodeOptions(
JSON_THROW_ON_ERROR,
)
->toArray(),
];
}
/**
* @param array<class-string<NormalizerInterface|DenormalizerInterface>, array<string, mixed>|null> $normalizers
* @param array<string, mixed> $context
*
* @return list<class-string<NormalizerInterface|DenormalizerInterface>>
*/
protected function getNormalizers(array $normalizers, array &$context): array {
$groups = [$normalizers, $this->config->getInstance()->normalizers, $this->getDefaultNormalizers()];
$list = [];
foreach ($groups as $group) {
foreach ($group as $normalizer => $options) {
if (!array_key_exists($normalizer, $list)) {
$list[$normalizer] = true;
}
if ($options === null) {
$list[$normalizer] = false;
} elseif ($list[$normalizer]) {
$context += $options;
} else {
// ignore
}
}
}
return array_keys(array_filter($list));
}
/**
* @return array<class-string<NormalizerInterface|DenormalizerInterface>, array<string, mixed>>
*/
protected function getDefaultNormalizers(): array {
return [
ArrayDenormalizer::class => [],
DateTimeNormalizer::class => (new DateTimeNormalizerContextBuilder())
->withFormat(DateTimeNormalizer::ContextFormatDefault)
->toArray(),
DateTimeZoneNormalizer::class => [],
DateIntervalNormalizer::class => [],
UnitEnumNormalizer::class => (new UnitEnumNormalizerContextBuilder())
->withAllowInvalidValues(false)
->toArray(),
BackedEnumNormalizer::class => (new BackedEnumNormalizerContextBuilder())
->withAllowInvalidValues(false)
->toArray(),
SerializableNormalizer::class => (new SerializableNormalizerContextBuilder())
->withDisableTypeEnforcement(false)
->withSkipNullValues(false)
->withSkipUninitializedValues(true)
->withPreserveEmptyObjects(true)
->withAllowExtraAttributes(false)
->toArray(),
];
}
}