1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Chubbyphp\Serialization\Provider; |
||
6 | |||
7 | use Chubbyphp\Serialization\Encoder\Encoder; |
||
8 | use Chubbyphp\Serialization\Encoder\JsonTypeEncoder; |
||
9 | use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder; |
||
10 | use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder; |
||
11 | use Chubbyphp\Serialization\Encoder\XmlTypeEncoder; |
||
12 | use Chubbyphp\Serialization\Encoder\YamlTypeEncoder; |
||
13 | use Chubbyphp\Serialization\Normalizer\Normalizer; |
||
14 | use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry; |
||
15 | use Chubbyphp\Serialization\Serializer; |
||
16 | use Pimple\Container; |
||
17 | use Pimple\ServiceProviderInterface; |
||
18 | use Symfony\Component\Yaml\Yaml; |
||
19 | |||
20 | final class SerializationProvider implements ServiceProviderInterface |
||
21 | { |
||
22 | public function register(Container $container): void |
||
23 | { |
||
24 | $container['serializer'] = function () use ($container) { |
||
25 | return new Serializer($container['serializer.normalizer'], $container['serializer.encoder']); |
||
26 | }; |
||
27 | 1 | ||
28 | 1 | $container['serializer.normalizer'] = function () use ($container) { |
|
29 | return new Normalizer( |
||
30 | $container['serializer.normalizer.objectmappingregistry'], |
||
31 | 1 | $container['logger'] ?? null |
|
32 | 1 | ); |
|
33 | 1 | }; |
|
34 | 1 | ||
35 | $container['serializer.normalizer.objectmappingregistry'] = function () use ($container) { |
||
36 | return new NormalizerObjectMappingRegistry($container['serializer.normalizer.objectmappings']); |
||
37 | }; |
||
38 | 1 | ||
39 | 1 | $container['serializer.normalizer.objectmappings'] = function () { |
|
40 | return []; |
||
41 | }; |
||
42 | 1 | ||
43 | 1 | $container['serializer.encoder'] = function () use ($container) { |
|
44 | return new Encoder($container['serializer.encodertypes']); |
||
45 | }; |
||
46 | 1 | ||
47 | 1 | $container['serializer.encodertypes'] = function () { |
|
48 | $encoderTypes = []; |
||
49 | |||
50 | 1 | $encoderTypes[] = new JsonTypeEncoder(); |
|
51 | 1 | $encoderTypes[] = new JsonxTypeEncoder(); |
|
52 | $encoderTypes[] = new UrlEncodedTypeEncoder(); |
||
53 | 1 | $encoderTypes[] = new XmlTypeEncoder(); |
|
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
54 | 1 | ||
55 | 1 | if (class_exists(Yaml::class)) { |
|
56 | 1 | $encoderTypes[] = new YamlTypeEncoder(); |
|
57 | } |
||
58 | 1 | ||
59 | 1 | @trigger_error( |
|
60 | 'Register the encoder types by yourself:' |
||
61 | .' $container[\'serializer.encodertypes\'] = function () { return [new JsonTypeEncoder()]; };', |
||
62 | 1 | E_USER_DEPRECATED |
|
63 | ); |
||
64 | 1 | ||
65 | return $encoderTypes; |
||
66 | }; |
||
67 | } |
||
68 | } |
||
69 |