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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 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 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 also throws an AuthorizationException
57
                $this->app->make($rules);
×
58
            } catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
×
59
                if (!$operation->canValidate()) {
×
60
                    return $body;
×
61
                }
62

63
                throw $this->getValidationError($e->validator, $e);
×
64
            }
65

66
            return $body;
×
67
        }
68

69
        if (!$operation->canValidate()) {
×
70
            return $body;
×
71
        }
72

73
        if (!\is_array($rules)) {
×
74
            return $body;
×
75
        }
76

77
        // In Symfony, validation is done on the Resource object (here $body) using Deserialization before Validation
78
        // Here, we did not deserialize yet, we validate on the raw body before.
79
        $validationBody = $request->request->all();
×
UNCOV
80
        if ('jsonapi' === $request->getRequestFormat()) {
×
UNCOV
81
            $validationBody = $validationBody['data']['attributes'];
×
82
        }
83

UNCOV
84
        $validator = Validator::make($validationBody, $rules);
×
UNCOV
85
        if ($validator->fails()) {
×
UNCOV
86
            throw $this->getValidationError($validator, new ValidationException($validator));
×
87
        }
88

UNCOV
89
        return $body;
×
90
    }
91
}
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