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\AnnotationReader\TimestampedAnnotationReader; |
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\CacheableSupportsMethodInterface; |
22
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
24
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Daniel West <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class TimestampedNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface |
30
|
|
|
{ |
31
|
|
|
use ClassMetadataTrait; |
32
|
|
|
|
33
|
|
|
use DenormalizerAwareTrait; |
34
|
|
|
|
35
|
|
|
private const ALREADY_CALLED = 'TIMESTAMPED_NORMALIZER_ALREADY_CALLED'; |
36
|
|
|
|
37
|
|
|
private TimestampedAnnotationReader $annotationReader; |
38
|
|
|
private TimestampedDataPersister $timestampedDataPersister; |
39
|
|
|
|
40
|
|
|
public function __construct(ManagerRegistry $registry, TimestampedAnnotationReader $annotationReader, TimestampedDataPersister $timestampedDataPersister) |
41
|
|
|
{ |
42
|
|
|
$this->initRegistry($registry); |
43
|
|
|
$this->annotationReader = $annotationReader; |
44
|
|
|
$this->timestampedDataPersister = $timestampedDataPersister; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function hasCacheableSupportsMethod(): bool |
48
|
|
|
{ |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
53
|
|
|
{ |
54
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $this->annotationReader->isConfigured($type); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
58
|
|
|
{ |
59
|
|
|
$context[self::ALREADY_CALLED] = true; |
60
|
|
|
|
61
|
|
|
$isNew = !isset($context[AbstractNormalizer::OBJECT_TO_POPULATE]); |
62
|
|
|
|
63
|
|
|
$object = $this->denormalizer->denormalize($data, $type, $format, $context); |
64
|
|
|
$this->timestampedDataPersister->persistTimestampedFields($object, $isNew); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $object; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|