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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

86.36
/src/Api/UriVariablesConverter.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\Api;
15

16
use ApiPlatform\Exception\InvalidUriVariableException;
17
use ApiPlatform\Metadata\Link;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use Symfony\Component\PropertyInfo\Type;
21

22
/**
23
 * UriVariables converter that chains uri variables transformers.
24
 *
25
 * @author Antoine Bluchet <soyuka@gmail.com>
26
 */
27
final class UriVariablesConverter implements UriVariablesConverterInterface
28
{
29
    /**
30
     * @param iterable<UriVariableTransformerInterface> $uriVariableTransformers
31
     */
32
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly iterable $uriVariableTransformers)
33
    {
34
    }
26✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function convert(array $uriVariables, string $class, array $context = []): array
40
    {
41
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation();
13✔
42
        $context += ['operation' => $operation];
13✔
43
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
13✔
44

45
        foreach ($uriVariables as $parameterName => $value) {
13✔
46
            $uriVariableDefinition = $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
3✔
47
            if ([] === $types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName])) {
3✔
48
                continue;
×
49
            }
50

51
            foreach ($this->uriVariableTransformers as $uriVariableTransformer) {
3✔
52
                if (!$uriVariableTransformer->supportsTransformation($value, $types, $context)) {
3✔
53
                    continue;
3✔
54
                }
55

56
                try {
57
                    $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context);
2✔
58
                    break;
2✔
59
                } catch (InvalidUriVariableException $e) {
×
60
                    throw new InvalidUriVariableException(sprintf('Identifier "%s" could not be transformed.', $parameterName), $e->getCode(), $e);
×
61
                }
62
            }
63
        }
64

65
        return $uriVariables;
13✔
66
    }
67

68
    private function getIdentifierTypes(string $resourceClass, array $properties): array
69
    {
70
        $types = [];
3✔
71
        foreach ($properties as $property) {
3✔
72
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
3✔
73
            foreach ($propertyMetadata->getBuiltinTypes() as $type) {
3✔
74
                $types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
3✔
75
            }
76
        }
77

78
        return $types;
3✔
79
    }
80
}
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

© 2026 Coveralls, Inc