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

api-platform / core / 10523796051

23 Aug 2024 10:00AM UTC coverage: 6.984% (-0.7%) from 7.707%
10523796051

push

github

dunglas
chore(laravel): remove duplicated ItemNormalizer definition

11317 of 162035 relevant lines covered (6.98%)

14.3 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
    ) {
39
    }
2,177✔
40

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

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

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

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

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

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

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

© 2025 Coveralls, Inc