1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Hydration; |
12
|
|
|
|
13
|
|
|
use ReflectionClass; |
14
|
|
|
use ReflectionProperty; |
15
|
|
|
use Doctrine\Common\Util\ClassUtils; |
16
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
17
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
18
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
19
|
|
|
use Addiks\RDMBundle\Hydration\EntityHydratorInterface; |
20
|
|
|
use Addiks\RDMBundle\DataLoader\DataLoaderInterface; |
21
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
22
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContext; |
23
|
|
|
use Webmozart\Assert\Assert; |
24
|
|
|
|
25
|
|
|
final class EntityHydrator implements EntityHydratorInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var MappingDriverInterface |
30
|
|
|
*/ |
31
|
|
|
private $mappingDriver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DataLoaderInterface |
35
|
|
|
*/ |
36
|
|
|
private $dbalDataLoader; |
37
|
|
|
|
38
|
5 |
|
public function __construct( |
39
|
|
|
MappingDriverInterface $mappingDriver, |
40
|
|
|
DataLoaderInterface $dbalDataLoader |
41
|
|
|
) { |
42
|
5 |
|
$this->mappingDriver = $mappingDriver; |
43
|
5 |
|
$this->dbalDataLoader = $dbalDataLoader; |
44
|
5 |
|
} |
45
|
|
|
|
46
|
3 |
View Code Duplication |
public function hydrateEntity($entity, EntityManagerInterface $entityManager): void |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
/** @var string $className */ |
49
|
3 |
|
$className = get_class($entity); |
50
|
|
|
|
51
|
3 |
|
if (class_exists(ClassUtils::class)) { |
52
|
3 |
|
$className = ClassUtils::getRealClass($className); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$classReflection = new ReflectionClass($className); |
56
|
|
|
|
57
|
|
|
/** @var ?EntityMappingInterface $mapping */ |
|
|
|
|
58
|
3 |
|
$mapping = $this->mappingDriver->loadRDMMetadataForClass($className); |
59
|
|
|
|
60
|
|
|
/** @var array<string> $dataFromAdditionalColumns */ |
61
|
3 |
|
$dataFromAdditionalColumns = array(); |
62
|
|
|
|
63
|
3 |
|
if ($mapping instanceof EntityMappingInterface) { |
64
|
3 |
|
if (!empty($mapping->collectDBALColumns())) { |
65
|
1 |
|
$dataFromAdditionalColumns = $this->dbalDataLoader->loadDBALDataForEntity( |
66
|
1 |
|
$entity, |
67
|
1 |
|
$entityManager |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
if ($mapping instanceof EntityMappingInterface) { |
72
|
3 |
|
$context = new HydrationContext($entity, $entityManager); |
73
|
|
|
|
74
|
3 |
|
foreach ($mapping->getFieldMappings() as $fieldName => $fieldMapping) { |
75
|
|
|
/** @var MappingInterface $fieldMapping */ |
76
|
|
|
|
77
|
|
|
/** @var mixed $value */ |
78
|
3 |
|
$value = $fieldMapping->resolveValue( |
79
|
3 |
|
$context, |
80
|
3 |
|
$dataFromAdditionalColumns |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
/** @var ReflectionClass $concreteClassReflection */ |
84
|
3 |
|
$concreteClassReflection = $classReflection; |
85
|
|
|
|
86
|
3 |
|
while (!$concreteClassReflection->hasProperty($fieldName)) { |
87
|
|
|
$concreteClassReflection = $concreteClassReflection->getParentClass(); |
88
|
|
|
|
89
|
|
|
Assert::notNull($concreteClassReflection, sprintf( |
90
|
|
|
"Property '%s' does not exist on object of class '%s'!", |
91
|
|
|
$fieldName, |
92
|
|
|
$className |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var ReflectionProperty $propertyReflection */ |
97
|
3 |
|
$propertyReflection = $concreteClassReflection->getProperty($fieldName); |
98
|
|
|
|
99
|
3 |
|
$propertyReflection->setAccessible(true); |
100
|
3 |
|
$propertyReflection->setValue($entity, $value); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
3 |
|
} |
105
|
|
|
|
106
|
3 |
View Code Duplication |
public function assertHydrationOnEntity($entity, EntityManagerInterface $entityManager): void |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
/** @var string $className */ |
109
|
3 |
|
$className = get_class($entity); |
110
|
|
|
|
111
|
3 |
|
if (class_exists(ClassUtils::class)) { |
112
|
3 |
|
$className = ClassUtils::getRealClass($className); |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
$classReflection = new ReflectionClass($className); |
116
|
|
|
|
117
|
|
|
/** @var ?EntityMappingInterface $mapping */ |
|
|
|
|
118
|
3 |
|
$mapping = $this->mappingDriver->loadRDMMetadataForClass($className); |
119
|
|
|
|
120
|
|
|
/** @var array<string> $dataFromAdditionalColumns */ |
121
|
3 |
|
$dataFromAdditionalColumns = array(); |
122
|
|
|
|
123
|
3 |
|
if ($mapping instanceof EntityMappingInterface) { |
124
|
3 |
|
if (!empty($mapping->collectDBALColumns())) { |
125
|
1 |
|
$dataFromAdditionalColumns = $this->dbalDataLoader->loadDBALDataForEntity( |
126
|
1 |
|
$entity, |
127
|
1 |
|
$entityManager |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
if ($mapping instanceof EntityMappingInterface) { |
132
|
3 |
|
$context = new HydrationContext($entity, $entityManager); |
133
|
|
|
|
134
|
3 |
|
foreach ($mapping->getFieldMappings() as $fieldName => $fieldMapping) { |
135
|
|
|
/** @var MappingInterface $fieldMapping */ |
136
|
|
|
|
137
|
|
|
/** @var ReflectionClass $concreteClassReflection */ |
138
|
3 |
|
$concreteClassReflection = $classReflection; |
139
|
|
|
|
140
|
3 |
|
while (!$concreteClassReflection->hasProperty($fieldName)) { |
141
|
|
|
$concreteClassReflection = $concreteClassReflection->getParentClass(); |
142
|
|
|
|
143
|
|
|
Assert::notNull($concreteClassReflection, sprintf( |
144
|
|
|
"Property '%s' does not exist on object of class '%s'!", |
145
|
|
|
$fieldName, |
146
|
|
|
$className |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @var ReflectionProperty $propertyReflection */ |
151
|
3 |
|
$propertyReflection = $concreteClassReflection->getProperty($fieldName); |
152
|
|
|
|
153
|
3 |
|
$propertyReflection->setAccessible(true); |
154
|
|
|
|
155
|
|
|
/** @var object $actualValue */ |
156
|
3 |
|
$actualValue = $propertyReflection->getValue($entity); |
157
|
|
|
|
158
|
3 |
|
$fieldMapping->assertValue( |
159
|
3 |
|
$context, |
160
|
3 |
|
$dataFromAdditionalColumns, |
161
|
3 |
|
$actualValue |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
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.