1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Remote; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Exception\HttpResponseException; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Validation\Validator as BaseValidator; |
9
|
|
|
use Proengsoft\JsValidation\Exceptions\BadRequestHttpException; |
10
|
|
|
use Proengsoft\JsValidation\Support\AccessProtectedTrait; |
11
|
|
|
use Proengsoft\JsValidation\Support\RuleListTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RemoteValidator. |
15
|
|
|
*/ |
16
|
|
|
class Validator |
17
|
|
|
{ |
18
|
|
|
use AccessProtectedTrait, RuleListTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validator extension name. |
22
|
|
|
*/ |
23
|
|
|
const EXTENSION_NAME = 'jsvalidation'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Illuminate\Validation\Validator |
27
|
|
|
*/ |
28
|
|
|
protected $validator; |
29
|
|
|
|
30
|
|
|
protected $validateAll = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* RemoteValidator constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Validation\Validator $validator |
36
|
|
|
*/ |
37
|
5 |
|
public function __construct(BaseValidator $validator) |
38
|
|
|
{ |
39
|
5 |
|
$this->validator = $validator; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
5 |
|
public function validate($attribute, $value, $parameters, $validateAll = false) |
43
|
|
|
{ |
44
|
5 |
|
$this->validateAll = $validateAll; |
45
|
5 |
|
$validationData = $this->parseJsRemoteRequest($attribute, $value, $parameters); |
46
|
5 |
|
$validationResult = $this->validateJsRemoteRequest($validationData[1]); |
47
|
3 |
|
throw new HttpResponseException( |
48
|
3 |
|
new JsonResponse($validationResult, 200)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Parse Validation input request data. |
53
|
|
|
* |
54
|
|
|
* @param $attribute |
55
|
|
|
* @param $value |
56
|
|
|
* @param $parameters |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
5 |
|
protected function parseJsRemoteRequest($attribute, $value, $parameters) |
60
|
|
|
{ |
61
|
5 |
|
parse_str("$value=", $attr_parts); |
62
|
5 |
|
$attr_parts = is_null($attr_parts) ? [] : $attr_parts; |
63
|
5 |
|
$newAttr = array_keys(Arr::dot($attr_parts)); |
64
|
|
|
|
65
|
5 |
|
return [$attribute, array_pop($newAttr), $parameters]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate remote Javascript Validations. |
70
|
|
|
* |
71
|
|
|
* @param $attribute |
72
|
|
|
* @return array|bool |
73
|
|
|
*/ |
74
|
5 |
|
protected function validateJsRemoteRequest($attribute) |
75
|
|
|
{ |
76
|
5 |
|
$validator = $this->validator; |
77
|
5 |
|
$validator = $this->setRemoteValidation($attribute, $validator); |
78
|
|
|
|
79
|
3 |
|
if ($validator->passes()) { |
80
|
2 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $validator->messages()->get($attribute); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets data for validate remote rules. |
88
|
|
|
* |
89
|
|
|
* @param $attribute |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Validation\Validator |
92
|
|
|
*/ |
93
|
5 |
|
protected function setRemoteValidation($attribute, BaseValidator $validator) |
94
|
|
|
{ |
95
|
5 |
|
if (! array_key_exists($attribute, $validator->getRules())) { |
96
|
1 |
|
throw new BadRequestHttpException("Undefined '$attribute' attribute"); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
$rules = $validator->getRules()[$attribute]; |
100
|
4 |
|
$rules = $this->purgeNonRemoteRules($rules, $validator); |
101
|
4 |
|
$validator->setRules([$attribute => $rules]); |
102
|
|
|
|
103
|
4 |
|
if (empty($validator->getRules()[$attribute])) { |
104
|
1 |
|
throw new BadRequestHttpException("No validations available for '$attribute'"); |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
return $validator; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
protected function purgeNonRemoteRules($rules, $validator) |
111
|
|
|
{ |
112
|
4 |
|
$disabled = $this->validationDisabled($rules); |
113
|
4 |
|
if (! $disabled && $this->validateAll) { |
114
|
|
|
return $rules; |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
$protectedValidator = $this->createProtectedCaller($validator); |
118
|
|
|
|
119
|
4 |
|
foreach ($rules as $i => $rule) { |
120
|
4 |
|
$parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]); |
121
|
4 |
|
if ($disabled || ! $this->isRemoteRule($parsedRule[0])) { |
122
|
4 |
|
unset($rules[$i]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
return $rules; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|