|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\User\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
|
6
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
7
|
|
|
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; |
|
8
|
|
|
use VSV\GVQ_API\User\Models\User; |
|
9
|
|
|
use VSV\GVQ_API\User\ValueObjects\Email; |
|
10
|
|
|
use VSV\GVQ_API\User\ValueObjects\Password; |
|
11
|
|
|
use VSV\GVQ_API\User\ValueObjects\Role; |
|
12
|
|
|
|
|
13
|
|
|
class UserDenormalizer implements DenormalizerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @inheritdoc |
|
17
|
|
|
*/ |
|
18
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()): User |
|
19
|
|
|
{ |
|
20
|
|
|
// TODO: Better to use decorator and inject uuid generator. |
|
21
|
|
|
if (!isset($data['id'])) { |
|
22
|
|
|
$data['id'] = Uuid::uuid4(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// TODO: Better solution for setting the role. |
|
26
|
|
|
$user = new User( |
|
27
|
|
|
Uuid::fromString($data['id']), |
|
28
|
|
|
new Email($data['email']), |
|
29
|
|
|
new NotEmptyString($data['lastName']), |
|
30
|
|
|
new NotEmptyString($data['firstName']), |
|
31
|
|
|
isset($context['role']) ? new Role($context['role']) : new Role($data['role']) |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
if (isset($data['password'])) { |
|
35
|
|
|
$user = $user->withPassword(Password::fromPlainText($data['password'])); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $user; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function supportsDenormalization($data, $type, $format = null): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return ($type === User::class) && ($format === 'json'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|