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
|
15 |
|
public function __construct( |
28
|
|
|
CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater |
29
|
|
|
) |
30
|
|
|
{ |
31
|
15 |
|
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
32
|
|
|
} |
33
|
|
|
|
34
|
7 |
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
35
|
|
|
{ |
36
|
|
|
// Make sure we're not called twice |
37
|
7 |
|
if (isset($context[self::ALREADY_CALLED])) { |
38
|
5 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
return $type === 'App\Entity\User'; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
45
|
|
|
{ |
46
|
|
|
/* @var UserInterface $object */ |
47
|
|
|
|
48
|
5 |
|
$context[self::ALREADY_CALLED] = true; |
49
|
|
|
|
50
|
5 |
|
$object = $this->denormalizer->denormalize($data, $class, $format, $context); |
51
|
|
|
|
52
|
5 |
|
$canonicalFieldsUpdater = $this->canonicalFieldsUpdater; |
53
|
5 |
|
if(isset($data['username'])){ |
|
|
|
|
54
|
3 |
|
$usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']); |
55
|
3 |
|
$object->setUsernameCanonical($usernameCanonical); |
56
|
|
|
} |
57
|
5 |
|
if(isset($data['email'])){ |
|
|
|
|
58
|
3 |
|
$emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']); |
59
|
3 |
|
$object->setEmailCanonical($emailCanonical); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
return $object; |
63
|
|
|
} |
64
|
|
|
} |