|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Doyo\UserBundle\Bridge\ApiPlatform; |
|
5
|
|
|
|
|
6
|
|
|
use Doyo\UserBundle\Model\UserInterface; |
|
7
|
|
|
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
|
9
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
10
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
class UserNormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use DenormalizerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
private const ALREADY_CALLED = 'USER_NORMALIZER'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var CanonicalFieldsUpdaterInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $canonicalFieldsUpdater; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* UserNormalizer constructor. |
|
25
|
|
|
* @param CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __construct( |
|
28
|
|
|
CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater |
|
29
|
|
|
) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
|
35
|
|
|
{ |
|
36
|
|
|
// Make sure we're not called twice |
|
37
|
2 |
|
if (isset($context[self::ALREADY_CALLED])) { |
|
38
|
2 |
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
return $type === 'App\Entity\User'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
45
|
|
|
{ |
|
46
|
|
|
/* @var UserInterface $object */ |
|
47
|
|
|
|
|
48
|
2 |
|
$context[self::ALREADY_CALLED] = true; |
|
49
|
|
|
|
|
50
|
2 |
|
$object = $this->denormalizer->denormalize($data, $class, $format, $context); |
|
51
|
|
|
|
|
52
|
2 |
|
$canonicalFieldsUpdater = $this->canonicalFieldsUpdater; |
|
53
|
2 |
|
if(isset($data['username'])){ |
|
|
|
|
|
|
54
|
2 |
|
$usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']); |
|
55
|
2 |
|
$object->setUsernameCanonical($usernameCanonical); |
|
56
|
|
|
} |
|
57
|
2 |
|
if(isset($data['email'])){ |
|
|
|
|
|
|
58
|
2 |
|
$emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']); |
|
59
|
2 |
|
$object->setEmailCanonical($emailCanonical); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
return $object; |
|
63
|
|
|
} |
|
64
|
|
|
} |