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

api-platform / core / 10790047473

10 Sep 2024 09:50AM UTC coverage: 7.659% (-0.001%) from 7.66%
10790047473

push

github

web-flow
feat(laravel): parameter validator + security (#6603)

0 of 38 new or added lines in 4 files covered. (0.0%)

1192 existing lines in 74 files now uncovered.

12581 of 164273 relevant lines covered (7.66%)

22.85 hits per line

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

0.0
/src/Laravel/State/ValidateProvider.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\Error;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\State\ProviderInterface;
19
use Illuminate\Contracts\Foundation\Application;
20
use Illuminate\Foundation\Http\FormRequest;
21
use Illuminate\Support\Facades\Validator;
22
use Illuminate\Validation\ValidationException;
23

24
/**
25
 * @implements ProviderInterface<object>
26
 */
27
final class ValidateProvider implements ProviderInterface
28
{
29
    use ValidationErrorTrait;
30

31
    /**
32
     * @param ProviderInterface<object> $inner
33
     */
34
    public function __construct(
35
        private readonly ProviderInterface $inner,
36
        private readonly Application $app,
37
    ) {
38
    }
×
39

40
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
41
    {
42
        $request = $context['request'];
×
43
        $body = $this->inner->provide($operation, $uriVariables, $context);
×
44

45
        if (!$operation->canValidate() || $operation instanceof Error) {
×
46
            return $body;
×
47
        }
48

49
        $rules = $operation->getRules();
×
50
        if (\is_callable($rules)) {
×
51
            $rules = $rules();
×
52
        }
53

54
        if (\is_string($rules) && is_a($rules, FormRequest::class, true)) {
×
55
            try {
56
                $this->app->make($rules);
×
57
                // } catch (AuthorizationException $e) { // TODO: we may want to catch this to transform to an error
58
            } catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
×
NEW
59
                throw $this->getValidationError($e->validator, $e);
×
60
            }
61

62
            return $body;
×
63
        }
64

65
        if (!\is_array($rules)) {
×
66
            return $body;
×
67
        }
68

NEW
69
        $validator = Validator::make($request->request->all(), $rules);
×
70
        if ($validator->fails()) {
×
NEW
71
            throw $this->getValidationError($validator, new ValidationException($validator));
×
72
        }
73

74
        return $body;
×
75
    }
76
}
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