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

api-platform / core / 14035635597

24 Mar 2025 12:46PM UTC coverage: 8.513% (-7.6%) from 16.086%
14035635597

push

github

web-flow
fix(laravel): defer "filters" dependent services (#7045)

0 of 109 new or added lines in 1 file covered. (0.0%)

3078 existing lines in 187 files now uncovered.

13386 of 157241 relevant lines covered (8.51%)

22.91 hits per line

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

100.0
/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\PropertyInfo\Type;
20

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

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

48
        foreach ($uriVariables as $parameterName => $value) {
1,736✔
49
            $uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
719✔
50

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

57
            if (!$types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $properties)) {
719✔
58
                continue;
8✔
59
            }
60

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

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

75
        return $uriVariables;
1,735✔
76
    }
77

78
    private function getIdentifierTypes(string $resourceClass, array $properties): array
79
    {
80
        $types = [];
719✔
81
        foreach ($properties as $property) {
719✔
82
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
719✔
83
            foreach ($propertyMetadata->getBuiltinTypes() as $type) {
719✔
84
                $types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
713✔
85
            }
86
        }
87

88
        return $types;
719✔
89
    }
90
}
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