1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Module\User\Data; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* User DTO (Data Transfer Object) class. |
7
|
|
|
* This class is used to transfer data between the different layers of the application |
8
|
|
|
* and serialize it for the frontend or database. |
9
|
|
|
* Documentation: https://samuel-gfeller.ch/docs/Domain#data-transfer-object-dto. |
10
|
|
|
*/ |
11
|
|
|
class UserData implements \JsonSerializable |
12
|
|
|
{ |
13
|
|
|
// Variable names matching database columns (camelCase instead of snake_case) |
14
|
|
|
public ?int $id; |
15
|
|
|
public ?string $firstName; |
16
|
|
|
public ?string $lastName; |
17
|
|
|
public ?string $email; |
18
|
|
|
public ?\DateTimeImmutable $updatedAt; |
19
|
|
|
public ?\DateTimeImmutable $createdAt; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array<string, mixed> $userData |
23
|
|
|
*/ |
24
|
5 |
|
public function __construct(array $userData = []) |
25
|
|
|
{ |
26
|
|
|
// Keys may be taken from view form or database, so they have to correspond to both; otherwise use mapper |
27
|
5 |
|
$this->id = $userData['id'] ?? null; |
28
|
5 |
|
$this->firstName = $userData['first_name'] ?? null; |
29
|
5 |
|
$this->lastName = $userData['last_name'] ?? null; |
30
|
5 |
|
$this->email = $userData['email'] ?? null; |
31
|
|
|
try { |
32
|
5 |
|
$this->updatedAt = $userData['updated_at'] ?? null ? new \DateTimeImmutable($userData['updated_at']) : null; |
33
|
|
|
} catch (\Exception $e) { |
34
|
|
|
$this->updatedAt = null; |
35
|
|
|
} |
36
|
|
|
try { |
37
|
5 |
|
$this->createdAt = $userData['created_at'] ?? null ? new \DateTimeImmutable($userData['created_at']) : null; |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
$this->createdAt = null; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Serialize the object to an array where the keys |
45
|
|
|
* represent the database fields. |
46
|
|
|
* |
47
|
|
|
* @return array<string, int|string|null> |
48
|
|
|
*/ |
49
|
1 |
|
public function toArrayForDatabase(): array |
50
|
|
|
{ |
51
|
1 |
|
return [ |
52
|
1 |
|
'id' => $this->id, |
53
|
1 |
|
'first_name' => $this->firstName, |
54
|
1 |
|
'last_name' => $this->lastName, |
55
|
1 |
|
'email' => $this->email, |
56
|
1 |
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Define how json_encode() should serialize the object |
61
|
|
|
* camelCase according to Google recommendation https://stackoverflow.com/a/19287394/9013718. |
62
|
|
|
* |
63
|
|
|
* @return array<string, mixed> in the format expected by the frontend |
64
|
|
|
*/ |
65
|
2 |
|
public function jsonSerialize(): array |
66
|
|
|
{ |
67
|
|
|
// camelCase according to Google recommendation https://stackoverflow.com/a/19287394/9013718 |
68
|
2 |
|
return [ |
69
|
2 |
|
'id' => $this->id, |
70
|
2 |
|
'firstName' => $this->firstName, |
71
|
2 |
|
'lastName' => $this->lastName, |
72
|
2 |
|
'email' => $this->email, |
73
|
2 |
|
'updatedAt' => $this->updatedAt?->format('Y-m-d H:i:s'), |
74
|
2 |
|
'createdAt' => $this->createdAt?->format('Y-m-d H:i:s'), |
75
|
2 |
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|