|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lamoda\IsmpClient\Impl\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse\Body; |
|
8
|
|
|
use Symfony\Component\Serializer\Exception\BadMethodCallException; |
|
9
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
|
10
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
11
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
|
12
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
13
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
14
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class FacadeDocBodyResponseBodyDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private const ORIGINAL_STRING_FIELD_KEY = 'original_string'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var SerializerInterface|DenormalizerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $denormalizer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
29
|
|
|
{ |
|
30
|
4 |
|
if (null === $this->denormalizer) { |
|
31
|
1 |
|
throw new BadMethodCallException('Please set a serializer before calling denormalize()!'); |
|
32
|
|
|
} |
|
33
|
3 |
|
if (!\is_array($data)) { |
|
34
|
1 |
|
throw new InvalidArgumentException('Data expected to be an array, ' . \gettype($data) . ' given.'); |
|
35
|
|
|
} |
|
36
|
2 |
|
if (Body::class !== $class) { |
|
37
|
1 |
|
throw new InvalidArgumentException('Unsupported class: ' . $class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
$data[self::ORIGINAL_STRING_FIELD_KEY] = $this->denormalizer->serialize($data, $format, $context); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
1 |
|
return $this->denormalizer->denormalize($data, $class, $format, $context); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
|
49
|
|
|
{ |
|
50
|
5 |
|
return Body::class === $type |
|
51
|
5 |
|
&& is_array($data) |
|
52
|
5 |
|
&& !array_key_exists(self::ORIGINAL_STRING_FIELD_KEY, $data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
7 |
|
public function setDenormalizer(DenormalizerInterface $denormalizer): void |
|
59
|
|
|
{ |
|
60
|
7 |
|
$this->denormalizer = $denormalizer; |
|
61
|
7 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function hasCacheableSupportsMethod(): bool |
|
67
|
|
|
{ |
|
68
|
3 |
|
return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: