PropertyNamer::serializedName()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 10
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