-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProperty.php
More file actions
463 lines (400 loc) · 12.8 KB
/
Property.php
File metadata and controls
463 lines (400 loc) · 12.8 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
namespace UniMapper\Entity\Reflection;
use UniMapper\Validator,
UniMapper\Convention,
UniMapper\Exception;
use UniMapper\Entity;
class Property
{
const TYPE_DATETIME = "DateTime",
TYPE_DATE = "Date",
TYPE_COLLECTION = "collection",
TYPE_ENTITY = "entity",
TYPE_BOOLEAN = "boolean",
TYPE_INTEGER = "integer",
TYPE_DOUBLE = "double",
TYPE_STRING = "string",
TYPE_ARRAY = "array";
/** @var string $type */
private $type;
/** @var mixed $typeOption */
private $typeOption;
/** @var string $name */
private $name;
/** @var array $scalarTypes */
private static $scalarTypes = [
self::TYPE_BOOLEAN,
self::TYPE_INTEGER,
self::TYPE_DOUBLE,
self::TYPE_STRING
];
/** @var array $typeAliases */
private static $typeAliases = [
"bool" => self::TYPE_BOOLEAN,
"int" => self::TYPE_INTEGER,
"real" => self::TYPE_DOUBLE,
"float" => self::TYPE_DOUBLE
];
/** @var Entity\Reflection */
private $reflection;
/** @var boolean $readonly */
private $readonly = false;
/** @var array */
private $options = [];
/**
* @param string $type
* @param string $name
* @param Entity\Reflection $reflection
* @param bool $readonly
* @param string $options
*/
public function __construct(
$type,
$name,
Entity\Reflection $reflection,
$readonly = false,
$options = null
) {
$this->reflection = $reflection;
$this->name = $name;
$this->readonly = (bool) $readonly;
$this->_initType($type);
$this->_initOptions($options);
}
private function _initOptions($options)
{
try {
$parsed = Annotation::parseOptions($options);
} catch (Exception\AnnotationException $e) {
throw new Exception\PropertyException(
$e->getMessage(),
$e->getCode(),
$e
);
}
foreach (Annotation::getRegisteredOptions() as $key => $class) {
$value = array_key_exists($key, $parsed) ? $parsed[$key] : false;
$parameters = preg_grep("/" . $key . "-[aA-zZ-]*/", array_keys($parsed));
if ($value !== false || !empty($parameters)) {
try {
$this->options[$key] = $class::create(
$this,
$value,
array_intersect_key($parsed, array_flip($parameters))
);
} catch (Exception\OptionException $e) {
throw new Exception\PropertyException(
$e->getMessage(),
$e->getCode(),
$e
);
}
}
}
foreach ($this->options as $key => $option) {
$class = Annotation::getRegisteredOptions()[$key];
try {
$class::afterCreate($this, $option);
} catch (Exception\OptionException $e) {
throw new Exception\PropertyException(
$e->getMessage(),
$e->getCode(),
$e
);
}
}
}
public function isWritable()
{
return $this->readonly;
}
/**
* Is type scalar?
*
* @param string $type
*
* @return bool
*/
public static function isScalarType($type)
{
return in_array($type, self::$scalarTypes, true);
}
/**
* Get entity reflection
*
* @return Entity\Reflection
*/
public function getReflection()
{
return $this->reflection;
}
/**
* Get property name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function getUnmapped()
{
if ($this->hasOption(Entity\Reflection\Property\Option\Map::KEY)
&& !$this->getOption(Entity\Reflection\Property\Option\Map::KEY)
) {
throw new \Exception("Mapping is disabled!");
}
$name = $this->hasOption(Entity\Reflection\Property\Option\Map::KEY) ? $this->getOption(Entity\Reflection\Property\Option\Map::KEY)->getUnmapped() : $this->name;
$adapterName = $this->reflection->getAdapterName();
if (Convention::hasAdapterConvention($adapterName)) {
return Convention::getAdapterConvention($adapterName)->mapProperty($name);
}
return $name;
}
/**
* Get property type
*
* @return string
*/
public function getType()
{
return $this->type;
}
public function getTypeOption()
{
return $this->typeOption;
}
/**
* Initialize property type
*
* @param string $definition
*
* @return mixed
*
* @throws Exception\PropertyException
*/
private function _initType($definition)
{
if (isset(self::$typeAliases[$definition])) {
$definition = self::$typeAliases[$definition];
}
if (self::isScalarType($definition)
|| in_array(
$definition,
[self::TYPE_ARRAY, self::TYPE_DATE, self::TYPE_DATETIME],
true
)
) {
// Scalar, array, date, datetime
$this->type = $definition;
} elseif (class_exists(Convention::nameToClass($definition, Convention::ENTITY_MASK))) {
// Entity
$this->type = self::TYPE_ENTITY;
$this->typeOption = $definition;
} elseif (substr($definition, -2) === "[]") {
// Collection
$this->type = self::TYPE_COLLECTION;
$this->typeOption = rtrim($definition, "[]");
} else {
throw new Exception\PropertyException(
"Unsupported type '" . $definition . "'!"
);
}
}
/**
* Validate value type
*
* @param mixed $value Given value
*
* @throws Exception\InvalidArgumentException
* @throws \Exception
*/
public function validateValueType($value)
{
if ($this->hasOption(Entity\Reflection\Property\Option\Primary::KEY)
&& (Entity\Reflection\Property\Option\Primary::isEmpty($value))
) {
throw new Exception\InvalidArgumentException(
"Primary value can not be empty string or null!",
$value
);
}
if ($value === null) {
return;
}
// Enumeration
if ($this->hasOption(Entity\Reflection\Property\Option\Enum::KEY)
&& !$this->getOption(Entity\Reflection\Property\Option\Enum::KEY)->isValid($value)
) {
throw new Exception\InvalidArgumentException(
"Value " . $value . " is not from defined entity enumeration "
. "range on property " . $this->name . "!",
$value
);
}
// Scalar or array type
if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {
if (gettype($value) === $this->type) {
return;
}
throw new Exception\InvalidArgumentException(
"Expected " . $this->type . " but " . gettype($value)
. " given on property " . $this->name . "!",
$value
);
}
// Object validation
$givenType = gettype($value);
if ($givenType === "object") {
$givenType = get_class($value);
}
if ($this->type === self::TYPE_ENTITY) {
// Entity
$expectedEntityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
if ($value instanceof $expectedEntityClass) {
return;
} else {
throw new Exception\InvalidArgumentException(
"Expected entity " . $this->typeOption . " but " . $givenType
. " given on property " . $this->name . "!",
$value
);
}
} elseif ($this->type === self::TYPE_COLLECTION) {
// Collection
$entityClass = Convention::nameToClass($this->typeOption, Convention::ENTITY_MASK);
if (!$value instanceof Entity\Collection) {
throw new Exception\InvalidArgumentException(
"Expected entity collection but " . $givenType . " given on"
. " property " . $this->name . "!",
$value
);
} elseif ($value->getEntityClass() !== $entityClass) {
throw new Exception\InvalidArgumentException(
"Expected collection of entity " . $entityClass
. " but collection of entity " . $value->getEntityClass()
. " given on property " . $this->name . "!",
$value
);
} else {
return;
}
} elseif ($this->type === self::TYPE_DATETIME) {
// DateTime
if ($value instanceof \DateTime) {
return;
} else {
throw new Exception\InvalidArgumentException(
"Expected DateTime but " . $givenType . " given on"
. " property " . $this->name . "!",
$value
);
}
} elseif ($this->type === self::TYPE_DATE) {
// Date
if ($value instanceof \DateTime) {
return;
} else {
throw new Exception\InvalidArgumentException(
"Expected date as DateTime object but " . $givenType
. " given on property " . $this->name . "!",
$value
);
}
} else {
// Unexpected
throw new \Exception("Unsupported type " . $this->type . "!");
}
}
/**
* Try to convert value on required type automatically
*
* @param mixed $value
*
* @return mixed
*
* @throws Exception\InvalidArgumentException
*/
public function convertValue($value)
{
if ($value === null || ($value === "" && $this->type !== self::TYPE_STRING)) {
return;
}
if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {
// Scalar and array
if ($this->type === self::TYPE_BOOLEAN && strtolower($value) === "false") {
return false;
}
if (is_scalar($value) || $this->type === self::TYPE_ARRAY) {
if (settype($value, $this->type)) {
return $value;
}
}
} elseif ($this->type === self::TYPE_DATETIME
|| $this->type === self::TYPE_DATE
) {
// DateTime
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) {
}
}
} elseif ($this->type === self::TYPE_COLLECTION
&& Validator::isTraversable($value)
) {
// Collection
return new Entity\Collection($this->typeOption, $value);
} elseif ($this->type === self::TYPE_ENTITY
&& Validator::isTraversable($value)
) {
// Entity
return Entity\Reflection::load($this->typeOption)->createEntity($value);
}
throw new Exception\InvalidArgumentException(
"Can not convert value on property '" . $this->name
. "' automatically!",
$value
);
}
/**
* Has option?
*
* @param string $key
*
* @return boolean
*/
public function hasOption($key)
{
return array_key_exists($key, $this->options);
}
/**
* Get option
*
* @param string $key
*
* @return mixed
*
* @throws Exception\InvalidArgumentException
*/
public function getOption($key)
{
if (!$this->hasOption($key)) {
throw new Exception\InvalidArgumentException(
"Option " . $key . " not defined on "
. $this->reflection->getClassName() . "::$"
. $this->name . "!"
);
}
return $this->options[$key];
}
}