1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Hydration; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\EntityManager; |
6
|
|
|
use Bankiru\Api\Doctrine\Exception\HydrationException; |
7
|
|
|
use Bankiru\Api\Doctrine\Exception\MappingException; |
8
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
9
|
|
|
use Bankiru\Api\Doctrine\Mapping\EntityMetadata; |
10
|
|
|
use Doctrine\Common\Proxy\Proxy; |
11
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
13
|
|
|
|
14
|
|
|
final class EntityHydrator implements Hydrator |
15
|
|
|
{ |
16
|
|
|
/** @var EntityMetadata */ |
17
|
|
|
private $metadata; |
18
|
|
|
/** @var EntityManager */ |
19
|
|
|
private $manager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* EntityHydrator constructor. |
23
|
|
|
* |
24
|
|
|
* @param EntityManager $manager |
25
|
|
|
* @param ApiMetadata $metadata |
26
|
|
|
*/ |
27
|
15 |
|
public function __construct(EntityManager $manager, ApiMetadata $metadata) |
28
|
|
|
{ |
29
|
15 |
|
$this->manager = $manager; |
30
|
15 |
|
$this->metadata = $metadata; |
|
|
|
|
31
|
15 |
|
} |
32
|
|
|
|
33
|
|
|
/** {@inheritdoc} */ |
34
|
15 |
|
public function hydarate($source, $entity = null) |
35
|
|
|
{ |
36
|
15 |
|
if (null === $entity) { |
37
|
15 |
|
$entity = $this->metadata->getReflectionClass()->newInstance(); |
38
|
15 |
|
} |
39
|
|
|
|
40
|
15 |
|
$acessor = new PropertyAccessor(); |
41
|
15 |
|
foreach ($this->metadata->getFieldNames() as $fieldName) { |
42
|
15 |
|
$property = $this->metadata->getReflectionProperty($fieldName); |
43
|
|
|
|
44
|
15 |
|
$apiField = $this->metadata->getApiFieldName($fieldName); |
45
|
|
|
|
46
|
|
|
try { |
47
|
15 |
|
$value = $acessor->getValue($source, $apiField); |
48
|
15 |
|
} catch (NoSuchPropertyException $exception) { |
49
|
4 |
|
if (!$this->metadata->getFieldMapping($fieldName)['nullable']) { |
50
|
|
|
throw new HydrationException( |
51
|
|
|
sprintf( |
52
|
|
|
'Field %s for property %s does not present in dehydrated data', |
53
|
|
|
$apiField, |
54
|
|
|
$fieldName |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$property->setValue($entity, null); |
60
|
|
|
|
61
|
4 |
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$type = |
65
|
15 |
|
$this->manager->getConfiguration()->getTypeRegistry()->get($this->metadata->getTypeOfField($fieldName)); |
66
|
15 |
|
$value = $type->fromApiValue($value); |
67
|
|
|
|
68
|
15 |
|
$property->setValue($entity, $value); |
69
|
15 |
|
} |
70
|
|
|
|
71
|
15 |
|
foreach ($this->metadata->getAssociationNames() as $fieldName) { |
72
|
10 |
|
$value = $this->hydrateAssociation($fieldName, $entity, $source); |
73
|
10 |
|
$property = $this->metadata->getReflectionProperty($fieldName); |
74
|
10 |
|
$property->setValue($entity, $value); |
75
|
15 |
|
} |
76
|
|
|
|
77
|
15 |
|
return $entity; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $field |
82
|
|
|
* @param \StdClass $source |
83
|
|
|
* @param object $entity |
84
|
|
|
* |
85
|
|
|
* @return array|Proxy|object |
86
|
|
|
* @throws HydrationException |
87
|
|
|
* @throws MappingException |
88
|
|
|
*/ |
89
|
10 |
|
private function hydrateAssociation($field, $entity, $source) |
90
|
|
|
{ |
91
|
10 |
|
$accessor = new PropertyAccessor(); |
92
|
10 |
|
$targetClassName = $this->metadata->getAssociationTargetClass($field); |
93
|
10 |
|
$mapping = $this->metadata->getAssociationMapping($field); |
94
|
10 |
|
$targetPersister = $this->manager->getUnitOfWork()->getEntityPersister($targetClassName); |
95
|
10 |
|
$targetMetadata = $this->manager->getClassMetadata($mapping['target']); |
96
|
10 |
|
$apiField = $mapping['api_field']; |
97
|
10 |
|
$field = $mapping['field']; |
98
|
10 |
|
$oid = spl_object_hash($entity); |
99
|
|
|
|
100
|
10 |
|
if ($this->metadata->isSingleValuedAssociation($field)) { |
101
|
10 |
|
$identifiers = $this->metadata->getIdentifierValues($entity); |
102
|
10 |
|
if ($mapping['isOwningSide']) { |
103
|
|
|
try { |
104
|
10 |
|
$value = $accessor->getValue($source, $apiField); |
105
|
10 |
|
} catch (NoSuchPropertyException $exception) { |
106
|
8 |
|
if ($mapping['nullable']) { |
107
|
8 |
|
$this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, null); |
108
|
|
|
|
109
|
8 |
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new HydrationException( |
113
|
|
|
sprintf('Api field %s for property %s does not present in response', $apiField, $field) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
if ($targetMetadata->isIdentifierComposite()) { |
118
|
|
|
throw new HydrationException('Composite references not supported'); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
$targetIdsNames = $targetMetadata->getIdentifierFieldNames(); |
122
|
5 |
|
$targetIdName = array_shift($targetIdsNames); |
123
|
5 |
|
$type = $this->manager |
124
|
5 |
|
->getConfiguration() |
125
|
5 |
|
->getTypeRegistry() |
126
|
5 |
|
->get($targetMetadata->getTypeOfField($targetIdName)); |
127
|
|
|
|
128
|
5 |
|
$identifiers = [$targetIdName => $type->fromApiValue($value)]; |
129
|
5 |
|
} |
130
|
|
|
|
131
|
5 |
|
$newValue = $targetPersister->getToOneEntity($mapping, $entity, $identifiers); |
132
|
|
|
|
133
|
5 |
|
$this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, $newValue); |
134
|
|
|
|
135
|
5 |
|
return $newValue; |
136
|
|
|
} |
137
|
|
|
|
138
|
10 |
|
if ($this->metadata->isCollectionValuedAssociation($field)) { |
139
|
10 |
|
$newValue = $targetPersister->getOneToManyCollection($mapping, $entity); |
140
|
|
|
|
141
|
10 |
|
$this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, $newValue); |
142
|
|
|
|
143
|
10 |
|
return $newValue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
throw new MappingException('Invalid metadata association type'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.