1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace ProjetNormandie\TwitchBundle\Serializer; |
||
6 | |||
7 | use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor; |
||
8 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
||
9 | use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
||
10 | use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
||
11 | use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
||
12 | use Symfony\Component\Serializer\SerializerInterface; |
||
13 | use Symfony\Component\Serializer\Serializer; |
||
14 | use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; |
||
15 | use Symfony\Component\Serializer\Encoder\JsonEncoder; |
||
16 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
||
17 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
||
18 | |||
19 | class SerializerRegistry |
||
20 | { |
||
21 | private static ?SerializerInterface $serializer = null; |
||
22 | |||
23 | public static function getSerializer(): SerializerInterface |
||
24 | { |
||
25 | if (self::$serializer === null) { |
||
26 | $phpDocExtractor = new PhpDocExtractor(); |
||
27 | $extractors = [ |
||
28 | new ConstructorExtractor([$phpDocExtractor]), |
||
29 | new ReflectionExtractor(), |
||
30 | $phpDocExtractor, |
||
31 | ]; |
||
32 | $propertyTypeExtractor = new PropertyInfoExtractor($extractors, $extractors); |
||
33 | |||
34 | self::$serializer = new Serializer( |
||
35 | [ |
||
36 | new DateTimeNormalizer(), |
||
37 | new JsonSerializableNormalizer(), |
||
38 | new ObjectNormalizer(null, null, null, $propertyTypeExtractor), |
||
39 | new ArrayDenormalizer(), |
||
40 | ], |
||
41 | [ |
||
42 | new JsonEncoder(), |
||
43 | ] |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | return self::$serializer; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
48 | } |
||
49 | } |
||
50 |