Completed
Push — master ( 28afa9...69e2c3 )
by JHONATAN
02:50
created

ObjectNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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 14
    public function __construct(
41
        Normalizer $normalizer,
42
        Denormalizer $denormalizer,
43
        NameConverterInterface $nameConverter = null
44
    ) {
45 14
        $this->normalizer    = $normalizer;
46 14
        $this->denormalizer  = $denormalizer;
47 14
        $this->nameConverter = $nameConverter ?? new CamelCaseToSnakeCaseNameConverter();
48 14
    }
49
    
50 11
    public function denormalize($data, $class, $format = null, array $context = array())
51
    {
52 11
        $data = $this->changeKeys($data, false);
53
        
54 11
        return $this->denormalizer->denormalize($data, $class, $format, $context);
55
    }
56
57 2
    public function normalize($object, $format = null, array $context = array())
58
    {
59 2
        $data = $this->normalizer->normalize($object, $format, $context);
60
        
61 2
        return $this->changeKeys($data, true);
62
    }
63
    
64 12
    private function changeKeys(array $array, bool $direction): array
65
    {
66 12
        $changed = [];
67
68 12
        foreach ($array as $key => $value) {
69 12
            if (is_array($value)) {
70 2
                $value = $this->changeKeys($value, $direction);
71
            }
72
            
73 12
            if ($direction) {
74 2
                $newKey = $this->nameConverter->normalize($key);
75
            } else {
76 11
                $newKey = $this->nameConverter->denormalize($key);
77
            }
78
79 12
            $changed[$newKey] = $value;
80
        }
81
82 12
        return $changed;
83
    }
84
85 10
    public function supportsDenormalization($data, $type, $format = null): bool
86
    {
87 10
        return $this->denormalizer->supportsDenormalization($data, $type, $format);
88
    }
89
90 2
    public function supportsNormalization($data, $format = null): bool
91
    {
92 2
        return $this->normalizer->supportsNormalization($data, $format);
93
    }
94
95 14
    public function setSerializer(SerializerInterface $serializer)
96
    {
97 14
        $this->serializer = $serializer;
98 14
        $this->normalizer->setNormalizer($serializer);
99 14
    }
100
}
101