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

api-platform / core / 11271860720

10 Oct 2024 10:04AM UTC coverage: 7.84% (+0.007%) from 7.833%
11271860720

push

github

soyuka
Merge 3.4

0 of 34 new or added lines in 5 files covered. (0.0%)

11160 existing lines in 381 files now uncovered.

12955 of 165237 relevant lines covered (7.84%)

27.06 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,184✔
40

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

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

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

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

UNCOV
64
            $violations = $this->validator->validate($value, $constraints);
733✔
UNCOV
65
            foreach ($violations as $violation) {
733✔
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,162✔
UNCOV
84
            throw new ValidationException($constraintViolationList);
69✔
85
        }
86

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