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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

0 of 4 new or added lines in 1 file covered. (0.0%)

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

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

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

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

UNCOV
54
        if ($parameter instanceof HeaderParameterInterface) {
980✔
UNCOV
55
            $key = strtolower($key);
78✔
56
        }
57

UNCOV
58
        $parsedKey = explode('[:property]', $key);
980✔
UNCOV
59
        if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
980✔
UNCOV
60
            $key = $parsedKey[0];
552✔
UNCOV
61
        } elseif (str_contains($key, '[')) {
938✔
UNCOV
62
            preg_match_all('/[^\[\]]+/', $key, $matches);
520✔
UNCOV
63
            $key = array_shift($matches[0]);
520✔
UNCOV
64
            $accessors = $matches[0];
520✔
65
        }
66

UNCOV
67
        $value = $values[$key] ?? new ParameterNotFound();
980✔
UNCOV
68
        foreach ($accessors ?? [] as $accessor) {
980✔
UNCOV
69
            if ($value instanceof ParameterNotFound) {
386✔
UNCOV
70
                break;
366✔
71
            }
72

UNCOV
73
            if (\is_array($value) && isset($value[$accessor])) {
98✔
UNCOV
74
                $value = $value[$accessor];
84✔
UNCOV
75
            } elseif (\is_array($value) && array_is_list($value)) {
92✔
76
                $l = [];
2✔
77
                foreach ($value as $i) {
2✔
78
                    if (\is_array($i) && isset($i[$accessor])) {
2✔
79
                        $l[] = $i[$accessor];
×
80
                    }
81
                }
82

83
                if (!$l) {
2✔
84
                    $value = new ParameterNotFound();
2✔
85
                } else {
86
                    $value = $l;
×
87
                }
88
            } else {
UNCOV
89
                $value = new ParameterNotFound();
90✔
90
            }
91
        }
92

UNCOV
93
        if ($value instanceof ParameterNotFound) {
980✔
UNCOV
94
            return $value;
927✔
95
        }
96

UNCOV
97
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
697✔
UNCOV
98
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
697✔
99

100
        // type-info 7.2
UNCOV
101
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
697✔
102
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
103
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
104
                    break;
×
105
                }
106
            }
107
        }
108

UNCOV
109
        if ($isCollection && true === $parameter->getCastToArray() && !\is_array($value)) {
697✔
UNCOV
110
            $value = [$value];
2✔
111
        }
112

UNCOV
113
        if (!$isCollection && $parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
697✔
UNCOV
114
            $value = $value[0];
50✔
115
        }
116

UNCOV
117
        if (true === $parameter->getCastToNativeType() && ($castFn = $parameter->getCastFn())) {
697✔
UNCOV
118
            if (\is_array($value)) {
64✔
119
                $value = array_map(fn ($v) => $castFn($v, $parameter), $value);
×
120
            } else {
UNCOV
121
                $value = $castFn($value, $parameter);
64✔
122
            }
123
        }
124

UNCOV
125
        return $value;
697✔
126
    }
127
}
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