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

api-platform / core / 13921115004

18 Mar 2025 10:36AM UTC coverage: 61.973% (+0.002%) from 61.971%
13921115004

Pull #7032

github

web-flow
Merge ea56207a5 into 7c52f34a0
Pull Request #7032: fix: allow parameter provider as object

4 of 5 new or added lines in 1 file covered. (80.0%)

11478 of 18521 relevant lines covered (61.97%)

68.32 hits per line

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

97.44
/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\Operation;
17
use ApiPlatform\State\Exception\ProviderNotFoundException;
18
use ApiPlatform\State\ParameterNotFound;
19
use ApiPlatform\State\ProviderInterface;
20
use ApiPlatform\State\Util\ParameterParserTrait;
21
use ApiPlatform\State\Util\RequestParser;
22
use Psr\Container\ContainerInterface;
23

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

35
    public function __construct(private readonly ?ProviderInterface $decorated = null, private readonly ?ContainerInterface $locator = null)
36
    {
37
    }
475✔
38

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

47
        if ($request && null === $request->attributes->get('_api_header_parameters')) {
475✔
48
            $request->attributes->set('_api_header_parameters', $request->headers->all());
448✔
49
        }
50

51
        $parameters = $operation->getParameters();
475✔
52
        foreach ($parameters ?? [] as $parameter) {
475✔
53
            $extraProperties = $parameter->getExtraProperties();
155✔
54
            unset($extraProperties['_api_values']);
155✔
55
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties($extraProperties));
155✔
56

57
            $context = ['operation' => $operation] + $context;
155✔
58
            $values = $this->getParameterValues($parameter, $request, $context);
155✔
59
            $value = $this->extractParameterValues($parameter, $values);
155✔
60

61
            if (($default = $parameter->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
155✔
62
                $value = $default;
20✔
63
            }
64

65
            if ($value instanceof ParameterNotFound) {
155✔
66
                continue;
139✔
67
            }
68

69
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
147✔
70
                $parameter->getExtraProperties() + ['_api_values' => $value]
147✔
71
            ));
147✔
72

73
            if (null === ($provider = $parameter->getProvider())) {
147✔
74
                continue;
115✔
75
            }
76

77
            if (\is_callable($provider)) {
32✔
78
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
16✔
79
                    $operation = $op;
12✔
80
                }
81

82
                continue;
12✔
83
            }
84

85
            if (\is_string($provider)) {
20✔
86
                if (!$this->locator->has($provider)) {
20✔
NEW
87
                    throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
88
                }
89

90
                $provider = $this->locator->get($provider);
20✔
91
            }
92

93
            if (($op = $provider->provide($parameter, $values, $context)) instanceof Operation) {
20✔
94
                $operation = $op;
16✔
95
            }
96
        }
97

98
        if ($parameters) {
475✔
99
            $operation = $operation->withParameters($parameters);
175✔
100
        }
101
        $request?->attributes->set('_api_operation', $operation);
475✔
102
        $context['operation'] = $operation;
475✔
103

104
        return $this->decorated?->provide($operation, $uriVariables, $context);
475✔
105
    }
106
}
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

© 2026 Coveralls, Inc