/
ParameterResourceMetadataCollectionFactory.php
101 lines (84 loc) · 4.42 KB
/
ParameterResourceMetadataCollectionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Metadata\Resource\Factory;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\HeaderParameterInterface;
use ApiPlatform\Metadata\Parameters;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\OpenApi;
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
use Psr\Container\ContainerInterface;
/**
* Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
*
* @experimental
*/
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, private readonly ?ContainerInterface $filterLocator = null)
{
}
public function create(string $resourceClass): ResourceMetadataCollection
{
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
foreach ($resourceMetadataCollection as $i => $resource) {
$operations = $resource->getOperations();
$internalPriority = -1;
foreach ($operations as $operationName => $operation) {
$parameters = [];
foreach ($operation->getParameters() ?? [] as $key => $parameter) {
if (null === $parameter->getKey()) {
$parameter = $parameter->withKey($key);
}
$filter = $parameter->getFilter();
if (\is_string($filter) && $this->filterLocator->has($filter)) {
$filter = $this->filterLocator->get($filter);
}
if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
$parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
}
// Read filter description to populate the Parameter
$description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
$parameter = $parameter->withSchema($schema);
}
if (null === $parameter->getOpenApi() && $openApi = $description[$key]['openapi'] ?? null) {
if ($openApi instanceof OpenApi\Model\Parameter) {
$parameter = $parameter->withOpenApi($openApi);
}
if (\is_array($openApi)) {
$parameter->withOpenApi(new OpenApi\Model\Parameter(
$key,
$parameter instanceof HeaderParameterInterface ? 'header' : 'query',
$description[$key]['description'] ?? '',
$description[$key]['required'] ?? $openApi['required'] ?? false,
$openApi['deprecated'] ?? false,
$openApi['allowEmptyValue'] ?? true,
$schema,
$openApi['style'] ?? null,
$openApi['explode'] ?? ('array' === ($schema['type'] ?? null)),
$openApi['allowReserved'] ?? false,
$openApi['example'] ?? null,
isset($openApi['examples']
) ? new \ArrayObject($openApi['examples']) : null
));
}
}
$priority = $parameter->getPriority() ?? $internalPriority--;
$parameters[$key] = $parameter->withPriority($priority);
}
$operations->add($operationName, $operation->withParameters(new Parameters($parameters)));
}
$resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
}
return $resourceMetadataCollection;
}
}