|
15 | 15 |
|
16 | 16 | namespace Spotlibs\PhpLib\Dtos;
|
17 | 17 |
|
| 18 | +use Exception; |
18 | 19 | use ReflectionClass;
|
19 | 20 |
|
20 | 21 | /**
|
@@ -44,15 +45,23 @@ public function __construct(array $data = [])
|
44 | 45 | $this->aliases = [];
|
45 | 46 | }
|
46 | 47 | $reflector = new ReflectionClass(static::class);
|
47 |
| - foreach ($data as $key => $value) { |
48 |
| - $temp_key = array_search($key, $this->aliases, true); |
49 |
| - $key = is_string($temp_key) ? $temp_key : $key; |
50 |
| - if (property_exists($this, $key)) { |
51 |
| - if (is_array($value)) { |
52 |
| - $this->convertArray($reflector, $key, $value); |
| 48 | + try { |
| 49 | + foreach ($data as $key => $value) { |
| 50 | + $temp_key = array_search($key, $this->aliases, true); |
| 51 | + $key = is_string($temp_key) ? $temp_key : $key; |
| 52 | + if (property_exists($this, $key)) { |
| 53 | + if (is_array($value)) { |
| 54 | + $this->convertArray($reflector, $key, $value); |
| 55 | + } |
| 56 | + $this->{$key} = $value; |
53 | 57 | }
|
54 |
| - $this->{$key} = $value; |
55 | 58 | }
|
| 59 | + } catch (\Throwable $th) { |
| 60 | + if (substr($th->getMessage(), 0, 30) == "property_exists(): Argument #2") { |
| 61 | + $jsonEncoded = json_encode($data); |
| 62 | + throw new Exception($th->getMessage() . '. Error constructing this array to DTO: ' . $jsonEncoded); |
| 63 | + } |
| 64 | + throw $th; |
56 | 65 | }
|
57 | 66 | }
|
58 | 67 |
|
|
0 commit comments