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

api-platform / core / 9710931785

28 Jun 2024 09:54AM UTC coverage: 64.157% (+2.0%) from 62.122%
9710931785

push

github

soyuka
Merge 3.3

35 of 36 new or added lines in 8 files covered. (97.22%)

1 existing line in 1 file now uncovered.

11223 of 17493 relevant lines covered (64.16%)

66.06 hits per line

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

96.43
/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\ProviderInterface;
18
use ApiPlatform\State\Util\ParameterParserTrait;
19
use ApiPlatform\Validator\Exception\ValidationException;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\Validator\ConstraintViolation;
22
use Symfony\Component\Validator\ConstraintViolationList;
23
use Symfony\Component\Validator\Validator\ValidatorInterface;
24

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

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

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

46
        $operation = $request->attributes->get('_api_operation') ?? $operation;
384✔
47
        foreach ($operation->getParameters() ?? [] as $parameter) {
384✔
48
            if (!$constraints = $parameter->getConstraints()) {
72✔
49
                continue;
32✔
50
            }
51

52
            $key = $this->getParameterFlattenKey($parameter->getKey(), $this->extractParameterValues($parameter, $request, $context));
52✔
53
            $value = $parameter->getExtraProperties()['_api_values'][$key] ?? null;
52✔
54
            $violations = $this->validator->validate($value, $constraints);
52✔
55
            if (0 !== \count($violations)) {
52✔
56
                $constraintViolationList = new ConstraintViolationList();
44✔
57
                foreach ($violations as $violation) {
44✔
58
                    $propertyPath = $key !== $parameter->getKey() ? $key.$violation->getPropertyPath() : ($parameter->getProperty() ?? $key);
44✔
59
                    $constraintViolationList->add(new ConstraintViolation(
44✔
60
                        $violation->getMessage(),
44✔
61
                        $violation->getMessageTemplate(),
44✔
62
                        $violation->getParameters(),
44✔
63
                        $violation->getRoot(),
44✔
64
                        $propertyPath,
44✔
65
                        $violation->getInvalidValue(),
44✔
66
                        $violation->getPlural(),
44✔
67
                        $violation->getCode(),
44✔
68
                        $violation->getConstraint(),
44✔
69
                        $violation->getCause()
44✔
70
                    ));
44✔
71
                }
72

73
                throw new ValidationException($constraintViolationList);
44✔
74
            }
75
        }
76

77
        return $this->decorated->provide($operation, $uriVariables, $context);
384✔
78
    }
79
}
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