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\ORM\Mapping\ClassMetadataInfo; |
18
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
19
|
|
|
use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider; |
20
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
22
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
23
|
|
|
use Silverback\ApiComponentsBundle\Helper\ComponentPosition\ComponentPositionSortValueHelper; |
24
|
|
|
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
25
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
26
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException; |
27
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
28
|
|
|
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; |
29
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
30
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
31
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
32
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
33
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
34
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
35
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
36
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
37
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* When creating a new component position the sort value should be set if not already explicitly set in the request. |
41
|
|
|
* |
42
|
|
|
* @author Daniel West <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class ComponentPositionNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, ContextAwareNormalizerInterface, NormalizerAwareInterface |
45
|
|
|
{ |
46
|
|
|
use DenormalizerAwareTrait; |
47
|
|
|
use NormalizerAwareTrait; |
48
|
|
|
|
49
|
|
|
private const ALREADY_CALLED = 'COMPONENT_POSITION_NORMALIZER_ALREADY_CALLED'; |
50
|
|
|
|
51
|
|
|
private PageDataProvider $pageDataProvider; |
52
|
|
|
private ComponentPositionSortValueHelper $componentPositionSortValueHelper; |
53
|
|
|
private RequestStack $requestStack; |
54
|
|
|
private PublishableStatusChecker $publishableStatusChecker; |
55
|
|
|
private ManagerRegistry $registry; |
56
|
|
|
private IriConverterInterface $iriConverter; |
57
|
|
|
|
58
|
|
|
public function __construct( |
59
|
|
|
PageDataProvider $pageDataProvider, |
60
|
|
|
ComponentPositionSortValueHelper $componentPositionSortValueHelper, |
61
|
|
|
RequestStack $requestStack, |
62
|
|
|
PublishableStatusChecker $publishableStatusChecker, |
63
|
|
|
ManagerRegistry $registry, |
64
|
|
|
IriConverterInterface $iriConverter |
65
|
|
|
) { |
66
|
|
|
$this->pageDataProvider = $pageDataProvider; |
67
|
|
|
$this->componentPositionSortValueHelper = $componentPositionSortValueHelper; |
68
|
|
|
$this->requestStack = $requestStack; |
69
|
|
|
$this->publishableStatusChecker = $publishableStatusChecker; |
70
|
|
|
$this->registry = $registry; |
71
|
|
|
$this->iriConverter = $iriConverter; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function hasCacheableSupportsMethod(): bool |
75
|
|
|
{ |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
80
|
|
|
{ |
81
|
|
|
return !isset($context[self::ALREADY_CALLED]) && ComponentPosition::class === $type; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
85
|
|
|
{ |
86
|
|
|
$context[self::ALREADY_CALLED] = true; |
87
|
|
|
|
88
|
|
|
$originalObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null; |
89
|
|
|
$originalSortValue = $originalObject ? $originalObject->sortValue : null; |
90
|
|
|
|
91
|
|
|
/** @var ComponentPosition $object */ |
92
|
|
|
$object = $this->denormalizer->denormalize($data, $type, $format, $context); |
93
|
|
|
|
94
|
|
|
$this->componentPositionSortValueHelper->calculateSortValue($object, $originalSortValue); |
95
|
|
|
|
96
|
|
|
return $object; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
100
|
|
|
{ |
101
|
|
|
return $data instanceof ComponentPosition && !isset($context[self::ALREADY_CALLED]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function normalize($object, $format = null, array $context = []) |
105
|
|
|
{ |
106
|
|
|
/* @var ComponentPosition $object */ |
107
|
|
|
/* @var mixed|null $format */ |
108
|
|
|
|
109
|
|
|
$context[self::ALREADY_CALLED] = true; |
110
|
|
|
|
111
|
|
|
$context[MetadataNormalizer::METADATA_CONTEXT]['static_component'] = $object->component ? $this->iriConverter->getIriFromItem($object->component) : null; |
112
|
|
|
$object = $this->normalizeForPageData($object); |
113
|
|
|
|
114
|
|
|
$component = $object->component; |
115
|
|
|
if ( |
116
|
|
|
$component && |
117
|
|
|
$this->publishableStatusChecker->getAnnotationReader()->isConfigured($component) && |
118
|
|
|
$this->publishableStatusChecker->isGranted($component) |
119
|
|
|
) { |
120
|
|
|
$object->setComponent($this->normalizePublishableComponent($component)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function normalizePublishableComponent(AbstractComponent $component) |
127
|
|
|
{ |
128
|
|
|
$configuration = $this->publishableStatusChecker->getAnnotationReader()->getConfiguration($type = \get_class($component)); |
129
|
|
|
$em = $this->registry->getManagerForClass(\get_class($component)); |
130
|
|
|
if (!$em) { |
131
|
|
|
throw new InvalidArgumentException(sprintf('Could not find entity manager for class %s', $type)); |
132
|
|
|
} |
133
|
|
|
/** @var ClassMetadataInfo $classMetadata */ |
134
|
|
|
$classMetadata = $em->getClassMetadata($type); |
135
|
|
|
$draft = $classMetadata->getFieldValue($component, $configuration->reverseAssociationName); |
136
|
|
|
|
137
|
|
|
return $draft ?? $component; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function normalizeForPageData(ComponentPosition $object): ComponentPosition |
141
|
|
|
{ |
142
|
|
|
if (!$object->pageDataProperty || !$this->requestStack->getCurrentRequest()) { |
143
|
|
|
return $object; |
144
|
|
|
} |
145
|
|
|
$pageData = $this->pageDataProvider->getPageData(); |
146
|
|
|
if (!$pageData) { |
147
|
|
|
return $object; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
151
|
|
|
try { |
152
|
|
|
$component = $propertyAccessor->getValue($pageData, $object->pageDataProperty); |
153
|
|
|
} catch (UnexpectedTypeException | NoSuchIndexException | NoSuchPropertyException $e) { |
154
|
|
|
return $object; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// optional to have the page data component found |
158
|
|
|
if (!$component) { |
159
|
|
|
return $object; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// it must be a component if it is found though |
163
|
|
|
if (!$component instanceof AbstractComponent) { |
164
|
|
|
throw new InvalidArgumentException(sprintf('The page data property %s is not a component', $object->pageDataProperty)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// populate the position |
168
|
|
|
$object->setComponent($component); |
169
|
|
|
|
170
|
|
|
return $object; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|