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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.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\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
    {
UNCOV
34
    }
×
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
    {
UNCOV
45
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation();
×
UNCOV
46
        $context += ['operation' => $operation];
×
UNCOV
47
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
×
48

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

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

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

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

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

UNCOV
76
        return $uriVariables;
×
77
    }
78

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

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

UNCOV
89
            if (!$type = $propertyMetadata->getNativeType()) {
×
UNCOV
90
                continue;
×
91
            }
92

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

UNCOV
100
        return $typeStrings;
×
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