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

api-platform / core / 13944674159

19 Mar 2025 10:47AM UTC coverage: 7.275% (-0.01%) from 7.289%
13944674159

push

github

soyuka
Merge 3.4

6 of 23 new or added lines in 4 files covered. (26.09%)

2380 existing lines in 154 files now uncovered.

12415 of 170643 relevant lines covered (7.28%)

12.04 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
    }
1,087✔
38

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

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

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

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

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

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

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

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

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

82
                continue;
9✔
83
            }
84

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

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

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

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

104
        return $this->decorated?->provide($operation, $uriVariables, $context);
1,054✔
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

© 2025 Coveralls, Inc