|
9 | 9 | * @param $args |
10 | 10 | * |
11 | 11 | * @return T |
| 12 | + * @throws ReflectionException |
12 | 13 | */ |
13 | 14 | function plainToClass($class, $args) |
14 | 15 | { |
15 | 16 | $instance = new $class; |
16 | 17 | if ($args !== null) { |
17 | | - if (method_exists($class,'plainToClass')){ |
| 18 | + if (method_exists($class, 'plainToClass')) { |
18 | 19 | return $class::plainToClass($args); |
19 | 20 | } |
20 | | - foreach ($args as $key => $val) { |
21 | | - if (property_exists($instance, $key)) { |
22 | | - $instance->{$key} = $val; |
| 21 | + $refInstance = new ReflectionClass($class); |
| 22 | + if (is_object($args)) { |
| 23 | + $refArgsObject = new ReflectionObject($args); |
| 24 | + foreach ($refInstance->getProperties() as $item) { |
| 25 | + if ($refArgsObject->hasProperty($item->name)) { |
| 26 | + $scalarTypes = ['int', 'float', 'string', 'bool']; |
| 27 | + |
| 28 | + $propertyClass = $refInstance->getProperty($item->name); |
| 29 | + $propertyClassType = $refInstance->getProperty($item->name)->getType()->getName(); |
| 30 | + |
| 31 | + $propertyArgs = $refArgsObject->getProperty($item->name); |
| 32 | + $propertyArgsType = getType($args->{$item->name}); |
| 33 | + |
| 34 | + ## if scalar type |
| 35 | + if (in_array($propertyClassType, $scalarTypes) && in_array($propertyArgsType, $scalarTypes)) { |
| 36 | + $instance->{$item->name} = $args->{$item->name}; |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if ($propertyClassType === 'array' && $propertyArgsType === 'array') { |
| 41 | + if ($propertyClass->getDocComment()) { |
| 42 | + preg_match('/array<([a-zA-Z\d\\\]+)>/m', $propertyClass->getDocComment(), $docType); |
| 43 | + $docType = $docType[1] ?? null; |
| 44 | + |
| 45 | + if ($docType && class_exists($docType)) { |
| 46 | + foreach ($args->{$item->name} as $el) { |
| 47 | + $instance->{$item->name}[] = plainToClass($docType, $el); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if ($propertyClassType && class_exists($propertyClassType)) { |
| 55 | + $instance->{$item->name} = plainToClass($propertyClassType, $args->{$item->name}); |
| 56 | + continue; |
| 57 | + } |
| 58 | + $instance->{$item->name} = $args->{$item->name}; |
| 59 | + } |
| 60 | + } |
| 61 | + } else { |
| 62 | + foreach ($refInstance->getProperties() as $item) { |
| 63 | + if (array_key_exists($item->name, $args)) { |
| 64 | + $scalarTypes = ['int', 'float', 'string', 'bool']; |
| 65 | + |
| 66 | + $propertyClass = $refInstance->getProperty($item->name); |
| 67 | + $propertyClassType = $refInstance->getProperty($item->name)->getType()->getName(); |
| 68 | + ## if scalar type |
| 69 | + if (in_array($propertyClassType, $scalarTypes)) { |
| 70 | + $instance->{$item->name} = $args[$item->name]; |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if ($propertyClassType === 'array') { |
| 75 | + if ($propertyClass->getDocComment()) { |
| 76 | + preg_match('/array<([a-zA-Z\d\\\]+)>/m', $propertyClass->getDocComment(), $docType); |
| 77 | + $docType = $docType[1] ?? null; |
| 78 | + |
| 79 | + if ($docType && class_exists($docType)) { |
| 80 | + foreach ($args[$item->name] as $el) { |
| 81 | + $instance->{$item->name}[] = plainToClass($docType, $el); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if ($propertyClassType && class_exists($propertyClassType)) { |
| 89 | + $instance->{$item->name} = plainToClass($propertyClassType, $args[$item->name]); |
| 90 | + continue; |
| 91 | + } |
| 92 | + $instance->{$item->name} = $args[$item->name]; |
| 93 | + } |
23 | 94 | } |
24 | 95 | } |
25 | 96 | } |
|
0 commit comments