generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathDataValidatorResolver.php
53 lines (43 loc) · 1.6 KB
/
DataValidatorResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Spatie\LaravelData\Resolvers;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Validator as ValidatorFacade;
use Illuminate\Validation\Validator;
use Spatie\LaravelData\Contracts\DataObject;
use Spatie\LaravelData\Support\Validation\DataRules;
use Spatie\LaravelData\Support\Validation\ValidationPath;
class DataValidatorResolver
{
public function __construct(
protected DataValidationRulesResolver $dataValidationRulesResolver,
protected DataValidationMessagesAndAttributesResolver $dataValidationMessagesAndAttributesResolver
) {
}
/** @param class-string<DataObject> $dataClass */
public function execute(string $dataClass, Arrayable|array $payload): Validator
{
$payload = $payload instanceof Arrayable ? $payload->toArray() : $payload;
$rules = $this->dataValidationRulesResolver->execute(
$dataClass,
$payload,
ValidationPath::create(),
DataRules::create()
);
['messages' => $messages, 'attributes' => $attributes] = $this->dataValidationMessagesAndAttributesResolver->execute(
$dataClass,
$payload,
ValidationPath::create()
);
$validator = ValidatorFacade::make(
$payload,
$rules,
$messages,
$attributes
);
if (method_exists($dataClass, 'stopOnFirstFailure')) {
$validator->stopOnFirstFailure(app()->call([$dataClass, 'stopOnFirstFailure']));
}
$dataClass::withValidator($validator);
return $validator;
}
}