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

api-platform / core / 15256141101

26 May 2025 02:16PM UTC coverage: 21.714% (-4.8%) from 26.526%
15256141101

push

github

web-flow
Merge 4.1

6 of 387 new or added lines in 28 files covered. (1.55%)

1891 existing lines in 117 files now uncovered.

11118 of 51201 relevant lines covered (21.71%)

29.33 hits per line

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

65.22
/src/State/Provider/ParameterProvider.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\Provider;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\State\Exception\ParameterNotSupportedException;
19
use ApiPlatform\State\Exception\ProviderNotFoundException;
20
use ApiPlatform\State\ParameterNotFound;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\ParameterParserTrait;
23
use ApiPlatform\State\Util\RequestParser;
24
use Psr\Container\ContainerInterface;
25

26
/**
27
 * Loops over parameters to:
28
 *   - compute its values set as extra properties from the Parameter object (`_api_values`)
29
 *   - call the Parameter::provider if any and updates the Operation
30
 *
31
 * @experimental
32
 */
33
final class ParameterProvider implements ProviderInterface
34
{
35
    use ParameterParserTrait;
36

37
    public function __construct(private readonly ?ProviderInterface $decorated = null, private readonly ?ContainerInterface $locator = null)
38
    {
39
    }
855✔
40

41
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
42
    {
43
        $request = $context['request'] ?? null;
831✔
44
        if ($request && null === $request->attributes->get('_api_query_parameters')) {
831✔
45
            $queryString = RequestParser::getQueryString($request);
710✔
46
            $request->attributes->set('_api_query_parameters', $queryString ? RequestParser::parseRequestParams($queryString) : []);
710✔
47
        }
48

49
        if ($request && null === $request->attributes->get('_api_header_parameters')) {
831✔
50
            $request->attributes->set('_api_header_parameters', $request->headers->all());
710✔
51
        }
52

53
        $parameters = $operation->getParameters();
831✔
54

55
        if ($operation instanceof HttpOperation && true === $operation->getStrictQueryParameterValidation()) {
831✔
UNCOV
56
            $keys = [];
×
UNCOV
57
            foreach ($parameters as $parameter) {
×
UNCOV
58
                $keys[] = $parameter->getKey();
×
59
            }
60

UNCOV
61
            foreach (array_keys($request->attributes->get('_api_query_parameters')) as $key) {
×
UNCOV
62
                if (!\in_array($key, $keys, true)) {
×
UNCOV
63
                    throw new ParameterNotSupportedException($key);
×
64
                }
65
            }
66
        }
67

68
        foreach ($parameters ?? [] as $parameter) {
831✔
69
            $extraProperties = $parameter->getExtraProperties();
251✔
70
            unset($extraProperties['_api_values']);
251✔
71
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties($extraProperties));
251✔
72

73
            $context = ['operation' => $operation] + $context;
251✔
74
            $values = $this->getParameterValues($parameter, $request, $context);
251✔
75
            $value = $this->extractParameterValues($parameter, $values);
251✔
76

77
            if (($default = $parameter->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
251✔
78
                $value = $default;
160✔
79
            }
80

81
            if ($value instanceof ParameterNotFound) {
251✔
82
                continue;
247✔
83
            }
84

85
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
218✔
86
                $parameter->getExtraProperties() + ['_api_values' => $value]
218✔
87
            ));
218✔
88

89
            if (null === ($provider = $parameter->getProvider())) {
218✔
90
                continue;
218✔
91
            }
92

UNCOV
93
            if (\is_callable($provider)) {
×
UNCOV
94
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
×
UNCOV
95
                    $operation = $op;
×
96
                }
97

UNCOV
98
                continue;
×
99
            }
100

UNCOV
101
            if (\is_string($provider)) {
×
UNCOV
102
                if (!$this->locator->has($provider)) {
×
103
                    throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
104
                }
105

UNCOV
106
                $provider = $this->locator->get($provider);
×
107
            }
108

UNCOV
109
            if (($op = $provider->provide($parameter, $values, $context)) instanceof Operation) {
×
UNCOV
110
                $operation = $op;
×
111
            }
112
        }
113

114
        if ($parameters) {
831✔
115
            $operation = $operation->withParameters($parameters);
251✔
116
        }
117
        $request?->attributes->set('_api_operation', $operation);
831✔
118
        $context['operation'] = $operation;
831✔
119

120
        return $this->decorated?->provide($operation, $uriVariables, $context);
831✔
121
    }
122
}
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