1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Serializer; |
6
|
|
|
use JMS\Serializer\SerializerInterface as JmsSerializer; |
7
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
8
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
9
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
10
|
|
|
|
11
|
|
|
final class JmsSerializerAdapter implements SerializerInterface, NormalizerInterface, DenormalizerInterface |
12
|
|
|
{ |
13
|
|
|
/** @var JmsSerializer */ |
14
|
|
|
private $serializer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* JmsSerializerAdapter constructor. |
18
|
|
|
* |
19
|
|
|
* @param JmsSerializer $serializer |
20
|
|
|
*/ |
21
|
6 |
|
public function __construct(JmsSerializer $serializer) |
22
|
|
|
{ |
23
|
6 |
|
$this->serializer = $serializer; |
24
|
6 |
|
} |
25
|
|
|
|
26
|
|
|
/** {@inheritdoc} */ |
27
|
6 |
|
public function serialize($data, $format, array $context = []) |
28
|
|
|
{ |
29
|
6 |
|
return $this->serializer->serialize($data, $format, JmsContextFactory::serialization($context)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** {@inheritdoc} */ |
33
|
|
|
public function deserialize($data, $type, $format, array $context = []) |
34
|
|
|
{ |
35
|
|
|
$this->serializer->deserialize($data, $type, $format, JmsContextFactory::deserialization($context)); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** {@inheritdoc} */ |
39
|
5 |
|
public function normalize($object, $format = null, array $context = []) |
40
|
|
|
{ |
41
|
5 |
|
$jmsContext = JmsContextFactory::serialization($context); |
42
|
|
|
|
43
|
5 |
|
if ($this->serializer instanceof Serializer) { |
44
|
5 |
|
return $this->serializer->toArray($object, $jmsContext); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return json_decode($this->serializer->serialize($object, 'json', $jmsContext), true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** {@inheritdoc} */ |
51
|
|
|
public function supportsNormalization($data, $format = null) |
52
|
|
|
{ |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** {@inheritdoc} */ |
57
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
58
|
|
|
{ |
59
|
|
|
$jmsContext = JmsContextFactory::deserialization($context); |
60
|
|
|
|
61
|
|
|
if ($this->serializer instanceof Serializer) { |
62
|
|
|
return $this->serializer->fromArray($data, $class, $jmsContext); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->serializer->deserialize(json_encode($data), 'json', $jmsContext); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** {@inheritdoc} */ |
69
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
70
|
|
|
{ |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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: