1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\DeserializationContext; |
6
|
|
|
use JMS\Serializer\SerializationContext; |
7
|
|
|
use JMS\Serializer\Serializer; |
8
|
|
|
use JMS\Serializer\SerializerInterface as JmsSerializer; |
9
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
10
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
11
|
|
|
|
12
|
|
|
final class JmsSerializerAdapter implements SerializerInterface, NormalizerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var JmsSerializer */ |
15
|
|
|
private $serializer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JmsSerializerAdapter constructor. |
19
|
|
|
* |
20
|
|
|
* @param JmsSerializer $serializer |
21
|
|
|
*/ |
22
|
2 |
|
public function __construct(JmsSerializer $serializer) |
23
|
|
|
{ |
24
|
2 |
|
$this->serializer = $serializer; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** {@inheritdoc} */ |
28
|
2 |
View Code Duplication |
public function serialize($data, $format, array $context = []) |
|
|
|
|
29
|
|
|
{ |
30
|
2 |
|
$jmsContext = SerializationContext::create(); |
31
|
2 |
|
if (array_key_exists('groups', $context)) { |
32
|
|
|
$jmsContext->setGroups($context['groups']); |
33
|
|
|
} |
34
|
2 |
|
$jmsContext->setSerializeNull(true); |
35
|
|
|
|
36
|
2 |
|
return $this->serializer->serialize($data, $format, $jmsContext); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** {@inheritdoc} */ |
40
|
|
View Code Duplication |
public function deserialize($data, $type, $format, array $context = []) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$jmsContext = DeserializationContext::create(); |
43
|
|
|
if (array_key_exists('groups', $context)) { |
44
|
|
|
$jmsContext->setGroups($context['groups']); |
45
|
|
|
} |
46
|
|
|
$jmsContext->setSerializeNull(true); |
47
|
|
|
|
48
|
|
|
$this->serializer->deserialize($data, $type, $format, $jmsContext); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** {@inheritdoc} */ |
52
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
53
|
|
|
{ |
54
|
2 |
|
$jmsContext = SerializationContext::create(); |
55
|
2 |
|
if (array_key_exists('groups', $context)) { |
56
|
|
|
$jmsContext->setGroups($context['groups']); |
57
|
|
|
} |
58
|
2 |
|
$jmsContext->setSerializeNull(true); |
59
|
|
|
|
60
|
2 |
|
if ($this->serializer instanceof Serializer) { |
61
|
2 |
|
return $this->serializer->toArray($object, $jmsContext); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return json_decode($this->serializer->serialize($object, 'json', $jmsContext), true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
68
|
|
|
public function supportsNormalization($data, $format = null) |
69
|
|
|
{ |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.