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