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 ApiPlatform\Api\ResourceClassResolverInterface; |
17
|
|
|
use ApiPlatform\Util\ClassInfoTrait; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataInterface; |
20
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
21
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
22
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
24
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
25
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
26
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
27
|
|
|
use Traversable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Daniel West <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class PersistedNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use ClassInfoTrait; |
35
|
|
|
use NormalizerAwareTrait; |
36
|
|
|
|
37
|
|
|
private const ALREADY_CALLED = 'PERSISTED_NORMALIZER_ALREADY_CALLED'; |
38
|
|
|
|
39
|
|
|
private PropertyAccessor $propertyAccessor; |
40
|
|
|
|
41
|
6 |
|
public function __construct( |
42
|
|
|
private EntityManagerInterface $entityManager, |
43
|
|
|
private ResourceClassResolverInterface $resourceClassResolver, |
44
|
|
|
private ResourceMetadataInterface $resourceMetadata |
45
|
|
|
) { |
46
|
6 |
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
50
|
|
|
{ |
51
|
2 |
|
$context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id'); |
52
|
2 |
|
$this->resourceMetadata->setPersisted($this->entityManager->contains($object)); |
53
|
|
|
|
54
|
2 |
|
return $this->normalizer->normalize($object, $format, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function supportsNormalization($data, $format = null, $context = []): bool |
58
|
|
|
{ |
59
|
3 |
|
if (!\is_object($data) || $data instanceof Traversable) { |
60
|
1 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
if (!isset($context[self::ALREADY_CALLED])) { |
64
|
2 |
|
$context[self::ALREADY_CALLED] = []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
3 |
|
$id = $this->propertyAccessor->getValue($data, 'id'); |
69
|
|
|
} catch (NoSuchPropertyException $e) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
return !\in_array($id, $context[self::ALREADY_CALLED], true) && |
74
|
3 |
|
$this->resourceClassResolver->isResourceClass($this->getObjectClass($data)); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function hasCacheableSupportsMethod(): bool |
78
|
|
|
{ |
79
|
1 |
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|