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