• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.76
/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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 ApiPlatform\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\FilterInterface;
18
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
19
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
20
use ApiPlatform\Metadata\Parameter;
21
use ApiPlatform\Metadata\Parameters;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
25
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
26
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
27
use Psr\Container\ContainerInterface;
28
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
29

30
/**
31
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
32
 *
33
 * @experimental
34
 */
35
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
36
{
37
    public function __construct(
38
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
39
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
40
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
41
        private readonly ?ContainerInterface $filterLocator = null,
42
        private readonly ?NameConverterInterface $nameConverter = null,
43
    ) {
44
    }
2,684✔
45

46
    public function create(string $resourceClass): ResourceMetadataCollection
47
    {
48
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
111✔
49

50
        $propertyNames = [];
111✔
51
        $properties = [];
111✔
52
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $i => $property) {
111✔
53
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
96✔
54
            if ($propertyMetadata->isReadable()) {
96✔
55
                $propertyNames[] = $property;
96✔
56
                $properties[$property] = $propertyMetadata;
96✔
57
            }
58
        }
59

60
        foreach ($resourceMetadataCollection as $i => $resource) {
111✔
61
            $operations = $resource->getOperations();
100✔
62

63
            $internalPriority = -1;
100✔
64
            foreach ($operations as $operationName => $operation) {
100✔
65
                $parameters = $operation->getParameters() ?? new Parameters();
100✔
66
                foreach ($parameters as $key => $parameter) {
100✔
67
                    if (':property' === $key) {
15✔
68
                        foreach ($propertyNames as $property) {
6✔
69
                            $converted = $this->nameConverter?->denormalize($property) ?? $property;
6✔
70
                            $propertyParameter = $this->setDefaults($converted, $parameter, $resourceClass, $properties);
6✔
71
                            $priority = $propertyParameter->getPriority() ?? $internalPriority--;
6✔
72
                            $parameters->add($converted, $propertyParameter->withPriority($priority)->withKey($converted));
6✔
73
                        }
74

75
                        $parameters->remove($key, $parameter::class);
6✔
76
                        continue;
6✔
77
                    }
78

79
                    $key = $parameter->getKey() ?? $key;
15✔
80

81
                    if (str_contains($key, ':property')) {
15✔
82
                        $p = [];
9✔
83
                        foreach ($propertyNames as $prop) {
9✔
84
                            $p[$this->nameConverter?->denormalize($prop) ?? $prop] = $prop;
6✔
85
                        }
86

87
                        $parameter = $parameter->withExtraProperties(($parameter->getExtraProperties() ?? []) + ['_properties' => $p]);
9✔
88
                    }
89

90
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties);
15✔
91
                    $priority = $parameter->getPriority() ?? $internalPriority--;
15✔
92
                    $parameters->add($key, $parameter->withPriority($priority));
15✔
93
                }
94

95
                if (\count($parameters) > 0) {
100✔
96
                    $operations->add($operationName, $operation->withParameters($parameters));
15✔
97
                }
98
            }
99

100
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
100✔
101

102
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
100✔
103
                continue;
49✔
104
            }
105

106
            $internalPriority = -1;
82✔
107
            foreach ($graphQlOperations as $operationName => $operation) {
82✔
108
                $parameters = $operation->getParameters() ?? new Parameters();
82✔
109
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
82✔
110
                    $key = $parameter->getKey() ?? $key;
6✔
111
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties);
6✔
112
                    $priority = $parameter->getPriority() ?? $internalPriority--;
6✔
113
                    $parameters->add($key, $parameter->withPriority($priority));
6✔
114
                }
115

116
                $graphQlOperations[$operationName] = $operation->withParameters($parameters);
82✔
117
            }
118

119
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
82✔
120
        }
121

122
        return $resourceMetadataCollection;
111✔
123
    }
124

125
    private function addFilterMetadata(Parameter $parameter): Parameter
126
    {
127
        if (!($filterId = $parameter->getFilter())) {
15✔
128
            return $parameter;
15✔
129
        }
130

131
        $filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
9✔
132

133
        if (!$filter) {
9✔
134
            return $parameter;
×
135
        }
136

137
        if (null === $parameter->getSchema() && $filter instanceof JsonSchemaFilterInterface) {
9✔
138
            if ($schema = $filter->getSchema($parameter)) {
6✔
139
                $parameter = $parameter->withSchema($schema);
6✔
140
            }
141
        }
142

143
        if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface) {
9✔
144
            if ($openApiParameter = $filter->getOpenApiParameters($parameter)) {
6✔
145
                $parameter = $parameter->withOpenApi($openApiParameter);
6✔
146
            }
147
        }
148

149
        return $parameter;
9✔
150
    }
151

152
    /**
153
     * @param array<string, ApiProperty> $properties
154
     */
155
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass, array $properties): Parameter
156
    {
157
        if (null === $parameter->getKey()) {
15✔
158
            $parameter = $parameter->withKey($key);
15✔
159
        }
160

161
        $filter = $parameter->getFilter();
15✔
162
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
15✔
163
            $filter = $this->filterLocator->get($filter);
9✔
164
        }
165

166
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
15✔
167
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
6✔
168
        }
169

170
        // Read filter description to populate the Parameter
171
        $description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
15✔
172
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
15✔
173
            $parameter = $parameter->withSchema($schema);
×
174
        }
175

176
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
15✔
177
            $parameter = $parameter->withProperty($property);
6✔
178
        }
179

180
        $currentKey = $key;
15✔
181
        if (null === $parameter->getProperty() && isset($properties[$key])) {
15✔
182
            $parameter = $parameter->withProperty($key);
6✔
183
        }
184

185
        if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
15✔
186
            $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
×
187
            $currentKey = $nameConvertedKey;
×
188
        }
189

190
        if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
15✔
191
            $parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
×
192
        }
193

194
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
15✔
195
            $parameter = $parameter->withRequired($required);
×
196
        }
197

198
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
15✔
199
            $parameter = $parameter->withOpenApi($openApi);
×
200
        }
201

202
        return $this->addFilterMetadata($parameter);
15✔
203
    }
204
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc