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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 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 LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $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 = [];
264✔
32

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

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

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

47
            if (($uriVariableDefinition->getCompositeIdentifier() ?? true) && 1 < ($numIdentifiers = \count($uriVariableDefinition->getIdentifiers() ?? []))) {
108✔
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];
108✔
63
            $uriVariablesMap[$parameterName] = $uriVariableDefinition;
108✔
64
        }
65

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

71
        return $identifiers;
256✔
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