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

api-platform / core / 7407219156

04 Jan 2024 08:32AM UTC coverage: 35.357% (-1.9%) from 37.257%
7407219156

push

github

soyuka
cs: various fixes

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

425 existing lines in 27 files now uncovered.

10237 of 28953 relevant lines covered (35.36%)

27.07 hits per line

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

0.0
/src/Api/QueryParameterValidator/Validator/Required.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\Api\QueryParameterValidator\Validator;
15

16
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18
use ApiPlatform\State\Util\RequestParser;
19

20
/**
21
 * @deprecated use \ApiPlatform\ParameterValidator\Validator\Required instead
22
 */
23
final class Required implements ValidatorInterface
24
{
25
    use CheckFilterDeprecationsTrait;
26

27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function validate(string $name, array $filterDescription, array $queryParameters): array
31
    {
32
        // filter is not required, the `checkRequired` method can not break
UNCOV
33
        if (!($filterDescription['required'] ?? false)) {
×
UNCOV
34
            return [];
×
35
        }
36

37
        // if query param is not given, then break
UNCOV
38
        if (!$this->requestHasQueryParameter($queryParameters, $name)) {
×
UNCOV
39
            return [
×
UNCOV
40
                sprintf('Query parameter "%s" is required', $name),
×
UNCOV
41
            ];
×
42
        }
43

UNCOV
44
        $this->checkFilterDeprecations($filterDescription);
×
45

46
        // if query param is empty and the configuration does not allow it
UNCOV
47
        if (!($filterDescription['openapi']['allowEmptyValue'] ?? $filterDescription['swagger']['allowEmptyValue'] ?? false) && empty($this->requestGetQueryParameter($queryParameters, $name))) {
×
UNCOV
48
            return [
×
UNCOV
49
                sprintf('Query parameter "%s" does not allow empty value', $name),
×
UNCOV
50
            ];
×
51
        }
52

UNCOV
53
        return [];
×
54
    }
55

56
    /**
57
     * Test if request has required parameter.
58
     */
59
    private function requestHasQueryParameter(array $queryParameters, string $name): bool
60
    {
UNCOV
61
        $matches = RequestParser::parseRequestParams($name);
×
UNCOV
62
        if (!$matches) {
×
63
            return false;
×
64
        }
65

UNCOV
66
        $rootName = array_keys($matches)[0] ?? '';
×
UNCOV
67
        if (!$rootName) {
×
UNCOV
68
            return false;
×
69
        }
70

UNCOV
71
        if (\is_array($matches[$rootName])) {
×
UNCOV
72
            $keyName = array_keys($matches[$rootName])[0];
×
73

UNCOV
74
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
75

UNCOV
76
            return \is_array($queryParameter) && isset($queryParameter[$keyName]);
×
77
        }
78

UNCOV
79
        return \array_key_exists((string) $rootName, $queryParameters);
×
80
    }
81

82
    /**
83
     * Test if required filter is valid. It validates array notation too like "required[bar]".
84
     */
85
    private function requestGetQueryParameter(array $queryParameters, string $name)
86
    {
UNCOV
87
        $matches = RequestParser::parseRequestParams($name);
×
UNCOV
88
        if (empty($matches)) {
×
89
            return null;
×
90
        }
91

UNCOV
92
        $rootName = array_keys($matches)[0] ?? '';
×
UNCOV
93
        if (!$rootName) {
×
UNCOV
94
            return null;
×
95
        }
96

UNCOV
97
        if (\is_array($matches[$rootName])) {
×
UNCOV
98
            $keyName = array_keys($matches[$rootName])[0];
×
99

UNCOV
100
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
101

UNCOV
102
            if (\is_array($queryParameter) && isset($queryParameter[$keyName])) {
×
UNCOV
103
                return $queryParameter[$keyName];
×
104
            }
105

UNCOV
106
            return null;
×
107
        }
108

UNCOV
109
        return $queryParameters[(string) $rootName];
×
110
    }
111
}
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