|
9 | 9 |
|
10 | 10 | class UserDTO extends Data
|
11 | 11 | {
|
12 |
| - public function __construct( |
13 |
| - public int $id, |
14 |
| - public string $salutation_type, |
15 |
| - public ?string $firstname, |
16 |
| - public ?string $lastname, |
17 |
| - public string $email, |
18 |
| - public bool $is_superadmin, |
19 |
| - public bool $is_accountant, |
20 |
| - ) {} |
21 |
| - |
22 |
| - public static function fromResponse(Response $response): self |
23 |
| - { |
24 |
| - if ($response->failed()) { |
25 |
| - throw new \Exception('Failed to create DTO from Response'); |
26 |
| - } |
27 |
| - |
28 |
| - $data = $response->json(); |
29 |
| - |
30 |
| - return self::fromArray($data); |
31 |
| - } |
32 |
| - |
33 |
| - public static function fromArray(array $data): self |
34 |
| - { |
35 |
| - if (! $data) { |
36 |
| - throw new Exception('Unable to create DTO. Data missing from response.'); |
37 |
| - } |
38 |
| - |
39 |
| - return new self( |
40 |
| - id: Arr::get($data, 'id'), |
41 |
| - salutation_type: Arr::get($data, 'salutation_type'), |
42 |
| - firstname: Arr::get($data, 'firstname'), |
43 |
| - lastname: Arr::get($data, 'lastname'), |
44 |
| - email: Arr::get($data, 'email'), |
45 |
| - is_superadmin: Arr::get($data, 'is_superadmin'), |
46 |
| - is_accountant: Arr::get($data, 'is_accountant'), |
47 |
| - ); |
48 |
| - } |
| 12 | + public function __construct( |
| 13 | + public int $id, |
| 14 | + public ?string $salutation_type, |
| 15 | + public ?string $firstname, |
| 16 | + public ?string $lastname, |
| 17 | + public string $email, |
| 18 | + public bool $is_superadmin, |
| 19 | + public bool $is_accountant, |
| 20 | + ) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public static function fromResponse(Response $response): self |
| 25 | + { |
| 26 | + if ($response->failed()) { |
| 27 | + throw new \Exception('Failed to create DTO from Response'); |
| 28 | + } |
| 29 | + |
| 30 | + $data = $response->json(); |
| 31 | + |
| 32 | + return self::fromArray($data); |
| 33 | + } |
| 34 | + |
| 35 | + public static function fromArray(array $data): self |
| 36 | + { |
| 37 | + if (!$data) { |
| 38 | + throw new Exception('Unable to create DTO. Data missing from response.'); |
| 39 | + } |
| 40 | + |
| 41 | + return new self( |
| 42 | + id: Arr::get($data, 'id'), |
| 43 | + salutation_type: Arr::get($data, 'salutation_type'), |
| 44 | + firstname: Arr::get($data, 'firstname'), |
| 45 | + lastname: Arr::get($data, 'lastname'), |
| 46 | + email: Arr::get($data, 'email'), |
| 47 | + is_superadmin: Arr::get($data, 'is_superadmin'), |
| 48 | + is_accountant: Arr::get($data, 'is_accountant'), |
| 49 | + ); |
| 50 | + } |
49 | 51 | }
|
0 commit comments