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\IriConverterInterface; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
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
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Daniel West <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class PageDataNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface |
31
|
|
|
{ |
32
|
|
|
use NormalizerAwareTrait; |
33
|
|
|
|
34
|
|
|
private const ALREADY_CALLED = 'PAGE_DATA_NORMALIZER_ALREADY_CALLED'; |
35
|
|
|
|
36
|
|
|
private ManagerRegistry $registry; |
37
|
|
|
private IriConverterInterface $iriConverter; |
38
|
|
|
private PropertyAccessor $propertyAccessor; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
ManagerRegistry $registry, |
42
|
|
|
IriConverterInterface $iriConverter, |
43
|
|
|
) { |
44
|
|
|
$this->registry = $registry; |
45
|
|
|
$this->iriConverter = $iriConverter; |
46
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function normalize($object, $format = null, array $context = []) |
50
|
|
|
{ |
51
|
|
|
$context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id'); |
52
|
|
|
$context[MetadataNormalizer::METADATA_CONTEXT]['page_data_props'] = $this->getPageDataProps($object); |
53
|
|
|
|
54
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getPageDataProps(AbstractPageData $data): array |
58
|
|
|
{ |
59
|
|
|
$abstractRefl = new \ReflectionClass(AbstractPageData::class); |
60
|
|
|
$reflProps = $abstractRefl->getProperties(); |
61
|
|
|
$abstractProps = array_map(static function (\ReflectionProperty $prop) { |
62
|
|
|
return $prop->name; |
63
|
|
|
}, $reflProps); |
64
|
|
|
|
65
|
|
|
$resourceClass = \get_class($data); |
66
|
|
|
$manager = $this->registry->getManagerForClass($resourceClass); |
67
|
|
|
if (!$manager) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
$classMetadata = $manager->getClassMetadata($resourceClass); |
71
|
|
|
$assocFields = array_filter($classMetadata->getAssociationNames(), static function ($name) use ($abstractProps) { |
72
|
|
|
return !\in_array($name, $abstractProps, true); |
73
|
|
|
}); |
74
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
75
|
|
|
$props = []; |
76
|
|
|
foreach ($assocFields as $assocField) { |
77
|
|
|
$assocData = $propertyAccessor->getValue($data, $assocField); |
78
|
|
|
if (!$assocData) { |
79
|
|
|
$resourceClass = $classMetadata->getAssociationTargetClass($assocField); |
80
|
|
|
$props[$assocField] = $this->iriConverter->getIriFromResourceClass($resourceClass); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $props; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function supportsNormalization($data, $format = null, $context = []): bool |
88
|
|
|
{ |
89
|
|
|
if (!\is_object($data) || $data instanceof \Traversable) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
93
|
|
|
$context[self::ALREADY_CALLED] = []; |
94
|
|
|
} |
95
|
|
|
try { |
96
|
|
|
$id = $this->propertyAccessor->getValue($data, 'id'); |
97
|
|
|
} catch (NoSuchPropertyException $e) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return !\in_array($id, $context[self::ALREADY_CALLED], true) && |
102
|
|
|
is_a($data, AbstractPageData::class); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function hasCacheableSupportsMethod(): bool |
106
|
|
|
{ |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|