1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Remote; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Validation\Factory; |
7
|
|
|
use Illuminate\Validation\Validator as BaseValidator; |
8
|
|
|
use Proengsoft\JsValidation\Support\AccessProtectedTrait; |
9
|
|
|
|
10
|
|
|
class Resolver |
11
|
|
|
{ |
12
|
|
|
use AccessProtectedTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Closure |
16
|
|
|
*/ |
17
|
|
|
protected $resolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Factory |
21
|
|
|
*/ |
22
|
|
|
protected $factory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RemoteValidator constructor. |
26
|
|
|
* |
27
|
|
|
* @param Factory $factory |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Factory $factory) |
30
|
|
|
{ |
31
|
|
|
$this->factory = $factory; |
32
|
|
|
$this->resolver = $this->getProtected($factory, 'resolver'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Closure used to resolve Validator instance. |
37
|
|
|
* |
38
|
|
|
* @param $field |
39
|
|
|
* |
40
|
|
|
* @return Closure |
41
|
|
|
*/ |
42
|
|
|
public function resolver($field) |
43
|
|
|
{ |
44
|
|
|
return function ($translator, $data, $rules, $messages, $customAttributes) use ($field) { |
45
|
|
|
return $this->resolve($translator, $data, $rules, $messages, $customAttributes, $field); |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Resolves Validator instance. |
51
|
|
|
* |
52
|
|
|
* @param $translator |
53
|
|
|
* @param $data |
54
|
|
|
* @param $rules |
55
|
|
|
* @param $messages |
56
|
|
|
* @param $customAttributes |
57
|
|
|
* @param $field |
58
|
|
|
* |
59
|
|
|
* @return Validator |
60
|
|
|
*/ |
61
|
|
|
protected function resolve($translator, $data, $rules, $messages, $customAttributes, $field) |
62
|
|
|
{ |
63
|
|
|
if (is_null($this->resolver)) { |
64
|
|
|
$validator = new BaseValidator($translator, $data, $rules, $messages, $customAttributes); |
65
|
|
|
} else { |
66
|
|
|
$validator = call_user_func($this->resolver, $translator, $data, $rules, $messages, $customAttributes); |
67
|
|
|
} |
68
|
|
|
$validator->sometimes($field, Validator::EXTENSION_NAME, function () { |
69
|
|
|
return true; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
return $validator; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Closure used to trigger JsValidations. |
77
|
|
|
* |
78
|
|
|
* @return Closure |
79
|
|
|
*/ |
80
|
|
|
public function validator() |
81
|
|
|
{ |
82
|
|
|
return function ($attribute, $value, $parameters, $validator) { |
83
|
|
|
$remoteValidator = new Validator($validator); |
84
|
|
|
$remoteValidator->validate($attribute, $value, $parameters); |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|