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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.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\Metadata\Error;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Contracts\Foundation\Application;
21
use Illuminate\Database\Eloquent\Model;
22
use Illuminate\Foundation\Http\FormRequest;
23
use Illuminate\Support\Facades\Validator;
24
use Illuminate\Validation\ValidationException;
25
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27

28
/**
29
 * @implements ProviderInterface<object>
30
 */
31
final class ValidateProvider implements ProviderInterface
32
{
33
    use ValidationErrorTrait;
34

35
    /**
36
     * @param ProviderInterface<object> $inner
37
     */
38
    public function __construct(
39
        private readonly ProviderInterface $inner,
40
        private readonly Application $app,
41
        // TODO: trigger deprecation in API Platform 4.2 when this is not defined
42
        private readonly ?NormalizerInterface $normalizer = null,
43
        ?NameConverterInterface $nameConverter = null,
44
    ) {
NEW
45
        $this->nameConverter = $nameConverter;
×
46
    }
47

48
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
49
    {
50
        $body = $this->inner->provide($operation, $uriVariables, $context);
×
51

52
        if ($operation instanceof Error) {
×
53
            return $body;
×
54
        }
55

56
        $rules = $operation->getRules();
×
57
        if (\is_callable($rules)) {
×
58
            $rules = $rules();
×
59
        }
60

61
        if (\is_string($rules) && is_a($rules, FormRequest::class, true)) {
×
62
            try {
63
                // this also throws an AuthorizationException
64
                $this->app->make($rules);
×
65
            } catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
×
66
                if (!$operation->canValidate()) {
×
67
                    return $body;
×
68
                }
69

70
                throw $this->getValidationError($e->validator, $e);
×
71
            }
72

73
            return $body;
×
74
        }
75

76
        if (!$operation->canValidate()) {
×
77
            return $body;
×
78
        }
79

80
        if (!\is_array($rules)) {
×
81
            return $body;
×
82
        }
83

NEW
84
        $validationBody = $this->getBodyForValidation($body);
×
85

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

91
        return $body;
×
92
    }
93

94
    /**
95
     * @return array<string, mixed>
96
     */
97
    private function getBodyForValidation(mixed $body): array
98
    {
NEW
99
        if (!$body) {
×
NEW
100
            return [];
×
101
        }
102

NEW
103
        if ($body instanceof Model) {
×
NEW
104
            return $body->toArray();
×
105
        }
106

NEW
107
        if ($this->normalizer) {
×
NEW
108
            if (!\is_array($v = $this->normalizer->normalize($body))) {
×
NEW
109
                throw new RuntimeException('An array is expected.');
×
110
            }
111

NEW
112
            return $v;
×
113
        }
114

115
        // hopefully this path never gets used, its there for BC-layer only
116
        // TODO: deprecation in API Platform 4.2
117
        // TODO: remove in 5.0
NEW
118
        if ($s = json_encode($body)) {
×
NEW
119
            return json_decode($s, true);
×
120
        }
121

NEW
122
        throw new RuntimeException('Could not transform the denormalized body in an array for validation');
×
123
    }
124
}
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

© 2025 Coveralls, Inc