1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\Jms; |
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
|
|
|
/** {@inheritdoc} */ |
16
|
|
|
public function getObjectProperty($className, $apiProperty) |
17
|
|
|
{ |
18
|
|
|
$metadata = $this->getMetadata($className); |
19
|
|
|
|
20
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
21
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
22
|
|
|
if ($propertyMetadata->serializedName === $apiProperty) { |
23
|
|
|
return $propertyMetadata->reflection ? $propertyMetadata->reflection->getName() : null; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return null; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
31
|
|
|
public function getApiProperty($className, $objectProperty) |
32
|
|
|
{ |
33
|
|
|
$metadata = $this->getMetadata($className); |
34
|
|
|
|
35
|
|
|
if (!array_key_exists($objectProperty, $metadata->propertyMetadata)) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $metadata->propertyMetadata[$objectProperty]->serializedName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** {@inheritdoc} */ |
43
|
|
|
public function getApiProperties($className) |
44
|
|
|
{ |
45
|
|
|
$metadata = $this->getMetadata($className); |
46
|
|
|
|
47
|
|
|
$apiProperties = []; |
48
|
|
|
|
49
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
50
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
51
|
|
|
$apiProperties[] = $propertyMetadata->serializedName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $apiProperties; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritdoc} */ |
58
|
|
|
public function getObjectProperties($className) |
59
|
|
|
{ |
60
|
|
|
$metadata = $this->getMetadata($className); |
61
|
|
|
|
62
|
|
|
$objectProperties = []; |
63
|
|
|
|
64
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
65
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
66
|
|
|
if (null !== $propertyMetadata->reflection) { |
67
|
|
|
$objectProperties[] = $propertyMetadata->reflection->getName(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $objectProperties; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $className |
76
|
|
|
* |
77
|
|
|
* @return \Metadata\ClassHierarchyMetadata|\Metadata\MergeableClassMetadata|null |
78
|
|
|
* @throws MapperException |
79
|
|
|
*/ |
80
|
|
|
private function getMetadata($className) |
81
|
|
|
{ |
82
|
|
|
$metadata = $this->factory->getMetadataForClass($className); |
83
|
|
|
|
84
|
|
|
if (null === $metadata) { |
85
|
|
|
throw MapperException::unsupportedClass($className); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $metadata; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|