1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace App\GraphQL\Queries\Support; |
10
|
|
|
|
11
|
|
|
use Illuminate\Validation\Factory; |
12
|
|
|
use Folklore\GraphQL\Support\Field; |
13
|
|
|
use Illuminate\Contracts\Validation\Validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class HasValidation. |
17
|
|
|
* @mixin Field |
18
|
|
|
*/ |
19
|
|
|
trait HasValidation |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param $args |
23
|
|
|
* @param $rules |
24
|
|
|
* @return Validator |
25
|
|
|
*/ |
26
|
|
|
protected function getValidator($args, $rules): Validator |
27
|
|
|
{ |
28
|
|
|
/** @var Factory $factory */ |
29
|
|
|
$factory = app(Factory::class); |
30
|
|
|
|
31
|
|
|
$validator = $factory->make($args, $rules, $this->messages()); |
32
|
|
|
|
33
|
|
|
$validator->after(function ($validator) use ($args) { |
34
|
|
|
$this->afterValidation($validator, $args); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
return $validator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Validator|\Illuminate\Validation\Validator $validator |
42
|
|
|
* @param array $args |
43
|
|
|
*/ |
44
|
|
|
private function afterValidation(Validator $validator, array $args): void |
45
|
|
|
{ |
46
|
|
|
$validatorErrors = $this->validate($validator, $args); |
47
|
|
|
|
48
|
|
|
if ($validatorErrors instanceof \Traversable) { |
49
|
|
|
foreach ($validatorErrors as $key => $message) { |
50
|
|
|
$validator->errors()->add($key, $message); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Validator $validator |
57
|
|
|
* @param array $args |
58
|
|
|
* @return \Generator|null |
59
|
|
|
*/ |
60
|
|
|
public function validate(Validator $validator, array $args = []): ?\Generator |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function messages(): array |
68
|
|
|
{ |
69
|
|
|
return []; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.