|
1
|
|
|
<?php |
|
2
|
|
|
namespace Workana\AsyncJobs\Normalizer; |
|
3
|
|
|
|
|
4
|
|
|
use Assert\Assertion; |
|
5
|
|
|
use Bernard\Normalizer\AbstractAggregateNormalizerAware; |
|
6
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
7
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
8
|
|
|
|
|
9
|
|
|
use Workana\AsyncJobs\AsyncAction; |
|
10
|
|
|
use Workana\AsyncJobs\Parameter; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Normalizer for AsyncAction Job |
|
14
|
|
|
* |
|
15
|
|
|
* @author Carlos Frutos <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class AsyncActionNormalizer extends AbstractAggregateNormalizerAware implements NormalizerInterface, DenormalizerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use JobOptionsExtractor; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function normalize($object, $format = null, array $context = array()) |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
'actionClass' => get_class($object), |
|
28
|
|
|
'class' => $object->getClass(), |
|
29
|
|
|
'method' => $object->getMethod(), |
|
30
|
|
|
'parameters' => array_map(function($param) { |
|
31
|
|
|
return $this->aggregate->normalize($param); |
|
32
|
|
|
}, $object->getParameters()), |
|
33
|
|
|
'options' => $this->extractOptions($object), |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function supportsNormalization($data, $format = null) |
|
41
|
|
|
{ |
|
42
|
|
|
return ($data instanceof AsyncAction); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
49
|
|
|
{ |
|
50
|
|
|
Assertion::choicesNotEmpty($data, ['actionClass', 'class', 'method', 'options']); |
|
51
|
|
|
|
|
52
|
|
|
if (!is_a($data['actionClass'], AsyncAction::class, true)) { |
|
53
|
|
|
throw new InvalidArgumentException("Invalid action class: {$data['actionClass']}"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$action = Accesor::newInstanceWithoutConstructor($data['actionClass']); |
|
57
|
|
|
|
|
58
|
|
|
$denormalizedParams = array_map(function($paramData) { |
|
59
|
|
|
return $this->aggregate->denormalize($paramData, Parameter::class); |
|
60
|
|
|
}, $data['parameters']); |
|
61
|
|
|
|
|
62
|
|
|
Accesor::set($action, 'class', $data['class']); |
|
63
|
|
|
Accesor::set($action, 'method', $data['method']); |
|
64
|
|
|
Accesor::set($action, 'parameters', $denormalizedParams); |
|
65
|
|
|
|
|
66
|
|
|
$this->hydrateOptions($action, $data['options']); |
|
67
|
|
|
|
|
68
|
|
|
return $action; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
|
75
|
|
|
{ |
|
76
|
|
|
return is_a($type, AsyncAction::class, true); |
|
77
|
|
|
} |
|
78
|
|
|
} |