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\DataTransformer; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\DataTransformer\DataTransformerInterface; |
17
|
|
|
use ApiPlatform\Exception\InvalidIdentifierException; |
18
|
|
|
use ApiPlatform\Metadata\ApiResource; |
19
|
|
|
use ApiPlatform\Metadata\Operation; |
20
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
21
|
|
|
use ApiPlatform\Serializer\SerializerContextBuilderInterface; |
22
|
|
|
use ApiPlatform\State\ProviderInterface; |
23
|
|
|
use ApiPlatform\State\UriVariablesResolverTrait; |
24
|
|
|
use ApiPlatform\Util\AttributesExtractor; |
25
|
|
|
use ApiPlatform\Util\RequestParser; |
26
|
|
|
use Silverback\ApiComponentsBundle\Entity\Component\Collection; |
27
|
|
|
use Silverback\ApiComponentsBundle\Exception\OutOfBoundsException; |
28
|
|
|
use Silverback\ApiComponentsBundle\Serializer\SerializeFormatResolver; |
29
|
|
|
use Silverback\ApiComponentsBundle\Utility\ApiResourceRouteFinder; |
30
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Daniel West <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class CollectionOutputDataTransformer implements DataTransformerInterface |
38
|
|
|
{ |
39
|
|
|
use UriVariablesResolverTrait; |
40
|
|
|
|
41
|
|
|
private ApiResourceRouteFinder $resourceRouteFinder; |
42
|
|
|
private ProviderInterface $provider; |
43
|
|
|
private RequestStack $requestStack; |
44
|
|
|
private SerializerContextBuilderInterface $serializerContextBuilder; |
45
|
|
|
private NormalizerInterface $itemNormalizer; |
46
|
|
|
private SerializeFormatResolver $serializeFormatResolver; |
47
|
|
|
private string $itemsPerPageParameterName; |
48
|
|
|
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
ApiResourceRouteFinder $resourceRouteFinder, |
52
|
|
|
ProviderInterface $provider, |
53
|
|
|
RequestStack $requestStack, |
54
|
|
|
SerializerContextBuilderInterface $serializerContextBuilder, |
55
|
|
|
NormalizerInterface $itemNormalizer, |
56
|
|
|
SerializeFormatResolver $serializeFormatResolver, |
57
|
|
|
ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, |
58
|
|
|
string $itemsPerPageParameterName |
59
|
|
|
) { |
60
|
|
|
$this->resourceRouteFinder = $resourceRouteFinder; |
61
|
|
|
$this->provider = $provider; |
62
|
|
|
$this->requestStack = $requestStack; |
63
|
|
|
$this->serializerContextBuilder = $serializerContextBuilder; |
64
|
|
|
$this->itemNormalizer = $itemNormalizer; |
65
|
|
|
$this->serializeFormatResolver = $serializeFormatResolver; |
66
|
|
|
$this->itemsPerPageParameterName = $itemsPerPageParameterName; |
67
|
|
|
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function supportsTransformation($data, string $to, array $context = []): bool |
71
|
|
|
{ |
72
|
|
|
return $data instanceof Collection && Collection::class === $to; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param object|Collection $object |
77
|
|
|
*/ |
78
|
|
|
public function transform($object, string $to, array $context = []): Collection |
79
|
|
|
{ |
80
|
|
|
$parameters = $this->resourceRouteFinder->findByIri($object->getResourceIri()); |
|
|
|
|
81
|
|
|
$attributes = AttributesExtractor::extractAttributes($parameters); |
82
|
|
|
$request = $this->requestStack->getMainRequest(); |
83
|
|
|
if (!$request) { |
84
|
|
|
return $object; |
85
|
|
|
} |
86
|
|
|
// Fetch the collection with computed context |
87
|
|
|
$resourceClass = $attributes['resource_class']; |
88
|
|
|
|
89
|
|
|
$getCollectionOperation = $this->findGetCollectionOperation($resourceClass); |
90
|
|
|
if (!$getCollectionOperation) { |
91
|
|
|
return $object; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Build context |
95
|
|
|
$collectionContext = ['operation' => $getCollectionOperation]; |
96
|
|
|
|
97
|
|
|
// Build filters |
98
|
|
|
$filters = []; |
99
|
|
|
if (($perPage = $object->getPerPage()) !== null) { |
100
|
|
|
$filters[$this->itemsPerPageParameterName] = $perPage; |
101
|
|
|
} |
102
|
|
|
if (($defaultQueryParams = $object->getDefaultQueryParameters()) !== null) { |
103
|
|
|
$filters += $defaultQueryParams; |
104
|
|
|
} |
105
|
|
|
if (null === $requestFilters = $request->attributes->get('_api_filters')) { |
106
|
|
|
$queryString = RequestParser::getQueryString($request); |
107
|
|
|
$requestFilters = $queryString ? RequestParser::parseRequestParams($queryString) : null; |
108
|
|
|
} |
109
|
|
|
if ($requestFilters) { |
110
|
|
|
// not += because we want to overwrite with an empty string if provided in querystring. |
111
|
|
|
// e.g. a default search value could be overridden by no search value |
112
|
|
|
$filters = array_merge($filters, $requestFilters); |
113
|
|
|
} |
114
|
|
|
$collectionContext['filters'] = $filters; |
115
|
|
|
|
116
|
|
|
// Compose context for provider |
117
|
|
|
$collectionContext += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes); |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
$uriVariables = $this->getOperationUriVariables($getCollectionOperation, $parameters, $resourceClass); |
121
|
|
|
$collectionData = $this->provider->provide($resourceClass, $uriVariables, $getCollectionOperation->getName(), $collectionContext); |
|
|
|
|
122
|
|
|
} catch (InvalidIdentifierException $e) { |
123
|
|
|
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Normalize the collection into an array |
127
|
|
|
// Pagination disabled |
128
|
|
|
if (\is_array($collectionData)) { |
129
|
|
|
$collection = $collectionData; |
130
|
|
|
} else { |
131
|
|
|
if (!$collectionData instanceof \Traversable) { |
132
|
|
|
throw new OutOfBoundsException('$collectionData should be Traversable'); |
133
|
|
|
} |
134
|
|
|
$collection = iterator_count($collectionData) ? $collectionData : []; |
135
|
|
|
} |
136
|
|
|
$format = $this->serializeFormatResolver->getFormatFromRequest($request); |
137
|
|
|
$normalizedCollection = $this->itemNormalizer->normalize($collection, $format, $normalizationContext); |
138
|
|
|
|
139
|
|
|
// Update the original collection resource |
140
|
|
|
$object->setCollection($normalizedCollection); |
141
|
|
|
|
142
|
|
|
return $object; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function findGetCollectionOperation(string $resourceClass): ?Operation |
146
|
|
|
{ |
147
|
|
|
$metadata = $this->resourceMetadataCollectionFactory->create($resourceClass); |
148
|
|
|
$it = $metadata->getIterator(); |
149
|
|
|
/** @var ApiResource $apiResource */ |
150
|
|
|
foreach ($it as $apiResource) { |
151
|
|
|
$operations = $apiResource->getOperations(); |
152
|
|
|
if ($operations) { |
153
|
|
|
/** @var Operation $operation */ |
154
|
|
|
foreach ($operations as $operation) { |
155
|
|
|
if ($operation->isCollection() && Operation::METHOD_GET === $operation->getMethod()) { |
|
|
|
|
156
|
|
|
return $operation; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|