1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Javascript; |
4
|
|
|
|
5
|
|
|
use Proengsoft\JsValidation\Support\RuleListTrait; |
6
|
|
|
use Proengsoft\JsValidation\Support\DelegatedValidator; |
7
|
|
|
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait; |
8
|
|
|
|
9
|
|
|
class RuleParser |
10
|
|
|
{ |
11
|
|
|
use RuleListTrait, JavascriptRulesTrait, UseDelegatedValidatorTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Rule used to validate remote requests. |
15
|
|
|
*/ |
16
|
|
|
const REMOTE_RULE = 'laravelValidationRemote'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Rule used to validate javascript fields. |
20
|
|
|
*/ |
21
|
|
|
const JAVASCRIPT_RULE = 'laravelValidation'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Token used to secure romte validations. |
25
|
|
|
* |
26
|
|
|
* @string|null $remoteToken |
27
|
|
|
*/ |
28
|
|
|
protected $remoteToken; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Conditional Validations. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $conditional = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new JsValidation instance. |
39
|
|
|
* |
40
|
|
|
* @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator |
41
|
|
|
* @param string|null $remoteToken |
42
|
|
|
*/ |
43
|
18 |
|
public function __construct(DelegatedValidator $validator, $remoteToken = null) |
44
|
|
|
{ |
45
|
18 |
|
$this->validator = $validator; |
46
|
18 |
|
$this->remoteToken = $remoteToken; |
47
|
18 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return parsed Javascript Rule. |
51
|
|
|
* |
52
|
|
|
* @param string $attribute |
53
|
|
|
* @param string $rule |
54
|
|
|
* @param $parameters |
55
|
|
|
* @param $rawRule |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
6 |
|
public function getRule($attribute, $rule, $parameters, $rawRule) |
60
|
|
|
{ |
61
|
6 |
|
$isConditional = $this->isConditionalRule($attribute, $rawRule); |
62
|
6 |
|
$isRemote = $this->isRemoteRule($rule); |
63
|
|
|
|
64
|
6 |
|
if ($isConditional || $isRemote) { |
65
|
3 |
|
list($attribute, $parameters) = $this->remoteRule($attribute, $isConditional); |
66
|
3 |
|
$jsRule = self::REMOTE_RULE; |
67
|
3 |
|
} else { |
68
|
3 |
|
list($jsRule, $attribute, $parameters) = $this->clientRule($attribute, $rule, $parameters); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
$attribute = $this->getAttributeName($attribute); |
72
|
|
|
|
73
|
6 |
|
return [$attribute, $jsRule, $parameters]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets rules from Validator instance. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
1 |
|
public function getValidatorRules() |
82
|
|
|
{ |
83
|
1 |
|
return $this->validator->getRules(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add conditional rules. |
88
|
|
|
* |
89
|
|
|
* @param $attribute |
90
|
|
|
* @param array $rules |
91
|
|
|
*/ |
92
|
1 |
|
public function addConditionalRules($attribute, $rules = []) |
93
|
|
|
{ |
94
|
1 |
|
foreach ((array) $attribute as $key) { |
95
|
1 |
|
$current = isset($this->conditional[$key]) ? $this->conditional[$key] : []; |
96
|
1 |
|
$merge = head($this->validator->explodeRules((array) $rules)); |
97
|
1 |
|
$this->conditional[$key] = array_merge($current, $merge); |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if rule is passed with sometimes. |
103
|
|
|
* |
104
|
|
|
* @param $attribute |
105
|
|
|
* @param $rule |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
6 |
|
protected function isConditionalRule($attribute, $rule) |
109
|
|
|
{ |
110
|
6 |
|
return isset($this->conditional[$attribute]) && |
111
|
6 |
|
in_array($rule, $this->conditional[$attribute]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns Javascript parameters for remote validated rules. |
116
|
|
|
* |
117
|
|
|
* @param string $attribute |
118
|
|
|
* @param $rule |
119
|
|
|
* @param $parameters |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
3 |
|
protected function clientRule($attribute, $rule, $parameters) |
124
|
|
|
{ |
125
|
3 |
|
$jsRule = self::JAVASCRIPT_RULE; |
126
|
3 |
|
$method = "rule{$rule}"; |
127
|
|
|
|
128
|
3 |
|
if (method_exists($this, $method)) { |
129
|
1 |
|
list($attribute, $parameters) = $this->$method($attribute, $parameters); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
3 |
|
return [$jsRule, $attribute, $parameters]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns Javascript parameters for remote validated rules. |
137
|
|
|
* |
138
|
|
|
* @param string $attribute |
139
|
|
|
* @param bool $forceRemote |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
3 |
|
protected function remoteRule($attribute, $forceRemote) |
144
|
|
|
{ |
145
|
3 |
|
$attrHtmlName = $this->getAttributeName($attribute); |
146
|
|
|
$params = [ |
147
|
3 |
|
$attrHtmlName, |
148
|
3 |
|
$this->remoteToken, |
149
|
3 |
|
$forceRemote, |
150
|
3 |
|
]; |
151
|
|
|
|
152
|
3 |
|
return [$attribute, $params]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Handles multidimensional attribute names. |
157
|
|
|
* |
158
|
|
|
* @param $attribute |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
6 |
|
protected function getAttributeName($attribute) |
163
|
|
|
{ |
164
|
6 |
|
$attributeArray = explode('.', $attribute); |
165
|
6 |
|
if (count($attributeArray) > 1) { |
166
|
2 |
|
return $attributeArray[0].'['.implode('][', array_slice($attributeArray, 1)).']'; |
167
|
|
|
} |
168
|
|
|
|
169
|
4 |
|
return $attribute; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Parse named parameters to $key => $value items. |
174
|
|
|
* |
175
|
|
|
* @param array $parameters |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function parseNamedParameters($parameters) |
179
|
|
|
{ |
180
|
|
|
|
181
|
1 |
|
return array_reduce($parameters, function ($result, $item) { |
182
|
1 |
|
list($key, $value) = array_pad(explode('=', $item, 2), 2, null); |
183
|
|
|
|
184
|
1 |
|
$result[$key] = $value; |
185
|
|
|
|
186
|
1 |
|
return $result; |
187
|
1 |
|
}); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|