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

api-platform / core / 13586524635

28 Feb 2025 10:45AM UTC coverage: 7.289%. Remained the same
13586524635

push

github

soyuka
docs: v4.0.19

12436 of 170622 relevant lines covered (7.29%)

11.99 hits per line

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

97.37
/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\ParameterProviderInterface;
20
use ApiPlatform\State\ProviderInterface;
21
use ApiPlatform\State\Util\ParameterParserTrait;
22
use ApiPlatform\State\Util\RequestParser;
23
use Psr\Container\ContainerInterface;
24

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

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

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

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

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

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

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

66
            if ($value instanceof ParameterNotFound) {
359✔
67
                continue;
344✔
68
            }
69

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

74
            if (null === ($provider = $parameter->getProvider())) {
324✔
75
                continue;
303✔
76
            }
77

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

83
                continue;
9✔
84
            }
85

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

90
            /** @var ParameterProviderInterface $providerInstance */
91
            $providerInstance = $this->locator->get($provider);
12✔
92
            if (($op = $providerInstance->provide($parameter, $values, $context)) instanceof Operation) {
12✔
93
                $operation = $op;
9✔
94
            }
95
        }
96

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

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