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