|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vox\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
6
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
7
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
9
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
|
10
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* this normalizer has the primary goal to aggregate the name converter functionality to the normalization |
|
14
|
|
|
* its not optmized for performance though |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jhonatan Teixeira <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Normalizer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $normalizer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Denormalizer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $denormalizer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var NameConverterInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $nameConverter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var SerializerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $serializer; |
|
39
|
|
|
|
|
40
|
34 |
|
public function __construct( |
|
41
|
|
|
Normalizer $normalizer, |
|
42
|
|
|
Denormalizer $denormalizer, |
|
43
|
|
|
NameConverterInterface $nameConverter = null |
|
44
|
|
|
) { |
|
45
|
34 |
|
$this->normalizer = $normalizer; |
|
46
|
34 |
|
$this->denormalizer = $denormalizer; |
|
47
|
34 |
|
$this->nameConverter = $nameConverter ?? new CamelCaseToSnakeCaseNameConverter(); |
|
48
|
34 |
|
} |
|
49
|
|
|
|
|
50
|
21 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
51
|
|
|
{ |
|
52
|
21 |
|
$data = $this->changeKeys($data, false); |
|
53
|
|
|
|
|
54
|
21 |
|
return $this->denormalizer->denormalize($data, $class, $format, $context); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
10 |
|
public function normalize($object, $format = null, array $context = array()) |
|
58
|
|
|
{ |
|
59
|
10 |
|
$data = $this->normalizer->normalize($object, $format, $context); |
|
60
|
10 |
|
$this->normalizer->clear(); |
|
61
|
|
|
|
|
62
|
10 |
|
return $this->changeKeys($data, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
24 |
|
private function changeKeys(array $array, bool $direction): array |
|
66
|
|
|
{ |
|
67
|
24 |
|
$changed = []; |
|
68
|
|
|
|
|
69
|
24 |
|
foreach ($array as $key => $value) { |
|
70
|
24 |
|
if (is_array($value)) { |
|
71
|
9 |
|
$value = $this->changeKeys($value, $direction); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
24 |
|
if ($direction) { |
|
75
|
10 |
|
$newKey = $this->nameConverter->normalize($key); |
|
76
|
|
|
} else { |
|
77
|
21 |
|
$newKey = $this->nameConverter->denormalize($key); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
24 |
|
$changed[$newKey] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
24 |
|
return $changed; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
20 |
|
public function supportsDenormalization($data, $type, $format = null): bool |
|
87
|
|
|
{ |
|
88
|
20 |
|
return $this->denormalizer->supportsDenormalization($data, $type, $format); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
10 |
|
public function supportsNormalization($data, $format = null): bool |
|
92
|
|
|
{ |
|
93
|
10 |
|
return $this->normalizer->supportsNormalization($data, $format); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
34 |
|
public function setSerializer(SerializerInterface $serializer) |
|
97
|
|
|
{ |
|
98
|
34 |
|
$this->serializer = $serializer; |
|
99
|
34 |
|
$this->normalizer->setNormalizer($serializer); |
|
100
|
34 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|