1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\Symfony; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Cruds\Exception\MapperException; |
6
|
|
|
use ScayTrase\Api\Cruds\PropertyMapperInterface; |
7
|
|
|
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; |
8
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
9
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
10
|
|
|
|
11
|
|
|
final class SymfonyPropertyMapper implements PropertyMapperInterface |
12
|
|
|
{ |
13
|
|
|
/** @var ClassMetadataFactoryInterface */ |
14
|
|
|
private $factory; |
15
|
|
|
/** @var NameConverterInterface */ |
16
|
|
|
private $strategy; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $className |
20
|
|
|
* @param string $apiProperty |
21
|
|
|
* |
22
|
|
|
* @return null|string |
23
|
|
|
* @throws MapperException |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function getObjectProperty($className, $apiProperty) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if ($this->factory->hasMetadataFor($className)) { |
28
|
|
|
return null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!in_array($apiProperty, $this->getApiProperties($className), true)) { |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->strategy->denormalize($apiProperty); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $className |
40
|
|
|
* @param string $objectProperty |
41
|
|
|
* |
42
|
|
|
* @return null|string |
43
|
|
|
* @throws MapperException |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function getApiProperty($className, $objectProperty) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if ($this->factory->hasMetadataFor($className)) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!in_array($objectProperty, $this->getObjectProperties($className), true)) { |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->strategy->normalize($objectProperty); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $className |
60
|
|
|
* |
61
|
|
|
* @return string[] |
62
|
|
|
* @throws MapperException |
63
|
|
|
*/ |
64
|
|
|
public function getApiProperties($className) |
65
|
|
|
{ |
66
|
|
|
$properties = []; |
67
|
|
|
foreach ($this->getMetadata($className) as $property) { |
68
|
|
|
$properties[] = $this->strategy->denormalize($property->getName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $properties; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $className |
76
|
|
|
* |
77
|
|
|
* @return string[] |
78
|
|
|
* @throws MapperException |
79
|
|
|
*/ |
80
|
|
|
public function getObjectProperties($className) |
81
|
|
|
{ |
82
|
|
|
$properties = []; |
83
|
|
|
foreach ($this->getMetadata($className) as $property) { |
84
|
|
|
$properties[] = $property->getName(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $properties; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $className |
92
|
|
|
* |
93
|
|
|
* @return AttributeMetadataInterface[] |
94
|
|
|
*/ |
95
|
|
|
private function getMetadata($className) |
96
|
|
|
{ |
97
|
|
|
return $this->factory->getMetadataFor($className)->getAttributesMetadata(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.