PropertyNamer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A serializedName() 0 14 4
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\Naming;
10
11
use Tebru\AnnotationReader\AnnotationCollection;
12
use Tebru\Gson\Annotation\SerializedName;
13
use Tebru\Gson\Annotation\VirtualProperty;
14
use Tebru\Gson\PropertyNamingStrategy;
15
16
/**
17
 * Class PropertyNamer
18
 *
19
 * Gets the property name from annotation or naming strategy
20
 *
21
 * @author Nate Brunette <[email protected]>
22
 */
23
final class PropertyNamer
24
{
25
    /**
26
     * @var PropertyNamingStrategy
27
     */
28
    private $propertyNamingStrategy;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param PropertyNamingStrategy $propertyNamingStrategy
34
     */
35 12
    public function __construct(PropertyNamingStrategy $propertyNamingStrategy)
36
    {
37 12
        $this->propertyNamingStrategy = $propertyNamingStrategy;
38 12
    }
39
40
    /**
41
     * Get the serialized version of the property name
42
     *
43
     * @param string $propertyName
44
     * @param AnnotationCollection $annotations
45
     * @return string
46
     */
47 6
    public function serializedName(string $propertyName, AnnotationCollection $annotations): string
48
    {
49 6
        $serializedName = $annotations->get(SerializedName::class);
50 6
        if (null !== $serializedName) {
51 2
            return $serializedName->getValue();
52
        }
53
54
        /** @var VirtualProperty $virtualProperty */
55 4
        $virtualProperty = $annotations->get(VirtualProperty::class);
56 4
        if ($virtualProperty !== null && $virtualProperty->getSerializedName() !== null) {
57 1
            return $virtualProperty->getSerializedName();
58
        }
59
60 3
        return $this->propertyNamingStrategy->translateName($propertyName);
61
    }
62
}
63