• 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

97.06
/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\Metadata\Parameter;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\State\Exception\ProviderNotFoundException;
20
use ApiPlatform\State\ParameterProviderInterface;
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
    }
247✔
40

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

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

53
        $context = ['operation' => $operation] + $context;
247✔
54
        $p = $operation->getParameters() ?? [];
247✔
55
        $parameters = $p instanceof Parameters ? iterator_to_array($p) : $p;
247✔
56
        foreach ($parameters as $parameter) {
247✔
57
            $key = $parameter->getKey();
79✔
58
            $values = $this->extractParameterValues($parameter, $request, $context);
79✔
59
            $key = $this->getParameterFlattenKey($key, $values);
79✔
60

61
            if (!isset($values[$key])) {
79✔
62
                continue;
75✔
63
            }
64

65
            $parameters[$parameter->getKey()] = $parameter = $parameter->withExtraProperties(
75✔
66
                $parameter->getExtraProperties() + ['_api_values' => [$key => $values[$key]]]
75✔
67
            );
75✔
68

69
            if (null === ($provider = $parameter->getProvider())) {
75✔
70
                continue;
55✔
71
            }
72

73
            if (\is_callable($provider)) {
20✔
74
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
8✔
75
                    $operation = $op;
4✔
76
                }
77

78
                continue;
4✔
79
            }
80

81
            if (!\is_string($provider) || !$this->locator->has($provider)) {
16✔
82
                throw new ProviderNotFoundException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
83
            }
84

85
            /** @var ParameterProviderInterface $providerInstance */
86
            $providerInstance = $this->locator->get($provider);
16✔
87
            if (($op = $providerInstance->provide($parameter, $values, $context)) instanceof Operation) {
16✔
88
                $operation = $op;
12✔
89
            }
90
        }
91

92
        $operation = $operation->withParameters(new Parameters($parameters));
247✔
93
        $request?->attributes->set('_api_operation', $operation);
247✔
94
        $context['operation'] = $operation;
247✔
95

96
        return $this->decorated?->provide($operation, $uriVariables, $context);
247✔
97
    }
98
}
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