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

api-platform / core / 12691826531

09 Jan 2025 02:22PM UTC coverage: 61.963% (-0.2%) from 62.121%
12691826531

push

github

web-flow
 chore: add missing deprecation notices to ParameterValidator  (#6655)

Co-authored-by: soyuka <soyuka@users.noreply.github.com>

2 of 11 new or added lines in 11 files covered. (18.18%)

25 existing lines in 4 files now uncovered.

11475 of 18519 relevant lines covered (61.96%)

68.04 hits per line

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

0.0
/src/ParameterValidator/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\ParameterValidator\Validator;
15

16
use ApiPlatform\State\Util\RequestParser;
17

18
/**
19
 * @deprecated use \ApiPlatform\Metadata\Parameter::$constraints instead
20
 */
21
final class Required implements ValidatorInterface
22
{
23
    use CheckFilterDeprecationsTrait;
24

25
    public function __construct()
26
    {
NEW
27
        trigger_deprecation('api-platform/core', '3.4', 'The class "%s" is deprecated, use "\ApiPlatform\Metadata\Parameter::$constraints" instead.', __CLASS__);
×
28
    }
29

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

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

47
        $this->checkFilterDeprecations($filterDescription);
×
48

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

56
        return [];
×
57
    }
58

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

69
        $rootName = array_keys($matches)[0] ?? '';
×
70
        if (!$rootName) {
×
71
            return false;
×
72
        }
73

74
        if (\is_array($matches[$rootName])) {
×
75
            $keyName = array_keys($matches[$rootName])[0];
×
76

77
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
78

79
            return \is_array($queryParameter) && isset($queryParameter[$keyName]);
×
80
        }
81

82
        return \array_key_exists((string) $rootName, $queryParameters);
×
83
    }
84

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

95
        $rootName = array_keys($matches)[0] ?? '';
×
96
        if (!$rootName) {
×
97
            return null;
×
98
        }
99

100
        if (\is_array($matches[$rootName])) {
×
101
            $keyName = array_keys($matches[$rootName])[0];
×
102

103
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
104

105
            if (\is_array($queryParameter) && isset($queryParameter[$keyName])) {
×
106
                return $queryParameter[$keyName];
×
107
            }
108

109
            return null;
×
110
        }
111

112
        return $queryParameters[(string) $rootName];
×
113
    }
114
}
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