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\Metadata\Util\ClassInfoTrait; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider; |
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\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 PersistedNormalizer implements NormalizerInterface, NormalizerAwareInterface |
31
|
|
|
{ |
32
|
|
|
use ClassInfoTrait; |
33
|
|
|
use NormalizerAwareTrait; |
34
|
|
|
|
35
|
|
|
private const ALREADY_CALLED = 'PERSISTED_NORMALIZER_ALREADY_CALLED'; |
36
|
|
|
|
37
|
|
|
private PropertyAccessor $propertyAccessor; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
6 |
|
private EntityManagerInterface $entityManager, |
41
|
|
|
private ResourceClassResolverInterface $resourceClassResolver, |
42
|
|
|
private ResourceMetadataProvider $resourceMetadataProvider |
43
|
|
|
) { |
44
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
48
|
2 |
|
{ |
49
|
|
|
$context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id'); |
50
|
2 |
|
|
51
|
|
|
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object); |
52
|
2 |
|
$resourceMetadata->setPersisted($this->entityManager->contains($object)); |
53
|
2 |
|
|
54
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
public function supportsNormalization($data, $format = null, $context = []): bool |
58
|
3 |
|
{ |
59
|
|
|
if (!\is_object($data) || $data instanceof \Traversable) { |
60
|
3 |
|
return false; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
64
|
3 |
|
$context[self::ALREADY_CALLED] = []; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$id = $this->propertyAccessor->getValue($data, 'id'); |
69
|
3 |
|
} catch (NoSuchPropertyException $e) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return !\in_array($id, $context[self::ALREADY_CALLED], true) && |
74
|
3 |
|
$this->resourceClassResolver->isResourceClass($this->getObjectClass($data)); |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
78
|
1 |
|
{ |
79
|
|
|
return [ |
80
|
1 |
|
// 'object' => null, // Doesn't support any classes or interfaces |
81
|
|
|
'*' => false, // Supports any other types, but the result is not cacheable |
82
|
|
|
// MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.