1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation\Hydrator; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use GeneratedHydrator\Configuration; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
9
|
|
|
use ApiClients\Foundation\Resource\AbstractResource; |
10
|
|
|
use Zend\Hydrator\HydratorInterface; |
11
|
|
|
|
12
|
|
|
class Hydrator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $options; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $hydrators = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $annotations = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HandlerInterface[] |
31
|
|
|
*/ |
32
|
|
|
protected $annotationHandlers = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AnnotationReader |
36
|
|
|
*/ |
37
|
|
|
protected $annotationReader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $options |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct(array $options) |
43
|
|
|
{ |
44
|
3 |
|
$this->options = $options; |
45
|
3 |
|
$this->annotationReader = new AnnotationReader(); |
46
|
|
|
|
47
|
3 |
|
$this->setUpAnnotations(); |
48
|
3 |
|
$this->addSelfToExtraProperties(); |
49
|
3 |
|
} |
50
|
|
|
|
51
|
3 |
|
protected function setUpAnnotations() |
52
|
|
|
{ |
53
|
3 |
|
if (!isset($this->options[Options::ANNOTATIONS])) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
foreach ($this->options[Options::ANNOTATIONS] as $annotationClass => $handler) { |
58
|
3 |
|
$this->annotationHandlers[$annotationClass] = new $handler($this); |
59
|
|
|
} |
60
|
3 |
|
} |
61
|
|
|
|
62
|
3 |
|
protected function addSelfToExtraProperties() |
63
|
|
|
{ |
64
|
3 |
|
$this->options[Options::EXTRA_PROPERTIES]['hydrator'] = $this; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $class |
69
|
|
|
* @param array $json |
70
|
|
|
* @return ResourceInterface |
71
|
|
|
*/ |
72
|
3 |
View Code Duplication |
public function hydrate(string $class, array $json): ResourceInterface |
|
|
|
|
73
|
|
|
{ |
74
|
3 |
|
$fullClassName = implode( |
75
|
3 |
|
'\\', |
76
|
|
|
[ |
77
|
|
|
$this->options[Options::NAMESPACE], |
78
|
3 |
|
$this->options[Options::NAMESPACE_SUFFIX], |
79
|
3 |
|
$class, |
80
|
|
|
] |
81
|
|
|
); |
82
|
3 |
|
return $this->hydrateFQCN($fullClassName, $json); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $class |
87
|
|
|
* @param array $json |
88
|
|
|
* @return ResourceInterface |
89
|
|
|
*/ |
90
|
3 |
|
public function hydrateFQCN(string $class, array $json): ResourceInterface |
91
|
|
|
{ |
92
|
3 |
|
$hydrator = $this->getHydrator($class); |
93
|
3 |
|
$object = new $class(); |
94
|
3 |
|
$json = $this->hydrateApplyAnnotations($json, $object); |
95
|
3 |
|
$resource = $hydrator->hydrate($json, $object); |
96
|
3 |
|
if ($resource instanceof AbstractResource) { |
97
|
|
|
$resource->setExtraProperties($this->options[Options::EXTRA_PROPERTIES]); |
98
|
|
|
} |
99
|
3 |
|
return $resource; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $json |
104
|
|
|
* @param ResourceInterface $object |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
3 |
View Code Duplication |
protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
|
|
|
|
108
|
|
|
{ |
109
|
3 |
|
foreach ($this->annotationHandlers as $annotationClass => $handler) { |
110
|
3 |
|
$annotation = $this->getAnnotation($object, $annotationClass); |
111
|
3 |
|
if ($annotation === null) { |
112
|
3 |
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
$json = $handler->hydrate($annotation, $json, $object); |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
return $json; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $class |
123
|
|
|
* @param ResourceInterface $object |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
2 |
View Code Duplication |
public function extract(string $class, ResourceInterface $object): array |
|
|
|
|
127
|
|
|
{ |
128
|
2 |
|
$fullClassName = implode( |
129
|
2 |
|
'\\', |
130
|
|
|
[ |
131
|
|
|
$this->options[Options::NAMESPACE], |
132
|
2 |
|
$this->options[Options::NAMESPACE_SUFFIX], |
133
|
2 |
|
$class, |
134
|
|
|
] |
135
|
|
|
); |
136
|
2 |
|
return $this->extractFQCN($fullClassName, $object); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Takes a fully qualified class name and extracts the data for that class from the given $object |
141
|
|
|
* @param string $class |
142
|
|
|
* @param ResourceInterface $object |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
2 |
|
public function extractFQCN(string $class, ResourceInterface $object): array |
146
|
|
|
{ |
147
|
2 |
|
$json = $this->getHydrator($class)->extract($object); |
148
|
2 |
|
$json = $this->extractApplyAnnotations($object, $json); |
149
|
2 |
|
return $json; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $json |
154
|
|
|
* @param ResourceInterface $object |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
2 |
View Code Duplication |
protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
|
|
|
|
158
|
|
|
{ |
159
|
2 |
|
foreach ($this->annotationHandlers as $annotationClass => $handler) { |
160
|
2 |
|
$annotation = $this->getAnnotation($object, $annotationClass); |
161
|
2 |
|
if ($annotation === null) { |
162
|
2 |
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
$json = $handler->extract($annotation, $object, $json); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
return $json; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param ResourceInterface $object |
173
|
|
|
* @param string $annotationClass |
174
|
|
|
* @return null|AnnotationInterface |
175
|
|
|
*/ |
176
|
3 |
|
protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
177
|
|
|
{ |
178
|
3 |
|
$class = get_class($object); |
179
|
3 |
|
if (isset($this->annotations[$class][$annotationClass])) { |
180
|
1 |
|
return $this->annotations[$class][$annotationClass]; |
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
if (!isset($this->annotations[$class])) { |
184
|
3 |
|
$this->annotations[$class] = []; |
185
|
|
|
} |
186
|
|
|
|
187
|
3 |
|
$this->annotations[$class][$annotationClass] = $this->annotationReader |
188
|
3 |
|
->getClassAnnotation( |
189
|
3 |
|
new ReflectionClass($object), |
190
|
|
|
$annotationClass |
191
|
|
|
) |
192
|
|
|
; |
193
|
|
|
|
194
|
3 |
|
if (get_class($this->annotations[$class][$annotationClass]) === $annotationClass) { |
195
|
|
|
return $this->annotations[$class][$annotationClass]; |
196
|
|
|
} |
197
|
|
|
|
198
|
3 |
|
$this->annotations[$class][$annotationClass] = $this->annotationReader |
199
|
3 |
|
->getClassAnnotation( |
200
|
3 |
|
new ReflectionClass(get_parent_class($object)), |
201
|
|
|
$annotationClass |
202
|
|
|
) |
203
|
|
|
; |
204
|
|
|
|
205
|
3 |
|
return $this->annotations[$class][$annotationClass]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $resource |
210
|
|
|
* @param ResourceInterface $object |
211
|
|
|
* @return ResourceInterface |
212
|
|
|
*/ |
213
|
1 |
|
public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
214
|
|
|
{ |
215
|
1 |
|
return $this->hydrateFQCN( |
216
|
|
|
$this->options[Options::NAMESPACE] . '\\Async\\' . $resource, |
217
|
1 |
|
$this->extractFQCN( |
218
|
|
|
$this->options[Options::NAMESPACE] . '\\Sync\\' . $resource, |
219
|
|
|
$object |
220
|
|
|
) |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $class |
226
|
|
|
* @return HydratorInterface |
227
|
|
|
*/ |
228
|
3 |
|
protected function getHydrator(string $class): HydratorInterface |
229
|
|
|
{ |
230
|
3 |
|
if (isset($this->hydrators[$class])) { |
231
|
3 |
|
return $this->hydrators[$class]; |
232
|
|
|
} |
233
|
|
|
|
234
|
3 |
|
$config = new Configuration($class); |
235
|
3 |
|
if (isset($this->options[Options::RESOURCE_CACHE_DIR])) { |
236
|
3 |
|
$config->setGeneratedClassesTargetDir($this->options[Options::RESOURCE_CACHE_DIR]); |
237
|
|
|
} |
238
|
3 |
|
if (isset($this->options[Options::RESOURCE_NAMESPACE])) { |
239
|
3 |
|
$config->setGeneratedClassesNamespace($this->options[Options::RESOURCE_NAMESPACE]); |
240
|
|
|
} |
241
|
3 |
|
$hydrator = $config->createFactory()->getHydratorClass(); |
242
|
3 |
|
$this->hydrators[$class] = new $hydrator; |
243
|
|
|
|
244
|
3 |
|
return $this->hydrators[$class]; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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.