1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DoyoUserBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\UserBundle\Bridge\ApiPlatform; |
15
|
|
|
|
16
|
|
|
use Doyo\UserBundle\Model\UserInterface; |
17
|
|
|
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
21
|
|
|
|
22
|
|
|
class UserNormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface |
23
|
|
|
{ |
24
|
|
|
use DenormalizerAwareTrait; |
25
|
|
|
|
26
|
|
|
private const ALREADY_CALLED = 'USER_NORMALIZER'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CanonicalFieldsUpdaterInterface |
30
|
|
|
*/ |
31
|
|
|
private $canonicalFieldsUpdater; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* UserNormalizer constructor. |
35
|
|
|
*/ |
36
|
15 |
|
public function __construct( |
37
|
|
|
CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater |
38
|
|
|
) { |
39
|
15 |
|
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
43
|
|
|
{ |
44
|
|
|
// Make sure we're not called twice |
45
|
7 |
|
if (isset($context[self::ALREADY_CALLED])) { |
46
|
5 |
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
7 |
|
return 'App\Entity\User' === $type; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
53
|
|
|
{ |
54
|
|
|
/* @var UserInterface $object */ |
55
|
|
|
|
56
|
5 |
|
$context[self::ALREADY_CALLED] = true; |
57
|
|
|
|
58
|
5 |
|
$object = $this->denormalizer->denormalize($data, $class, $format, $context); |
59
|
|
|
|
60
|
5 |
|
$canonicalFieldsUpdater = $this->canonicalFieldsUpdater; |
61
|
5 |
|
if (isset($data['username'])) { |
62
|
3 |
|
$usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']); |
63
|
3 |
|
$object->setUsernameCanonical($usernameCanonical); |
64
|
|
|
} |
65
|
5 |
|
if (isset($data['email'])) { |
66
|
3 |
|
$emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']); |
67
|
3 |
|
$object->setEmailCanonical($emailCanonical); |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
return $object; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|