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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 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
    }
520✔
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();
469✔
46
        $context += ['operation' => $operation];
469✔
47
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
469✔
48

49
        foreach ($uriVariables as $parameterName => $value) {
469✔
50
            $uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
146✔
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];
146✔
54
            if ($uriVariableDefinition->getCompositeIdentifier()) {
146✔
55
                $properties = [$parameterName];
×
56
            }
57

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

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

67
                try {
68
                    $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context);
112✔
69
                    break;
108✔
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;
467✔
77
    }
78

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

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

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

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

100
        return $typeStrings;
146✔
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