|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Rest/DTO/User/User.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\DTO\User; |
|
10
|
|
|
|
|
11
|
|
|
use App\DTO\RestDto; |
|
12
|
|
|
use App\Entity\Interfaces\EntityInterface; |
|
13
|
|
|
use App\Entity\Interfaces\UserGroupAwareInterface; |
|
14
|
|
|
use App\Entity\User as Entity; |
|
15
|
|
|
use App\Entity\UserGroup as UserGroupEntity; |
|
16
|
|
|
use App\Enum\Language; |
|
17
|
|
|
use App\Service\Localization; |
|
18
|
|
|
use App\Validator\Constraints as AppAssert; |
|
19
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
20
|
|
|
use function array_map; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class User |
|
24
|
|
|
* |
|
25
|
|
|
* @package App\DTO\User |
|
26
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
27
|
|
|
* |
|
28
|
|
|
* @method Entity|EntityInterface update(EntityInterface $entity) |
|
29
|
|
|
* |
|
30
|
|
|
* @psalm-consistent-constructor |
|
31
|
|
|
*/ |
|
32
|
|
|
#[AppAssert\UniqueEmail] |
|
33
|
|
|
#[AppAssert\UniqueUsername] |
|
34
|
|
|
class User extends RestDto |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array<string, string> |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static array $mappings = [ |
|
40
|
|
|
'password' => 'updatePassword', |
|
41
|
|
|
'userGroups' => 'updateUserGroups', |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
#[Assert\NotBlank] |
|
45
|
|
|
#[Assert\NotNull] |
|
46
|
|
|
#[Assert\Length(min: 2, max: 255)] |
|
47
|
|
|
protected string $username = ''; |
|
48
|
|
|
|
|
49
|
|
|
#[Assert\NotBlank] |
|
50
|
|
|
#[Assert\NotNull] |
|
51
|
|
|
#[Assert\Length(min: 2, max: 255)] |
|
52
|
|
|
protected string $firstName = ''; |
|
53
|
|
|
|
|
54
|
|
|
#[Assert\NotBlank] |
|
55
|
|
|
#[Assert\NotNull] |
|
56
|
|
|
#[Assert\Length(min: 2, max: 255)] |
|
57
|
|
|
protected string $lastName = ''; |
|
58
|
|
|
|
|
59
|
|
|
#[Assert\NotBlank] |
|
60
|
|
|
#[Assert\NotNull] |
|
61
|
|
|
#[Assert\Email] |
|
62
|
|
|
protected string $email = ''; |
|
63
|
|
|
|
|
64
|
|
|
#[Assert\NotBlank] |
|
65
|
|
|
#[Assert\NotNull] |
|
66
|
|
|
//#[AppAssert\Language] |
|
67
|
|
|
protected Language $language; |
|
68
|
|
|
|
|
69
|
|
|
#[Assert\NotBlank] |
|
70
|
|
|
#[Assert\NotNull] |
|
71
|
|
|
#[AppAssert\Locale] |
|
72
|
|
|
protected string $locale = Localization::DEFAULT_LOCALE; |
|
73
|
|
|
|
|
74
|
|
|
#[Assert\NotBlank] |
|
75
|
|
|
#[Assert\NotNull] |
|
76
|
|
|
#[AppAssert\Timezone] |
|
77
|
|
|
protected string $timezone = Localization::DEFAULT_TIMEZONE; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var UserGroupEntity[]|array<int, UserGroupEntity> |
|
81
|
|
|
*/ |
|
82
|
|
|
#[AppAssert\EntityReferenceExists(entityClass: UserGroupEntity::class)] |
|
83
|
|
|
protected array $userGroups = []; |
|
84
|
|
|
|
|
85
|
|
|
protected string $password = ''; |
|
86
|
|
|
|
|
87
|
166 |
|
public function __construct() |
|
88
|
|
|
{ |
|
89
|
166 |
|
$this->language = Language::getDefault(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
9 |
|
public function getUsername(): string |
|
93
|
|
|
{ |
|
94
|
9 |
|
return $this->username; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
21 |
|
public function setUsername(string $username): self |
|
98
|
|
|
{ |
|
99
|
21 |
|
$this->setVisited('username'); |
|
100
|
|
|
|
|
101
|
21 |
|
$this->username = $username; |
|
102
|
|
|
|
|
103
|
21 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
public function getFirstName(): string |
|
107
|
|
|
{ |
|
108
|
8 |
|
return $this->firstName; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
19 |
|
public function setFirstName(string $firstName): self |
|
112
|
|
|
{ |
|
113
|
19 |
|
$this->setVisited('firstName'); |
|
114
|
|
|
|
|
115
|
19 |
|
$this->firstName = $firstName; |
|
116
|
|
|
|
|
117
|
19 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
8 |
|
public function getLastName(): string |
|
121
|
|
|
{ |
|
122
|
8 |
|
return $this->lastName; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
19 |
|
public function setLastName(string $lastName): self |
|
126
|
|
|
{ |
|
127
|
19 |
|
$this->setVisited('lastName'); |
|
128
|
|
|
|
|
129
|
19 |
|
$this->lastName = $lastName; |
|
130
|
|
|
|
|
131
|
19 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
11 |
|
public function getEmail(): string |
|
135
|
|
|
{ |
|
136
|
11 |
|
return $this->email; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
23 |
|
public function setEmail(string $email): self |
|
140
|
|
|
{ |
|
141
|
23 |
|
$this->setVisited('email'); |
|
142
|
|
|
|
|
143
|
23 |
|
$this->email = $email; |
|
144
|
|
|
|
|
145
|
23 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
14 |
|
public function getLanguage(): Language |
|
149
|
|
|
{ |
|
150
|
14 |
|
return $this->language; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
19 |
|
public function setLanguage(Language $language): self |
|
154
|
|
|
{ |
|
155
|
19 |
|
$this->setVisited('language'); |
|
156
|
|
|
|
|
157
|
19 |
|
$this->language = $language; |
|
158
|
|
|
|
|
159
|
19 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
7 |
|
public function getLocale(): string |
|
163
|
|
|
{ |
|
164
|
7 |
|
return $this->locale; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
12 |
|
public function setLocale(string $locale): self |
|
168
|
|
|
{ |
|
169
|
12 |
|
$this->setVisited('locale'); |
|
170
|
|
|
|
|
171
|
12 |
|
$this->locale = $locale; |
|
172
|
|
|
|
|
173
|
12 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
6 |
|
public function getTimezone(): string |
|
177
|
|
|
{ |
|
178
|
6 |
|
return $this->timezone; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
11 |
|
public function setTimezone(string $timezone): self |
|
182
|
|
|
{ |
|
183
|
11 |
|
$this->setVisited('timezone'); |
|
184
|
|
|
|
|
185
|
11 |
|
$this->timezone = $timezone; |
|
186
|
|
|
|
|
187
|
11 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return array<int, UserGroupEntity> |
|
192
|
|
|
*/ |
|
193
|
10 |
|
public function getUserGroups(): array |
|
194
|
|
|
{ |
|
195
|
10 |
|
return $this->userGroups; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param array<int, UserGroupEntity> $userGroups |
|
200
|
|
|
*/ |
|
201
|
16 |
|
public function setUserGroups(array $userGroups): self |
|
202
|
|
|
{ |
|
203
|
16 |
|
$this->setVisited('userGroups'); |
|
204
|
|
|
|
|
205
|
16 |
|
$this->userGroups = $userGroups; |
|
206
|
|
|
|
|
207
|
16 |
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
5 |
|
public function getPassword(): string |
|
211
|
|
|
{ |
|
212
|
5 |
|
return $this->password; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
12 |
|
public function setPassword(?string $password = null): self |
|
216
|
|
|
{ |
|
217
|
12 |
|
if ($password !== null) { |
|
218
|
12 |
|
$this->setVisited('password'); |
|
219
|
|
|
|
|
220
|
12 |
|
$this->password = $password; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
12 |
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritdoc} |
|
228
|
|
|
* |
|
229
|
|
|
* @param EntityInterface|Entity $entity |
|
230
|
|
|
*/ |
|
231
|
15 |
|
public function load(EntityInterface $entity): self |
|
232
|
|
|
{ |
|
233
|
15 |
|
if ($entity instanceof Entity) { |
|
234
|
15 |
|
$this->id = $entity->getId(); |
|
235
|
15 |
|
$this->username = $entity->getUsername(); |
|
236
|
15 |
|
$this->firstName = $entity->getFirstName(); |
|
237
|
15 |
|
$this->lastName = $entity->getLastName(); |
|
238
|
15 |
|
$this->email = $entity->getEmail(); |
|
239
|
15 |
|
$this->language = $entity->getLanguage(); |
|
240
|
15 |
|
$this->locale = $entity->getLocale(); |
|
241
|
15 |
|
$this->timezone = $entity->getTimezone(); |
|
242
|
|
|
|
|
243
|
|
|
/** @var array<int, UserGroupEntity> $groups */ |
|
244
|
15 |
|
$groups = $entity->getUserGroups()->toArray(); |
|
245
|
|
|
|
|
246
|
15 |
|
$this->userGroups = $groups; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
15 |
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Method to update User entity password. |
|
254
|
|
|
*/ |
|
255
|
3 |
|
protected function updatePassword(Entity $entity, string $value): self |
|
256
|
|
|
{ |
|
257
|
3 |
|
$entity->setPlainPassword($value); |
|
258
|
|
|
|
|
259
|
3 |
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Method to update User entity user groups. |
|
264
|
|
|
* |
|
265
|
|
|
* @param array<int, UserGroupEntity> $value |
|
266
|
|
|
*/ |
|
267
|
2 |
|
protected function updateUserGroups(UserGroupAwareInterface $entity, array $value): self |
|
268
|
|
|
{ |
|
269
|
2 |
|
$entity->clearUserGroups(); |
|
270
|
|
|
|
|
271
|
2 |
|
array_map( |
|
272
|
2 |
|
static fn (UserGroupEntity $userGroup): UserGroupAwareInterface => $entity->addUserGroup($userGroup), |
|
273
|
2 |
|
$value, |
|
274
|
2 |
|
); |
|
275
|
|
|
|
|
276
|
2 |
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|