1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Normalizer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Assert\AssertionFailedException; |
7
|
|
|
use Bernard\Message\PlainMessage; |
8
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
9
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
10
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
11
|
|
|
|
12
|
|
|
final class PlainMessageNormalizer implements NormalizerInterface, DenormalizerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
1 |
|
public function normalize($object, $format = null, array $context = []) |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
1 |
|
'name' => $object->getName(), |
21
|
1 |
|
'arguments' => $object->all(), |
22
|
1 |
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
1 |
|
Assertion::notEmptyKey($data, 'name'); |
32
|
1 |
|
Assertion::keyExists($data, 'arguments'); |
33
|
1 |
|
Assertion::isArray($data['arguments']); |
34
|
1 |
|
} catch (AssertionFailedException $e) { |
35
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return new PlainMessage($data['name'], $data['arguments']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
45
|
|
|
{ |
46
|
|
|
return $type === PlainMessage::class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function supportsNormalization($data, $format = null) |
53
|
|
|
{ |
54
|
|
|
return $data instanceof PlainMessage; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|