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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    {
UNCOV
36
        if ($request) {
×
UNCOV
37
            return ($parameter instanceof HeaderParameterInterface ? $request->attributes->get('_api_header_parameters') : $request->attributes->get('_api_query_parameters')) ?? [];
×
38
        }
39

UNCOV
40
        return $context['args'] ?? [];
×
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
    {
UNCOV
50
        $accessors = null;
×
UNCOV
51
        $key = $parameter->getKey();
×
UNCOV
52
        if (null === $key) {
×
53
            throw new \RuntimeException('A Parameter should have a key.');
×
54
        }
55

UNCOV
56
        if ($parameter instanceof HeaderParameterInterface) {
×
UNCOV
57
            $key = strtolower($key);
×
58
        }
59

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

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

UNCOV
79
        if ($value instanceof ParameterNotFound) {
×
UNCOV
80
            return $value;
×
81
        }
82

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

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

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

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

UNCOV
103
        return $value;
×
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