|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[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 Silverback\ApiComponentsBundle\Serializer\Normalizer; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser; |
|
17
|
|
|
use Silverback\ApiComponentsBundle\Helper\User\UserDataProcessor; |
|
18
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
19
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
20
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
|
21
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
22
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
|
23
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
|
24
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
|
25
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Daniel West <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class UserNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use DenormalizerAwareTrait; |
|
33
|
|
|
use NormalizerAwareTrait; |
|
34
|
|
|
|
|
35
|
|
|
private const ALREADY_CALLED = 'USER_NORMALIZER_ALREADY_CALLED'; |
|
36
|
|
|
|
|
37
|
|
|
private UserDataProcessor $userDataProcessor; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(UserDataProcessor $userDataProcessor) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->userDataProcessor = $userDataProcessor; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function hasCacheableSupportsMethod(): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
|
50
|
|
|
{ |
|
51
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
57
|
|
|
|
|
58
|
|
|
/** @var AbstractUser $object */ |
|
59
|
|
|
$object = $this->denormalizer->denormalize($data, $type, $format, $context); |
|
60
|
|
|
/** @var AbstractUser $oldObject */ |
|
61
|
|
|
$oldObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null; |
|
62
|
|
|
|
|
63
|
|
|
$this->userDataProcessor->processChanges($object, $oldObject); |
|
64
|
|
|
|
|
65
|
|
|
return $object; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function normalize($object, $format = null, array $context = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
76
|
|
|
|
|
77
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|