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
|
|
|
/** |
31
|
|
|
* RemoteValidator constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Validation\Validator $validator |
34
|
|
|
*/ |
35
|
|
|
public function __construct(BaseValidator $validator) |
36
|
|
|
{ |
37
|
|
|
$this->validator = $validator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function validate($attribute, $value, $parameters) |
41
|
|
|
{ |
42
|
|
|
$validationData = $this->parseJsRemoteRequest($attribute, $value, $parameters); |
43
|
|
|
$this->validateJsRemoteRequest($validationData[1]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parse Validation input request data. |
48
|
|
|
* |
49
|
|
|
* @param $attribute |
50
|
|
|
* @param $value |
51
|
|
|
* @param $parameters |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function parseJsRemoteRequest($attribute, $value, $parameters) |
56
|
|
|
{ |
57
|
|
|
$attr_parts = array(); |
58
|
|
|
parse_str("$value=", $attr_parts); |
59
|
|
|
$newAttr = array_keys(Arr::dot($attr_parts)); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return [$attribute, array_pop($newAttr), $parameters]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validate remote Javascript Validations. |
66
|
|
|
* |
67
|
|
|
* @param $attribute |
68
|
|
|
*/ |
69
|
|
|
protected function validateJsRemoteRequest($attribute) |
70
|
|
|
{ |
71
|
|
|
$validator = $this->validator; |
72
|
|
|
$validator = $this->setRemoteValidation($attribute, $validator); |
73
|
|
|
|
74
|
|
|
if ($validator->passes()) { |
75
|
|
|
$message = true; |
76
|
|
|
} else { |
77
|
|
|
$message = $validator->messages()->get($attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new HttpResponseException( |
81
|
|
|
new JsonResponse($message, 200)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets data for validate remote rules. |
86
|
|
|
* |
87
|
|
|
* @param $attribute |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Validation\Validator |
90
|
|
|
*/ |
91
|
|
|
protected function setRemoteValidation($attribute, BaseValidator $validator) |
92
|
|
|
{ |
93
|
|
|
if (!array_key_exists($attribute, $validator->getRules())) { |
94
|
|
|
throw new BadRequestHttpException("Undefined '$attribute' attribute"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$rules = $validator->getRules()[$attribute]; |
98
|
|
|
$disabled = $this->validationDisabled($rules); |
99
|
|
|
$protectedValidator = $this->createProtectedCaller($validator); |
100
|
|
|
|
101
|
|
|
foreach ($rules as $i => $rule) { |
102
|
|
|
$parsedRule = call_user_func($protectedValidator, 'parseRule', [$rule]); |
103
|
|
|
if ($disabled || !$this->isRemoteRule($parsedRule[0])) { |
104
|
|
|
unset($rules[$i]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$validator->setRules([$attribute => $rules]); |
108
|
|
|
|
109
|
|
|
if (empty($validator->getRules())) { |
110
|
|
|
throw new BadRequestHttpException("No validations available for '$attribute''"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $validator; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.