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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

85.71
/src/State/Util/ParameterParserTrait.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\Util;
15

16
use ApiPlatform\Metadata\HeaderParameter;
17
use ApiPlatform\Metadata\HeaderParameterInterface;
18
use ApiPlatform\Metadata\Parameter;
19
use ApiPlatform\State\ParameterNotFound;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\TypeInfo\Type\CollectionType;
22
use Symfony\Component\TypeInfo\Type\UnionType;
23

24
/**
25
 * @internal
26
 */
27
trait ParameterParserTrait
28
{
29
    /**
30
     * @param array<string, mixed> $context
31
     *
32
     * @return array<string, mixed>
33
     */
34
    private function getParameterValues(Parameter $parameter, ?Request $request, array $context): array
35
    {
36
        if ($request) {
286✔
37
            return ($parameter instanceof HeaderParameterInterface ? $request->attributes->get('_api_header_parameters') : $request->attributes->get('_api_query_parameters')) ?? [];
284✔
38
        }
39

40
        return $context['args'] ?? [];
2✔
41
    }
42

43
    /**
44
     * @param array<string, mixed> $values
45
     *
46
     * @return array<mixed, mixed>|ParameterNotFound|array
47
     */
48
    private function extractParameterValues(Parameter $parameter, array $values): string|ParameterNotFound|array
49
    {
50
        $accessors = null;
286✔
51
        $key = $parameter->getKey();
286✔
52
        if (null === $key) {
286✔
53
            throw new \RuntimeException('A Parameter should have a key.');
×
54
        }
55

56
        if ($parameter instanceof HeaderParameterInterface) {
286✔
57
            $key = strtolower($key);
40✔
58
        }
59

60
        $parsedKey = explode('[:property]', $key);
286✔
61
        if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
286✔
62
            $key = $parsedKey[0];
272✔
63
        } elseif (str_contains($key, '[')) {
272✔
64
            preg_match_all('/[^\[\]]+/', $key, $matches);
42✔
65
            $key = array_shift($matches[0]);
42✔
66
            $accessors = $matches[0];
42✔
67
        }
68

69
        $value = $values[$key] ?? new ParameterNotFound();
286✔
70
        foreach ($accessors ?? [] as $accessor) {
286✔
71
            if (\is_array($value) && isset($value[$accessor])) {
42✔
72
                $value = $value[$accessor];
×
73
            } else {
74
                $value = new ParameterNotFound();
42✔
75
                continue;
42✔
76
            }
77
        }
78

79
        if ($value instanceof ParameterNotFound) {
286✔
80
            return $value;
272✔
81
        }
82

83
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
272✔
84
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
272✔
85

86
        // type-info 7.2
87
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
272✔
88
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
89
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
90
                    break;
×
91
                }
92
            }
93
        }
94

95
        if ($isCollection && true === $parameter->getCastToArray() && !\is_array($value)) {
272✔
96
            $value = [$value];
2✔
97
        }
98

99
        if (!$isCollection && $parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
272✔
100
            $value = $value[0];
10✔
101
        }
102

103
        return $value;
272✔
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