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

api-platform / core / 10489563861

21 Aug 2024 12:09PM UTC coverage: 7.704% (+2.4%) from 5.352%
10489563861

push

github

web-flow
chore(laravel): code cleanup (#6526)

* chore(laravel): code cleanup

* fix ci

* fix phpdoc

* fix review

* fix review

0 of 11 new or added lines in 3 files covered. (0.0%)

1768 existing lines in 74 files now uncovered.

12476 of 161932 relevant lines covered (7.7%)

23.0 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\Laravel\ApiResource\ValidationError;
17
use ApiPlatform\Metadata\Error;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Foundation\Application;
21
use Illuminate\Foundation\Http\FormRequest;
22
use Illuminate\Support\Facades\Validator;
23
use Illuminate\Validation\ValidationException;
24

25
/**
26
 * @implements ProviderInterface<object>
27
 */
28
final class ValidateProvider implements ProviderInterface
29
{
30
    /**
31
     * @param ProviderInterface<object> $inner
32
     */
33
    public function __construct(
34
        private readonly ProviderInterface $inner,
35
        private readonly Application $app,
36
    ) {
UNCOV
37
    }
×
38

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

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

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

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

61
            return $body;
×
62
        }
63

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

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

73
        return $body;
×
74
    }
75

76
    private function getValidationError(ValidationException $e): ValidationError
77
    {
78
        $violations = [];
×
79
        foreach ($e->validator->errors()->messages() as $prop => $message) {
×
80
            $violations[] = ['propertyPath' => $prop, 'message' => implode(\PHP_EOL, $message)];
×
81
        }
82

83
        return new ValidationError($e->getMessage(), spl_object_hash($e), $e, $violations);
×
84
    }
85
}
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