-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMapper.php
More file actions
338 lines (281 loc) · 10.5 KB
/
Mapper.php
File metadata and controls
338 lines (281 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace UniMapper;
use UniMapper\Entity\Filter;
use UniMapper\Entity\Reflection;
class Mapper
{
/** @var array */
private $adapterMappings = [];
public function registerAdapterMapping($name, Adapter\Mapping $mapping)
{
if (isset($this->adapterMappings[$name])) {
throw new Exception\InvalidArgumentException(
"Mapping on adapater " . $name . " already registered!"
);
}
$this->adapterMappings[$name] = $mapping;
}
/**
* Convert value to defined property format
*
* @param Entity\Reflection\Property $property
* @param mixed $value
*
* @return mixed
*
* @throws Exception\InvalidArgumentException
*/
public function mapValue(Entity\Reflection\Property $property, $value)
{
// Call adapter's mapping if needed
if (!$property->getReflection()->hasAdapter()) {
throw new Exception\InvalidArgumentException(
"Entity " . $property->getReflection()->getClassName()
. " has no adapter defined!"
);
}
if (isset($this->adapterMappings[$property->getReflection()->getAdapterName()])) {
$value = $this->adapterMappings[$property->getReflection()->getAdapterName()]
->mapValue($property, $value);
}
if ($property->hasOption(Reflection\Property\Option\Map::KEY)) {
$mapOption = $property->getOption(Reflection\Property\Option\Map::KEY);
if (!$mapOption) {
throw new Exception\InvalidArgumentException(
"Mapping disabled on property " . $property->getName() . "!"
);
}
// Call map filter from property option
$filterIn = $mapOption->getFilterIn();
if ($filterIn) {
$value = call_user_func($filterIn, $value);
}
}
if ($value === null || $value === "") {
return null;
}
if ($property->isScalarType($property->getType())
|| $property->getType() === Entity\Reflection\Property::TYPE_ARRAY
) {
// Scalar & array
if ($property->getType() === Reflection\Property::TYPE_BOOLEAN
&& in_array($value, ["false", "true"], true)
) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
if (Validator::isTraversable($value)
&& $property->getType() !== Entity\Reflection\Property::TYPE_ARRAY
) {
throw new Exception\InvalidArgumentException(
"Traversable value can not be mapped to scalar!",
$value
);
}
if (settype($value, $property->getType())) {
return $value;
}
} elseif ($property->getType() === Entity\Reflection\Property::TYPE_COLLECTION) {
// Collection
return $this->mapCollection($property->getTypeOption(), $value);
} elseif ($property->getType() === Entity\Reflection\Property::TYPE_ENTITY) {
// Entity
if ($value instanceof Entity && $property->hasOption(Entity\Reflection\Property::OPTION_MAP_FILTER)) {
// if value is entity created by filter don't map it again
return $value;
}
return $this->mapEntity($property->getTypeOption(), $value);
} elseif ($property->getType() === Entity\Reflection\Property::TYPE_DATETIME
|| $property->getType() === Entity\Reflection\Property::TYPE_DATE
) {
// DateTime & Date
if ($value instanceof \DateTime) {
return $value;
} elseif (is_array($value) && isset($value["date"])) {
$date = $value["date"];
} elseif (is_object($value) && isset($value->date)) {
$date = $value->date;
} else {
$date = $value;
}
if (isset($date)) {
try {
return new \DateTime($date);
} catch (\Exception $e) {
throw new Exception\InvalidArgumentException(
"Can not map value to DateTime automatically! "
. $e->getMessage(),
$value
);
}
}
}
// Unexpected value type
throw new Exception\InvalidArgumentException(
"Value can not be mapped automatically!",
$value
);
}
public function mapCollection($name, $data)
{
if (!Validator::isTraversable($data)) {
throw new Exception\InvalidArgumentException(
"Input data must be traversable!",
$data
);
}
$collection = new Entity\Collection($name);
foreach ($data as $value) {
$collection[] = $this->mapEntity($name, $value);
}
return $collection;
}
public function mapEntity($name, $data)
{
if (!Validator::isTraversable($data)) {
throw new Exception\InvalidArgumentException(
"Input data must be traversable!",
$data
);
}
$reflection = Entity\Reflection::load($name);
$values = [];
foreach ($data as $name => $value) {
// Map property name if needed
foreach ($reflection->getProperties() as $property) {
if ($property->hasOption(Reflection\Property\Option\Map::KEY) && !$property->getOption(Reflection\Property\Option\Map::KEY)) {
if ($property->getName() === $name) {
continue 2; // Skip disabled
}
continue;
}
if ($property->getUnmapped() === $name) {
$name = $property->getName();
break;
}
}
// Skip undefined properties
if (!$reflection->hasProperty($name)) {
continue;
}
// Map value
$values[$name] = $this->mapValue(
$reflection->getProperty($name),
$value
);
}
return $reflection->createEntity($values);
}
/**
* Convert entity to simple array
*
* @param Entity $entity
*
* @return array
*/
public function unmapEntity(Entity $entity)
{
$output = [];
foreach ($entity->getData() as $name => $value) {
$property = $entity::getReflection()->getProperty($name);
// Skip associations & readonly & disabled mapping
if ($property->hasOption(Reflection\Property\Option\Assoc::KEY)
|| !$property->isWritable()
|| ($property->hasOption(Reflection\Property\Option\Map::KEY)
&& !$property->getOption(Reflection\Property\Option\Map::KEY))
) {
continue;
}
$output[$property->getUnmapped()] = $this->unmapValue($property, $value);
}
return $output;
}
public function unmapValue(Entity\Reflection\Property $property, $value)
{
if ($property->hasOption(Reflection\Property\Option\Map::KEY)) {
$mapOption = $property->getOption(Reflection\Property\Option\Map::KEY);
if (!$mapOption) {
throw new Exception\InvalidArgumentException(
"Mapping disabled on property " . $property->getName() . "!"
);
}
// Call map filter from property option
$filterOut = $mapOption->getFilterOut();
if ($filterOut) {
$value = call_user_func($filterOut, $value);
}
}
if ($value instanceof Entity\Collection) {
return $this->unmapCollection($value);
} elseif ($value instanceof Entity) {
return $this->unmapEntity($value);
}
// Call adapter's mapping if needed
if (!$property->getReflection()->hasAdapter()) {
throw new Exception\InvalidArgumentException(
"Entity " . $property->getReflection()->getClassName()
. " has no adapter defined!"
);
}
if (isset($this->adapterMappings[$property->getReflection()->getAdapterName()])) {
return $this->adapterMappings[$property->getReflection()->getAdapterName()]
->unmapValue($property, $value);
}
return $value;
}
/**
* Convert entity to simple array
*
* @param Entity\Collection $collection
*
* @return array
*/
public function unmapCollection(Entity\Collection $collection)
{
$data = [];
foreach ($collection as $index => $entity) {
$data[$index] = $this->unmapEntity($entity);
}
return $data;
}
/**
* @param Reflection $reflection
* @param array $filter
*
* @return array
*/
public function unmapFilter(Reflection $reflection, array $filter)
{
$result = [];
if (Filter::isGroup($filter)) {
foreach ($filter as $modifier => $item) {
$result[$modifier] = $this->unmapFilter($reflection, $item);
}
} else {
foreach ($filter as $name => $item) {
$property = $reflection->getProperty($name);
$unmappedName = $property->getUnmapped();
foreach ($item as $modifier => $value) {
if ($property->getType() !== Reflection\Property::TYPE_ARRAY
&& in_array($modifier, [Filter::EQUAL, Filter::NOT], true)
&& is_array($value)
) {
// IN/NOT IN cases
$result[$unmappedName][$modifier] = [];
foreach ($value as $key => $valueVal) {
$result[$unmappedName][$modifier][$key] = $this->unmapValue(
$property,
$valueVal
);
}
} else {
$result[$unmappedName][$modifier] = $this->unmapValue(
$property,
$value
);
}
}
}
}
return $result;
}
}