|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lamoda\OmsClient\Impl\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
8
|
|
|
use Lamoda\OmsClient\Serializer\SerializerInterface; |
|
9
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
10
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
|
11
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; |
|
12
|
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
|
13
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
14
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
15
|
|
|
|
|
16
|
|
|
final class SymfonySerializerAdapterFactory |
|
17
|
|
|
{ |
|
18
|
|
|
public const SYMFONY_VERSION_LESS_THAN_42 = 'symfony_version_less_42'; |
|
19
|
|
|
public const SYMFONY_VERSION_GREATER_OR_EQUAL_42 = 'symfony_version_greater_or_equal_42'; |
|
20
|
|
|
|
|
21
|
1 |
|
public static function create(string $symfonyVersion = self::SYMFONY_VERSION_LESS_THAN_42): SerializerInterface |
|
22
|
|
|
{ |
|
23
|
1 |
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
|
24
|
|
|
|
|
25
|
1 |
|
$symfonySerializer = new Serializer([ |
|
26
|
1 |
|
self::createDateTimeNormalizer($symfonyVersion), |
|
27
|
1 |
|
new ObjectNormalizer($classMetadataFactory), |
|
28
|
|
|
], [ |
|
29
|
1 |
|
new JsonEncoder(), |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
1 |
|
return new SymfonySerializerAdapter($symfonySerializer); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private static function createDateTimeNormalizer(string $symfonyVersion): DateTimeNormalizer |
|
36
|
|
|
{ |
|
37
|
|
|
// @codeCoverageIgnoreStart |
|
38
|
|
|
switch ($symfonyVersion) { |
|
39
|
|
|
case self::SYMFONY_VERSION_LESS_THAN_42: |
|
40
|
|
|
return new DateTimeNormalizer('Y-m-d'); |
|
|
|
|
|
|
41
|
|
|
case self::SYMFONY_VERSION_GREATER_OR_EQUAL_42: |
|
42
|
|
|
return new DateTimeNormalizer([ |
|
43
|
|
|
'datetime_format' => 'Y-m-d' |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
48
|
|
|
'Given $symfonyVersion is invalid - "%s"', |
|
49
|
|
|
$symfonyVersion |
|
50
|
|
|
)); |
|
51
|
|
|
// @codeCoverageIgnoreEnd |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: