|
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
|
|
|
|