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

api-platform / core / 6665644319

27 Oct 2023 09:57AM UTC coverage: 37.409% (+0.1%) from 37.305%
6665644319

push

github

soyuka
Merge 3.2

125 of 125 new or added lines in 32 files covered. (100.0%)

10319 of 27584 relevant lines covered (37.41%)

20.57 hits per line

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

60.87
/src/State/UriVariablesResolverTrait.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\State;
15

16
use ApiPlatform\Api\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
17
use ApiPlatform\Metadata\Exception\InvalidIdentifierException;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\UriVariablesConverterInterface;
20
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
21

22
trait UriVariablesResolverTrait
23
{
24
    private null|LegacyUriVariablesConverterInterface|UriVariablesConverterInterface $uriVariablesConverter = null;
25

26
    /**
27
     * Resolves an operation's UriVariables to their identifiers values.
28
     */
29
    private function getOperationUriVariables(HttpOperation $operation = null, array $parameters = [], string $resourceClass = null): array
30
    {
31
        $identifiers = [];
78✔
32

33
        if (!$operation) {
78✔
34
            return $identifiers;
×
35
        }
36

37
        $uriVariablesMap = [];
78✔
38
        foreach ($operation->getUriVariables() ?? [] as $parameterName => $uriVariableDefinition) {
78✔
39
            if (!isset($parameters[$parameterName])) {
27✔
40
                if (!isset($parameters['id'])) {
3✔
41
                    throw new InvalidIdentifierException(sprintf('Parameter "%s" not found, check the identifiers configuration.', $parameterName));
3✔
42
                }
43

44
                $parameterName = 'id';
×
45
            }
46

47
            if (($uriVariableDefinition->getCompositeIdentifier() ?? true) && 1 < ($numIdentifiers = \count($uriVariableDefinition->getIdentifiers() ?? []))) {
24✔
48
                $currentIdentifiers = CompositeIdentifierParser::parse($parameters[$parameterName]);
×
49

50
                if (($foundNumIdentifiers = \count($currentIdentifiers)) !== $numIdentifiers) {
×
51
                    throw new InvalidIdentifierException(sprintf('We expected "%s" identifiers and got "%s".', $numIdentifiers, $foundNumIdentifiers));
×
52
                }
53

54
                foreach ($currentIdentifiers as $key => $value) {
×
55
                    $identifiers[$key] = $value;
×
56
                    $uriVariablesMap[$key] = $uriVariableDefinition;
×
57
                }
58

59
                continue;
×
60
            }
61

62
            $identifiers[$parameterName] = $parameters[$parameterName];
24✔
63
            $uriVariablesMap[$parameterName] = $uriVariableDefinition;
24✔
64
        }
65

66
        if ($this->uriVariablesConverter) {
75✔
67
            $context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
45✔
68
            $identifiers = $this->uriVariablesConverter->convert($identifiers, $operation->getClass() ?? $resourceClass, $context);
45✔
69
        }
70

71
        return $identifiers;
75✔
72
    }
73
}
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