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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

96.43
/src/Metadata/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\Metadata;
15

16
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
17
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
20
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
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
    }
654✔
35

36
    /**
37
     * {@inheritdoc}
38
     *
39
     * To handle the composite identifiers type correctly, use an `uri_variables_map` that maps uriVariables to their uriVariablesDefinition.
40
     * Indeed, a composite identifier will already be parsed, and their corresponding properties will be the parameterName and not the defined
41
     * identifiers.
42
     */
43
    public function convert(array $uriVariables, string $class, array $context = []): array
44
    {
45
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation();
600✔
46
        $context += ['operation' => $operation];
600✔
47
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
600✔
48

49
        foreach ($uriVariables as $parameterName => $value) {
600✔
50
            $uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
166✔
51

52
            // When a composite identifier is used, we assume that the parameterName is the property to find our type
53
            $properties = $uriVariableDefinition->getIdentifiers() ?? [$parameterName];
166✔
54
            if ($uriVariableDefinition->getCompositeIdentifier()) {
166✔
55
                $properties = [$parameterName];
×
56
            }
57

58
            if (!$types = $this->getIdentifierTypeStrings($uriVariableDefinition->getFromClass() ?? $class, $properties)) {
166✔
59
                continue;
2✔
60
            }
61

62
            foreach ($this->uriVariableTransformers as $uriVariableTransformer) {
164✔
63
                if (!$uriVariableTransformer->supportsTransformation($value, $types, $context)) {
164✔
64
                    continue;
164✔
65
                }
66

67
                try {
68
                    $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context);
126✔
69
                    break;
122✔
70
                } catch (InvalidUriVariableException $e) {
4✔
71
                    throw new InvalidUriVariableException(\sprintf('Identifier "%s" could not be transformed.', $parameterName), $e->getCode(), $e);
4✔
72
                }
73
            }
74
        }
75

76
        return $uriVariables;
598✔
77
    }
78

79
    /**
80
     * @return list<string>
81
     */
82
    private function getIdentifierTypeStrings(string $resourceClass, array $properties): array
83
    {
84
        $typeStrings = [];
166✔
85

86
        foreach ($properties as $property) {
166✔
87
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
166✔
88

89
            if (!$type = $propertyMetadata->getNativeType()) {
166✔
90
                continue;
2✔
91
            }
92

93
            foreach ($type->traverse() as $t) {
164✔
94
                if (!$t instanceof CompositeTypeInterface && !$t instanceof WrappingTypeInterface) {
164✔
95
                    $typeStrings[] = (string) $t;
164✔
96
                }
97
            }
98
        }
99

100
        return $typeStrings;
166✔
101
    }
102
}
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