PropertyNameConverter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getSerializedName() 0 10 3
B getPropertyName() 0 14 5
1
<?php
2
3
namespace Happyr\SerializerBundle\PropertyManager;
4
5
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
6
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
7
8
/**
9
 * This class is aware of the meta data.
10
 *
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class PropertyNameConverter
14
{
15
    /**
16
     * @var NameConverterInterface
17
     */
18
    private $nameConverter;
19
20
    /**
21
     * @param NameConverterInterface $nameConverter
22
     */
23 23
    public function __construct(NameConverterInterface $nameConverter = null)
24
    {
25 23
        $this->nameConverter = $nameConverter;
26 23
        if ($this->nameConverter === null) {
27 23
            $this->nameConverter = new CamelCaseToSnakeCaseNameConverter();
28 23
        }
29 23
    }
30
31
    /**
32
     * Get the serialized name.
33
     *
34
     * @param array  $propertyMeta
35
     * @param string $propertyName
36
     *
37
     * @return string
38
     */
39 23
    public function getSerializedName(array $propertyMeta, $propertyName)
40
    {
41 23
        foreach ($propertyMeta as $metaName => $metaValue) {
42 22
            if ($metaName === 'serialized_name') {
43 5
                return $metaValue;
44
            }
45 23
        }
46
47 23
        return $this->nameConverter->normalize($propertyName);
48
    }
49
50
    /**
51
     * Get the property name form a serialized name and meta.
52
     *
53
     * @param array  $meta           root
54
     * @param string $serializedName
55
     *
56
     * @return string
57
     */
58 12
    public function getPropertyName($meta, $serializedName)
59
    {
60
        // Try find the name in the meta values
61 12
        foreach ($meta['property'] as $prop => $propertyMeta) {
62 12
            foreach ($propertyMeta as $metaName => $metaValue) {
63 12
                if ($metaName === 'serialized_name' && $metaValue === $serializedName) {
64 2
                    return $prop;
65
                }
66 12
            }
67 12
        }
68
69
        // Fallback on the name converter
70 12
        return $this->nameConverter->denormalize($serializedName);
71
    }
72
}
73