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

api-platform / core / 10797021570

10 Sep 2024 04:49PM UTC coverage: 7.679% (+0.02%) from 7.659%
10797021570

push

github

soyuka
fix(laravel): parameter fixes and tests

32 of 49 new or added lines in 6 files covered. (65.31%)

2574 existing lines in 81 files now uncovered.

12619 of 164324 relevant lines covered (7.68%)

23.18 hits per line

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

0.0
/src/Laravel/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\Laravel\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 Illuminate\Support\Facades\Validator;
21
use Illuminate\Validation\ValidationException;
22
use Symfony\Component\HttpFoundation\Request;
23

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

36
    /**
37
     * @param ProviderInterface<object> $decorated
38
     */
39
    public function __construct(
40
        private readonly ?ProviderInterface $decorated = null,
41
    ) {
42
    }
×
43

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

50
        $operation = $request->attributes->get('_api_operation') ?? $operation;
×
51
        if (!($operation->getQueryParameterValidationEnabled() ?? true)) {
×
52
            return $this->decorated->provide($operation, $uriVariables, $context);
×
53
        }
54

55
        $allConstraints = [];
×
56
        foreach ($operation->getParameters() ?? [] as $parameter) {
×
57
            if (!$constraints = $parameter->getConstraints()) {
×
58
                continue;
×
59
            }
60

61
            $key = $parameter->getKey();
×
62
            $value = $parameter->getValue();
×
63
            if ($value instanceof ParameterNotFound) {
×
64
                $value = null;
×
65
            }
66

NEW
67
            foreach ((array) $constraints as $k => $c) {
×
NEW
68
                if (!\is_string($k)) {
×
NEW
69
                    $k = $key;
×
70
                }
71

NEW
72
                $allConstraints[$k] = $c;
×
73
            }
74
        }
75

76
        $validator = Validator::make($request->query->all(), $allConstraints);
×
77
        if ($validator->fails()) {
×
78
            throw $this->getValidationError($validator, new ValidationException($validator));
×
79
        }
80

81
        return $this->decorated->provide($operation, $uriVariables, $context);
×
82
    }
83
}
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