|
1
|
|
|
<?php |
|
2
|
|
|
namespace Workana\AsyncJobs\Normalizer; |
|
3
|
|
|
|
|
4
|
|
|
use Bernard\Normalizer\AbstractAggregateNormalizerAware; |
|
5
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
6
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
7
|
|
|
use Workana\AsyncJobs\Parameter; |
|
8
|
|
|
use Workana\AsyncJobs\SerializableEvent; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Carlos Frutos <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class SerializableEventNormalizer extends AbstractAggregateNormalizerAware implements NormalizerInterface, DenormalizerInterface |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritDoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
public function normalize($object, $format = null, array $context = array()) |
|
19
|
|
|
{ |
|
20
|
|
|
$parameters = Accesor::get($object, 'parameters'); |
|
21
|
|
|
|
|
22
|
|
|
return [ |
|
23
|
|
|
'class' => get_class($object), |
|
24
|
|
|
'parameters' => array_map(function($param) { |
|
25
|
|
|
return $this->aggregate->normalize($param); |
|
26
|
|
|
}, $parameters), |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function supportsNormalization($data, $format = null) |
|
34
|
|
|
{ |
|
35
|
|
|
return ($data instanceof SerializableEvent); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritDoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
$denormalizedParams = []; |
|
44
|
|
|
foreach ($data['parameters'] as $name => $paramData) { |
|
45
|
|
|
$denormalizedParams[$name] = $this->aggregate->denormalize($paramData, Parameter::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$event = Accesor::newInstanceWithoutConstructor($data['class']); |
|
49
|
|
|
|
|
50
|
|
|
Accesor::set($event, 'parameters', $denormalizedParams); |
|
51
|
|
|
|
|
52
|
|
|
return $event; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
|
59
|
|
|
{ |
|
60
|
|
|
return is_a($type, SerializableEvent::class, true); |
|
61
|
|
|
} |
|
62
|
|
|
} |