|
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 Doctrine\Persistence\ManagerRegistry; |
|
17
|
|
|
use Silverback\ApiComponentsBundle\AttributeReader\TimestampedAttributeReader; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\Helper\Timestamped\TimestampedDataPersister; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
|
20
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
21
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
22
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
|
23
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Daniel West <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class TimestampedNormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use ClassMetadataTrait; |
|
31
|
|
|
|
|
32
|
|
|
use DenormalizerAwareTrait; |
|
33
|
|
|
|
|
34
|
|
|
private const ALREADY_CALLED = 'TIMESTAMPED_NORMALIZER_ALREADY_CALLED'; |
|
35
|
|
|
|
|
36
|
|
|
private TimestampedAttributeReader $annotationReader; |
|
37
|
|
|
private TimestampedDataPersister $timestampedDataPersister; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(ManagerRegistry $registry, TimestampedAttributeReader $annotationReader, TimestampedDataPersister $timestampedDataPersister) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->initRegistry($registry); |
|
42
|
|
|
$this->annotationReader = $annotationReader; |
|
43
|
|
|
$this->timestampedDataPersister = $timestampedDataPersister; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
|
47
|
|
|
{ |
|
48
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
|
49
|
|
|
$context[self::ALREADY_CALLED] = []; |
|
50
|
|
|
} |
|
51
|
|
|
$id = $type; |
|
52
|
|
|
|
|
53
|
|
|
return !\in_array($id, $context[self::ALREADY_CALLED], true) && $this->annotationReader->isConfigured($type); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function denormalize($data, $type, $format = null, array $context = []): mixed |
|
57
|
|
|
{ |
|
58
|
|
|
$context[self::ALREADY_CALLED][] = $type; |
|
59
|
|
|
|
|
60
|
|
|
$isNew = !isset($context[AbstractNormalizer::OBJECT_TO_POPULATE]); |
|
61
|
|
|
|
|
62
|
|
|
$object = $this->denormalizer->denormalize($data, $type, $format, $context); |
|
63
|
|
|
$this->timestampedDataPersister->persistTimestampedFields($object, $isNew); |
|
64
|
|
|
|
|
65
|
|
|
return $object; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// TODO: is this only for objects? |
|
69
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
return ['*' => false]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.