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
|
|
|
* SymfonyPropertyMapper constructor. |
20
|
|
|
* |
21
|
|
|
* @param ClassMetadataFactoryInterface $factory |
22
|
|
|
* @param NameConverterInterface $strategy |
23
|
|
|
*/ |
24
|
2 |
|
public function __construct(ClassMetadataFactoryInterface $factory, NameConverterInterface $strategy) |
25
|
|
|
{ |
26
|
2 |
|
$this->factory = $factory; |
27
|
2 |
|
$this->strategy = $strategy; |
28
|
2 |
|
} |
29
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
31
|
1 |
View Code Duplication |
public function getObjectProperty($className, $apiProperty) |
|
|
|
|
32
|
|
|
{ |
33
|
1 |
|
if (!$this->factory->hasMetadataFor($className)) { |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
if (!in_array($apiProperty, $this->getApiProperties($className), true)) { |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $this->strategy->denormalize($apiProperty); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** {@inheritdoc} */ |
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
|
|
|
/** {@inheritdoc} */ |
59
|
1 |
|
public function getApiProperties($className) |
60
|
|
|
{ |
61
|
1 |
|
$properties = []; |
62
|
1 |
|
foreach ($this->getMetadata($className) as $property) { |
63
|
1 |
|
$properties[] = $this->strategy->denormalize($property->getName()); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
return $properties; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
1 |
|
public function getObjectProperties($className) |
71
|
|
|
{ |
72
|
1 |
|
$properties = []; |
73
|
|
|
foreach ($this->getMetadata($className) as $property) { |
74
|
|
|
$properties[] = $property->getName(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $properties; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $className |
82
|
|
|
* |
83
|
|
|
* @return AttributeMetadataInterface[] |
84
|
|
|
*/ |
85
|
1 |
|
private function getMetadata($className) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
1 |
|
return $this->factory->getMetadataFor($className)->getAttributesMetadata(); |
89
|
|
|
} catch (\InvalidArgumentException $exception) { |
90
|
|
|
throw MapperException::unsupportedClass($className); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.