|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Module\User\Data; |
|
4
|
|
|
|
|
5
|
|
|
use App\Module\User\Enum\UserLang; |
|
6
|
|
|
use App\Module\User\Enum\UserStatus; |
|
7
|
|
|
use App\Module\User\Enum\UserTheme; |
|
8
|
|
|
|
|
9
|
|
|
class UserData implements \JsonSerializable |
|
10
|
|
|
{ |
|
11
|
|
|
// Variable names matching database columns (camelCase instead of snake_case) |
|
12
|
|
|
public ?int $id; // Mysql always returns string from db https://stackoverflow.com/a/5323169/9013718 |
|
13
|
|
|
public ?string $firstName; |
|
14
|
|
|
public ?string $lastName; |
|
15
|
|
|
// Email has to be default null as it is an indicator that user obj is empty in AuthService register function |
|
16
|
|
|
public ?string $email; |
|
17
|
|
|
public ?string $password; |
|
18
|
|
|
public ?string $password2; |
|
19
|
|
|
public ?string $passwordHash; |
|
20
|
|
|
public ?UserStatus $status = null; |
|
21
|
|
|
public ?UserTheme $theme = null; |
|
22
|
|
|
public UserLang $language = UserLang::English; |
|
23
|
|
|
public ?int $userRoleId = null; |
|
24
|
|
|
public ?\DateTimeImmutable $updatedAt; |
|
25
|
|
|
public ?\DateTimeImmutable $createdAt; |
|
26
|
|
|
// When adding a new attribute that should be editable with updateUser() it has to be added to authorization and service |
|
27
|
|
|
|
|
28
|
165 |
|
public function __construct(array $userData = []) |
|
29
|
|
|
{ |
|
30
|
|
|
// Keys may be taken from view form or database, so they have to correspond to both; otherwise use mapper |
|
31
|
165 |
|
$this->id = $userData['id'] ?? null; |
|
32
|
165 |
|
$this->firstName = $userData['first_name'] ?? null; |
|
33
|
165 |
|
$this->lastName = $userData['last_name'] ?? null; |
|
34
|
165 |
|
$this->email = $userData['email'] ?? null; |
|
35
|
165 |
|
$this->password = $userData['password'] ?? null; |
|
36
|
165 |
|
$this->password2 = $userData['password2'] ?? null; |
|
37
|
165 |
|
$this->passwordHash = $userData['password_hash'] ?? null; |
|
38
|
165 |
|
$this->theme = $userData['theme'] ?? null ? UserTheme::tryFrom($userData['theme']) : null; |
|
39
|
165 |
|
$this->language = UserLang::tryFrom($userData['language'] ?? '') ?? UserLang::English; |
|
40
|
|
|
// It may be useful to surround the datetime values with try catch in the user data constructor as object |
|
41
|
|
|
// can be created before validation |
|
42
|
165 |
|
$this->updatedAt = $userData['updated_at'] ?? null ? new \DateTimeImmutable($userData['updated_at']) : null; |
|
43
|
165 |
|
$this->createdAt = $userData['created_at'] ?? null ? new \DateTimeImmutable($userData['created_at']) : null; |
|
44
|
165 |
|
$this->status = isset($userData['status']) ? UserStatus::tryFrom($userData['status']) : UserStatus::Unverified; |
|
45
|
|
|
// Empty check is for testUserSubmitCreate_invalid test function where user_role_id is an empty string |
|
46
|
165 |
|
$this->userRoleId = !empty($userData['user_role_id']) ? $userData['user_role_id'] : null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the first and lastName in one string separated by a whitespace. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
7 |
|
public function getFullName(): string |
|
55
|
|
|
{ |
|
56
|
7 |
|
return $this->firstName . ' ' . $this->lastName; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns values of object as array for database (pw2 not included). |
|
61
|
|
|
* |
|
62
|
|
|
* The array keys MUST match with the database column names. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function toArrayForDatabase(): array |
|
67
|
|
|
{ |
|
68
|
2 |
|
return [ |
|
69
|
2 |
|
'id' => $this->id, |
|
70
|
2 |
|
'first_name' => $this->firstName, |
|
71
|
2 |
|
'last_name' => $this->lastName, |
|
72
|
2 |
|
'email' => $this->email, |
|
73
|
2 |
|
'password_hash' => $this->passwordHash, |
|
74
|
2 |
|
'user_role_id' => $this->userRoleId, |
|
75
|
2 |
|
'status' => $this->status?->value, |
|
76
|
2 |
|
'theme' => $this->theme?->value, |
|
77
|
2 |
|
'language' => $this->language->value, |
|
78
|
2 |
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
public function jsonSerialize(): array |
|
82
|
|
|
{ |
|
83
|
4 |
|
return [ |
|
84
|
4 |
|
'id' => $this->id, |
|
85
|
4 |
|
'firstName' => $this->firstName, |
|
86
|
4 |
|
'lastName' => $this->lastName, |
|
87
|
4 |
|
'email' => $this->email, |
|
88
|
4 |
|
'status' => $this->status, |
|
89
|
4 |
|
'userRoleId' => $this->userRoleId, |
|
90
|
4 |
|
'updatedAt' => $this->updatedAt?->format('Y-m-d H:i:s'), |
|
91
|
4 |
|
'createdAt' => $this->createdAt?->format('Y-m-d H:i:s'), |
|
92
|
4 |
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|