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

api-platform / core / 9744944258

01 Jul 2024 01:15PM UTC coverage: 64.67% (+2.0%) from 62.637%
9744944258

push

github

soyuka
Merge 3.3

75 of 85 new or added lines in 15 files covered. (88.24%)

1 existing line in 1 file now uncovered.

11398 of 17625 relevant lines covered (64.67%)

66.83 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
        $constraintViolationList = new ConstraintViolationList();
384✔
48
        foreach ($operation->getParameters() ?? [] as $parameter) {
384✔
49
            if (!$constraints = $parameter->getConstraints()) {
72✔
50
                continue;
64✔
51
            }
52

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

73
        if (0 !== \count($constraintViolationList)) {
384✔
74
            throw new ValidationException($constraintViolationList);
44✔
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

© 2026 Coveralls, Inc