|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GpsLab component. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Peter Gribanov <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Payload\Serializer; |
|
12
|
|
|
|
|
13
|
|
|
use GpsLab\Component\Payload\Payload; |
|
14
|
|
|
use GpsLab\Component\Payload\Serializer\MessageTypeResolver\MessageTypeResolver; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
17
|
|
|
|
|
18
|
|
|
class TypedPayloadNormalizer implements NormalizerInterface, DenormalizerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var MessageTypeResolver |
|
22
|
|
|
*/ |
|
23
|
|
|
private $resolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param MessageTypeResolver $resolver |
|
27
|
|
|
*/ |
|
28
|
8 |
|
public function __construct(MessageTypeResolver $resolver) |
|
29
|
|
|
{ |
|
30
|
8 |
|
$this->resolver = $resolver; |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $data |
|
35
|
|
|
* @param string|null $format |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function supportsNormalization($data, $format = null) |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $data instanceof Payload; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Payload $object |
|
46
|
|
|
* @param string|null $format |
|
47
|
|
|
* @param array $context |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function normalize($object, $format = null, array $context = []) |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
1 |
|
'type' => $this->resolver->typeOfMessage($object), |
|
55
|
1 |
|
'payload' => $object->payload(), |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $data |
|
61
|
|
|
* @param string $class |
|
62
|
|
|
* @param string|null $format |
|
63
|
|
|
* @param array $context |
|
64
|
|
|
* |
|
65
|
|
|
* @return Payload |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
68
|
|
|
{ |
|
69
|
1 |
|
return new $class($data['payload']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param mixed $data |
|
74
|
|
|
* @param string $type |
|
75
|
|
|
* @param string|null $format |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
5 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return |
|
82
|
5 |
|
is_array($data) && |
|
83
|
5 |
|
isset($data['type'], $data['payload']) && |
|
84
|
5 |
|
is_array($data['payload']) && |
|
85
|
5 |
|
$this->resolver->isTypeOfMessage($data['type'], $type) |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|