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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

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

96.88
/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
    ) {
UNCOV
39
    }
2,171✔
40

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

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

UNCOV
52
        $constraintViolationList = new ConstraintViolationList();
2,149✔
UNCOV
53
        foreach ($operation->getParameters() ?? [] as $parameter) {
2,149✔
UNCOV
54
            if (!$constraints = $parameter->getConstraints()) {
816✔
UNCOV
55
                continue;
722✔
56
            }
57

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

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

UNCOV
83
        if (0 !== \count($constraintViolationList)) {
2,149✔
UNCOV
84
            throw new ValidationException($constraintViolationList);
69✔
85
        }
86

UNCOV
87
        return $this->decorated->provide($operation, $uriVariables, $context);
2,149✔
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