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

api-platform / core / 9835534319

08 Jul 2024 07:42AM UTC coverage: 64.804% (-0.007%) from 64.811%
9835534319

push

github

soyuka
Merge 3.3

11 of 14 new or added lines in 5 files covered. (78.57%)

182 existing lines in 29 files now uncovered.

11458 of 17681 relevant lines covered (64.8%)

67.53 hits per line

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

93.75
/src/Symfony/Validator/State/ParameterValidatorProvider.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\Symfony\Validator\State;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\ParameterNotFound;
18
use ApiPlatform\State\ProviderInterface;
19
use ApiPlatform\State\Util\ParameterParserTrait;
20
use ApiPlatform\Validator\Exception\ValidationException;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\Validator\ConstraintViolation;
23
use Symfony\Component\Validator\ConstraintViolationList;
24
use Symfony\Component\Validator\Validator\ValidatorInterface;
25

26
/**
27
 * Validates parameters using the Symfony validator.
28
 *
29
 * @experimental
30
 */
31
final class ParameterValidatorProvider implements ProviderInterface
32
{
33
    use ParameterParserTrait;
34

35
    public function __construct(
36
        private readonly ValidatorInterface $validator,
37
        private readonly ProviderInterface $decorated
38
    ) {
39
    }
392✔
40

41
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
42
    {
43
        if (!($request = $context['request']) instanceof Request) {
392✔
UNCOV
44
            return $this->decorated->provide($operation, $uriVariables, $context);
×
45
        }
46

47
        $operation = $request->attributes->get('_api_operation') ?? $operation;
392✔
48
        if (!($operation->getQueryParameterValidationEnabled() ?? true)) {
392✔
49
            return $this->decorated->provide($operation, $uriVariables, $context);
4✔
50
        }
51

52
        $constraintViolationList = new ConstraintViolationList();
388✔
53
        foreach ($operation->getParameters() ?? [] as $parameter) {
388✔
54
            if (!$constraints = $parameter->getConstraints()) {
76✔
55
                continue;
68✔
56
            }
57

58
            $key = $parameter->getKey();
52✔
59
            $value = $parameter->getValue();
52✔
60
            if ($value instanceof ParameterNotFound) {
52✔
NEW
61
                $value = null;
×
62
            }
63

64
            $violations = $this->validator->validate($value, $constraints);
52✔
65
            foreach ($violations as $violation) {
52✔
66
                $constraintViolationList->add(new ConstraintViolation(
44✔
67
                    $violation->getMessage(),
44✔
68
                    $violation->getMessageTemplate(),
44✔
69
                    $violation->getParameters(),
44✔
70
                    $violation->getRoot(),
44✔
71
                    $parameter->getProperty() ?? (
44✔
72
                        str_contains($key, ':property') ? str_replace('[:property]', $violation->getPropertyPath(), $key) : $key.$violation->getPropertyPath()
44✔
73
                    ),
74
                    $violation->getInvalidValue(),
44✔
75
                    $violation->getPlural(),
44✔
76
                    $violation->getCode(),
44✔
77
                    $violation->getConstraint(),
44✔
78
                    $violation->getCause()
44✔
79
                ));
44✔
80
            }
81
        }
82

83
        if (0 !== \count($constraintViolationList)) {
388✔
84
            throw new ValidationException($constraintViolationList);
44✔
85
        }
86

87
        return $this->decorated->provide($operation, $uriVariables, $context);
388✔
88
    }
89
}
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