Completed
Push — master ( 77ccc9...77f195 )
by JHONATAN
03:13
created

ObjectNormalizer::supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Vox\Serializer;
4
5
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
6
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
7
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
11
class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface
12
{
13
    /**
14
     * @var Normalizer
15
     */
16
    private $normalizer;
17
    
18
    /**
19
     * @var Denormalizer
20
     */
21
    private $denormalizer;
22
    
23
    /**
24
     * @var NameConverterInterface
25
     */
26
    private $nameConverter;
27
    
28
    /**
29
     * @param Normalizer $normalizer
30
     * @param Denormalizer $denormalizer
31
     * @param ClassMetadataFactoryInterface $classMetadataFactory
32
     * @param NameConverterInterface $nameConverter
33
     */
34 2
    public function __construct(
35
        Normalizer $normalizer,
36
        Denormalizer $denormalizer,
37
        NameConverterInterface $nameConverter = null
38
    ) {
39 2
        $this->normalizer    = $normalizer;
40 2
        $this->denormalizer  = $denormalizer;
41 2
        $this->nameConverter = $nameConverter ?? new CamelCaseToSnakeCaseNameConverter();
42 2
    }
43
    
44 1
    public function denormalize($data, $class, $format = null, array $context = array())
45
    {
46 1
        $data = $this->changeKeys($data, false);
47
        
48 1
        return $this->denormalizer->denormalize($data, $class, $format, $context);
49
    }
50
51 1
    public function normalize($object, $format = null, array $context = array())
52
    {
53 1
        $data = $this->normalizer->normalize($object, $format, $context);
54
        
55 1
        return $this->changeKeys($data, true);
56
    }
57
    
58 2
    private function changeKeys(array $array, bool $direction): array
59
    {
60 2
        $changed = [];
61
62 2
        foreach ($array as $key => $value) {
63 2
            if (is_array($value)) {
64 2
                $value = $this->changeKeys($value, $direction);
65
            }
66
            
67 2
            if ($direction) {
68 1
                $newKey = $this->nameConverter->normalize($key);
69
            } else {
70 1
                $newKey = $this->nameConverter->denormalize($key);
71
            }
72
73 2
            $changed[$newKey] = $value;
74
        }
75
76 2
        return $changed;
77
    }
78
79
    public function supportsDenormalization($data, $type, $format = null): bool
80
    {
81
        return $this->denormalizer->supportsDenormalization($data, $type, $format);
82
    }
83
84
    public function supportsNormalization($data, $format = null): bool
85
    {
86
        return $this->normalizer->supportsNormalization($data, $format);
87
    }
88
}
89