1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
6
|
|
|
use JMS\Serializer\Naming\PropertyNamingStrategyInterface; |
7
|
|
|
use Metadata\MetadataFactoryInterface; |
8
|
|
|
use ScayTrase\Api\Cruds\Exception\MapperException; |
9
|
|
|
use ScayTrase\Api\Cruds\PropertyMapperInterface; |
10
|
|
|
|
11
|
|
|
final class JmsPropertyMapper implements PropertyMapperInterface |
12
|
|
|
{ |
13
|
|
|
/** @var MetadataFactoryInterface */ |
14
|
|
|
private $factory; |
15
|
|
|
/** @var PropertyNamingStrategyInterface */ |
16
|
|
|
private $strategy; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* JmsPropertyMapper constructor. |
20
|
|
|
* |
21
|
|
|
* @param MetadataFactoryInterface $factory |
22
|
|
|
* @param PropertyNamingStrategyInterface $strategy |
23
|
|
|
*/ |
24
|
2 |
|
public function __construct(MetadataFactoryInterface $factory, PropertyNamingStrategyInterface $strategy) |
25
|
|
|
{ |
26
|
2 |
|
$this->factory = $factory; |
27
|
2 |
|
$this->strategy = $strategy; |
28
|
2 |
|
} |
29
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
31
|
2 |
|
public function getEntityProperty($className, $apiProperty) |
32
|
|
|
{ |
33
|
2 |
|
$metadata = $this->getMetadata($className); |
34
|
|
|
|
35
|
2 |
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
36
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
37
|
2 |
|
if ($this->getPropertyName($propertyMetadata) === $apiProperty) { |
38
|
2 |
|
return $propertyMetadata->reflection ? $propertyMetadata->reflection->getName() : null; |
39
|
|
|
} |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** {@inheritdoc} */ |
46
|
|
|
public function getApiProperty($className, $objectProperty) |
47
|
|
|
{ |
48
|
|
|
$metadata = $this->getMetadata($className); |
49
|
|
|
|
50
|
|
|
if (!array_key_exists($objectProperty, $metadata->propertyMetadata)) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $metadata->propertyMetadata[$objectProperty]->serializedName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritdoc} */ |
58
|
|
|
public function getApiProperties($className) |
59
|
|
|
{ |
60
|
|
|
$metadata = $this->getMetadata($className); |
61
|
|
|
|
62
|
|
|
$apiProperties = []; |
63
|
|
|
|
64
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
65
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
66
|
|
|
$apiProperties[] = $this->getPropertyName($propertyMetadata); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $apiProperties; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** {@inheritdoc} */ |
73
|
|
|
public function getEntityProperties($className) |
74
|
|
|
{ |
75
|
|
|
$metadata = $this->getMetadata($className); |
76
|
|
|
|
77
|
|
|
$objectProperties = []; |
78
|
|
|
|
79
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
80
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
81
|
|
|
if (null !== $propertyMetadata->reflection) { |
82
|
|
|
$objectProperties[] = $propertyMetadata->reflection->getName(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $objectProperties; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $className |
91
|
|
|
* |
92
|
|
|
* @return \Metadata\ClassHierarchyMetadata|\Metadata\MergeableClassMetadata|null |
93
|
|
|
* @throws MapperException |
94
|
|
|
*/ |
95
|
2 |
|
private function getMetadata($className) |
96
|
|
|
{ |
97
|
2 |
|
$metadata = $this->factory->getMetadataForClass($className); |
98
|
|
|
|
99
|
2 |
|
if (null === $metadata) { |
100
|
|
|
throw MapperException::unsupportedClass($className); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $metadata; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param PropertyMetadata $propertyMetadata |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
private function getPropertyName(PropertyMetadata $propertyMetadata) |
112
|
|
|
{ |
113
|
2 |
|
return $this->strategy->translateName($propertyMetadata); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|