1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
6
|
|
|
|
7
|
|
|
use Silverback\ApiComponentBundle\DataTransformer\DataTransformerInterface; |
8
|
|
|
use Silverback\ApiComponentBundle\DataTransformer\SortableDataTransformerInterface; |
9
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent; |
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Page\Dynamic\DynamicContent; |
12
|
|
|
use Silverback\ApiComponentBundle\Security\RestrictedResourceVoter; |
13
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
14
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
19
|
|
|
|
20
|
|
|
class ApiNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface |
21
|
|
|
{ |
22
|
|
|
use NormalizerAwareTrait; |
23
|
|
|
|
24
|
|
|
private const ALREADY_CALLED = 'API_COMPONENT_BUNDLE_NORMALIZER_ALREADY_CALLED'; |
25
|
|
|
|
26
|
|
|
/** @var iterable|DataTransformerInterface[] */ |
27
|
|
|
private $dataTransformers; |
28
|
|
|
|
29
|
|
|
/** @var DataTransformerInterface[] */ |
30
|
|
|
private $supportedTransformers = []; |
31
|
|
|
|
32
|
|
|
private $propertyAccessor; |
33
|
|
|
private $restrictedResourceVoter; |
34
|
|
|
|
35
|
|
|
public function __construct(iterable $dataTransformers = [], RestrictedResourceVoter $restrictedResourceVoter) |
36
|
|
|
{ |
37
|
|
|
$this->dataTransformers = $dataTransformers; |
38
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
39
|
|
|
$this->restrictedResourceVoter = $restrictedResourceVoter; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getId($object) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
return $this->propertyAccessor->getValue($object, 'id'); |
46
|
|
|
} catch (NoSuchPropertyException $e) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
52
|
|
|
{ |
53
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
54
|
|
|
$context[self::ALREADY_CALLED] = []; |
55
|
|
|
} |
56
|
|
|
$this->supportedTransformers = []; |
57
|
|
|
if (!is_object($data) || in_array($this->getId($data), $context[self::ALREADY_CALLED], true)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($this->dataTransformers as $transformer) { |
62
|
|
|
if ($transformer->supportsTransformation($data)) { |
63
|
|
|
$this->supportedTransformers[] = $transformer; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$sortedTransformers = []; |
68
|
|
|
foreach ($this->supportedTransformers as $transformer) { |
69
|
|
|
if ($transformer instanceof SortableDataTransformerInterface) { |
70
|
|
|
$sortedTransformers[] = [ |
71
|
|
|
$transformer, |
72
|
|
|
$transformer->getSortOrder() |
73
|
|
|
]; |
74
|
|
|
} else { |
75
|
|
|
$sortedTransformers[] = [ |
76
|
|
|
$transformer, |
77
|
|
|
0 |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
usort($sortedTransformers, static function($a, $b) { |
82
|
|
|
if ($a[1] === $b[1]) { |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
return ($a[1] < $b[1]) ? -1 : 1; |
86
|
|
|
}); |
87
|
|
|
$sortedTransformers = array_map(static function ($t) { |
88
|
|
|
return $t[0]; |
89
|
|
|
}, $sortedTransformers); |
90
|
|
|
$this->supportedTransformers = $sortedTransformers; |
91
|
|
|
|
92
|
|
|
if ($data instanceof AbstractComponent) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->restrictedResourceVoter->isSupported($data)) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return !empty($this->supportedTransformers); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function normalize($object, $format = null, array $context = []) |
104
|
|
|
{ |
105
|
|
|
if (!$this->restrictedResourceVoter->vote($object)) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
$context[self::ALREADY_CALLED][] = $this->getId($object); |
109
|
|
|
if ($object instanceof AbstractComponent || $object instanceof DynamicContent || $object instanceof AbstractContent) { |
110
|
|
|
$context['groups'] = array_map(static function($grp) { |
111
|
|
|
if (strpos($grp, 'route') === 0) { |
112
|
|
|
return str_replace('route', 'component', $grp); |
113
|
|
|
} |
114
|
|
|
return $grp; |
115
|
|
|
}, $context['groups']); |
116
|
|
|
} |
117
|
|
|
foreach ($this->supportedTransformers as $transformer) { |
118
|
|
|
$transformer->transform($object, $context); |
119
|
|
|
} |
120
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function hasCacheableSupportsMethod(): bool |
124
|
|
|
{ |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|