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