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

api-platform / core / 15276908793

27 May 2025 01:41PM UTC coverage: 26.397% (+4.7%) from 21.714%
15276908793

push

github

web-flow
chore: type-info v7.3.0-RC1 #7177 (#7178)

13727 of 52003 relevant lines covered (26.4%)

72.72 hits per line

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

88.57
/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) {
762✔
37
            return ($parameter instanceof HeaderParameterInterface ? $request->attributes->get('_api_header_parameters') : $request->attributes->get('_api_query_parameters')) ?? [];
760✔
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;
762✔
51
        $key = $parameter->getKey();
762✔
52
        if (null === $key) {
762✔
53
            throw new \RuntimeException('A Parameter should have a key.');
×
54
        }
55

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

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

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

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

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

86
        // type-info 7.2
87
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
503✔
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)) {
503✔
96
            $value = [$value];
2✔
97
        }
98

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

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