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
|
|
|
|
8
|
|
|
use Workana\AsyncJobs\Parameter; |
9
|
|
|
|
10
|
|
|
class ParameterNormalizer extends AbstractAggregateNormalizerAware implements NormalizerInterface, DenormalizerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Normalizes an object into a set of arrays/scalars. |
14
|
|
|
* |
15
|
|
|
* @param object $object object to normalize |
16
|
|
|
* @param string $format format the normalization result will be encoded as |
17
|
|
|
* @param array $context Context options for the normalizer |
18
|
|
|
* |
19
|
|
|
* @return array|scalar |
20
|
|
|
*/ |
21
|
|
|
public function normalize($object, $format = null, array $context = array()) |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'type' => $object->getType(), |
25
|
|
|
'value' => $this->aggregate->normalize($object->getValue()), |
26
|
|
|
'name' => $object->getName(), |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Checks whether the given class is supported for normalization by this normalizer. |
32
|
|
|
* |
33
|
|
|
* @param mixed $data Data to normalize |
34
|
|
|
* @param string $format The format being (de-)serialized from or into |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function supportsNormalization($data, $format = null) |
39
|
|
|
{ |
40
|
|
|
return ($data instanceof Parameter); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Denormalizes data back into an object of the given class. |
45
|
|
|
* |
46
|
|
|
* @param mixed $data data to restore |
47
|
|
|
* @param string $class the expected class to instantiate |
48
|
|
|
* @param string $format format the given data was extracted from |
49
|
|
|
* @param array $context options available to the denormalizer |
50
|
|
|
* |
51
|
|
|
* @return object |
52
|
|
|
*/ |
53
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
54
|
|
|
{ |
55
|
|
|
$value = $this->aggregate->denormalize($data['value'], $data['type']); |
56
|
|
|
|
57
|
|
|
return new Parameter($value, $data['name']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks whether the given class is supported for denormalization by this normalizer. |
62
|
|
|
* |
63
|
|
|
* @param mixed $data Data to denormalize from |
64
|
|
|
* @param string $type The class to which the data should be denormalized |
65
|
|
|
* @param string $format The format being deserialized from |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
70
|
|
|
{ |
71
|
|
|
return is_a($type, Parameter::class, true); |
72
|
|
|
} |
73
|
|
|
} |