1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Normalizer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Assert\AssertionFailedException; |
7
|
|
|
use Bernard\Envelope; |
8
|
|
|
use Normalt\Normalizer\AggregateNormalizer; |
9
|
|
|
use Normalt\Normalizer\AggregateNormalizerAware; |
10
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
11
|
|
|
use Symfony\Component\Serializer\Exception\RuntimeException; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
14
|
|
|
|
15
|
|
|
final class EnvelopeNormalizer implements NormalizerInterface, DenormalizerInterface, AggregateNormalizerAware |
16
|
|
|
{ |
17
|
|
|
private $aggregate; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param AggregateNormalizer $aggregate |
21
|
|
|
*/ |
22
|
2 |
|
public function setAggregateNormalizer(AggregateNormalizer $aggregate) |
23
|
|
|
{ |
24
|
2 |
|
$this->aggregate = $aggregate; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
1 |
|
public function normalize($object, $format = null, array $context = []) |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
1 |
|
'class' => $object->getClass(), |
34
|
1 |
|
'timestamp' => $object->getTimestamp(), |
35
|
1 |
|
'message' => $this->aggregate->normalize($object->getMessage()), |
36
|
1 |
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
1 |
|
Assertion::choicesNotEmpty($data, ['message', 'class', 'timestamp']); |
46
|
1 |
|
Assertion::classExists($data['class']); |
47
|
1 |
|
} catch (AssertionFailedException $e) { |
48
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$envelope = new Envelope($this->aggregate->denormalize($data['message'], $data['class'])); |
52
|
|
|
|
53
|
1 |
|
$this->forcePropertyValue($envelope, 'class', $data['class']); |
54
|
1 |
|
$this->forcePropertyValue($envelope, 'timestamp', $data['timestamp']); |
55
|
|
|
|
56
|
1 |
|
return $envelope; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
63
|
|
|
{ |
64
|
|
|
return $type === Envelope::class; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function supportsNormalization($data, $format = null) |
71
|
|
|
{ |
72
|
|
|
return $data instanceof Envelope; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Envelope $envelope |
77
|
|
|
* @param string $property |
78
|
|
|
* @param mixed $value |
79
|
|
|
*/ |
80
|
1 |
|
private function forcePropertyValue(Envelope $envelope, $property, $value) |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
1 |
|
$property = new \ReflectionProperty($envelope, $property); |
84
|
1 |
|
} catch (\ReflectionException $e) { |
85
|
|
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$property->setAccessible(true); |
89
|
1 |
|
$property->setValue($envelope, $value); |
90
|
1 |
|
} |
91
|
|
|
} |
92
|
|
|
|