1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour; |
4
|
|
|
|
5
|
|
|
use Respect\Validation\Validatable; |
6
|
|
|
use WebTheory\Saveyour\Contracts\InputPurifierInterface; |
7
|
|
|
|
8
|
|
|
class InputPurifier implements InputPurifierInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Validation rules |
12
|
|
|
* |
13
|
|
|
* @var Validatable[] |
14
|
|
|
*/ |
15
|
|
|
protected $rules = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Callback function(s) to sanitize incoming data |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $filters = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Alerts to display upon validation failure |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $alerts = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $violations = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get callback function(s) to sanitize incoming data before saving to database |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
6 |
|
public function getFilters(): array |
42
|
|
|
{ |
43
|
6 |
|
return $this->filters; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set callback function(s) to sanitize incoming data before saving to database |
48
|
|
|
* |
49
|
|
|
* @param array $filters Callback function(s) to sanitize incoming data before saving to database |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
9 |
|
public function setFilters(callable ...$filters) |
54
|
|
|
{ |
55
|
9 |
|
$this->filters = $filters; |
56
|
|
|
|
57
|
9 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set callback function(s) to sanitize incoming data before saving to database |
62
|
|
|
* |
63
|
|
|
* @param callable $filters Callback function(s) to sanitize incoming data before saving to database |
64
|
|
|
* |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
6 |
|
public function addFilter(callable $filter) |
68
|
|
|
{ |
69
|
6 |
|
$this->filters[] = $filter; |
70
|
|
|
|
71
|
6 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get validation |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
3 |
|
public function getRules(): array |
80
|
|
|
{ |
81
|
3 |
|
return $this->rules; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
3 |
|
public function getRule(string $rule): Validatable |
88
|
|
|
{ |
89
|
3 |
|
return $this->rules[$rule]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add validation rules |
94
|
|
|
* |
95
|
|
|
* @param array $rules Array of Validatable instances |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
33 |
|
public function setRules(array $rules) |
100
|
|
|
{ |
101
|
33 |
|
$this->rules = []; |
102
|
|
|
|
103
|
33 |
|
foreach ($rules as $rule => $validator) { |
104
|
|
|
|
105
|
33 |
|
if (is_array($validator)) { |
106
|
18 |
|
$alert = $validator['alert'] ?? null; |
107
|
18 |
|
$validator = $validator['validator']; |
108
|
|
|
} |
109
|
|
|
|
110
|
33 |
|
$this->addRule($rule, $validator, $alert ?? null); |
111
|
|
|
} |
112
|
|
|
|
113
|
30 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add validation rule |
118
|
|
|
* |
119
|
|
|
* @param string $rule Name of the the rule being checked |
120
|
|
|
* @param Validatable $validator Validatable instance |
121
|
|
|
* @param string $alert Message to be displayed if validation fails |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
45 |
|
public function addRule(string $rule, Validatable $validator, ?string $alert = null) |
126
|
|
|
{ |
127
|
45 |
|
$this->rules[$rule] = $validator; |
128
|
|
|
|
129
|
45 |
|
if ($alert) { |
130
|
15 |
|
$this->addAlert($rule, $alert); |
131
|
|
|
} |
132
|
|
|
|
133
|
45 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get validation_messages |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
6 |
|
public function getAlerts(): array |
142
|
|
|
{ |
143
|
6 |
|
return $this->alerts; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
*/ |
149
|
9 |
|
public function getAlert(string $alert) |
150
|
|
|
{ |
151
|
9 |
|
return $this->alerts[$alert]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set validation messages |
156
|
|
|
* |
157
|
|
|
* @param string $alerts validation_messages |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
6 |
|
public function setAlerts(array $alerts) |
162
|
|
|
{ |
163
|
6 |
|
foreach ($alerts as $rule => $alert) { |
164
|
6 |
|
$this->addAlert($rule, $alert); |
165
|
|
|
} |
166
|
|
|
|
167
|
6 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set validation_messages |
172
|
|
|
* |
173
|
|
|
* @param string $alerts validation_messages |
174
|
|
|
* |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
24 |
|
public function addAlert(string $rule, string $alert) |
178
|
|
|
{ |
179
|
24 |
|
$this->alerts[$rule] = $alert; |
180
|
|
|
|
181
|
24 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* |
186
|
|
|
*/ |
187
|
36 |
|
public function filterInput($input) |
188
|
|
|
{ |
189
|
36 |
|
if (true === $this->validateInput($input)) { |
190
|
24 |
|
return $this->returnIfPassed($this->sanitizeInput($input)); |
191
|
|
|
} |
192
|
|
|
|
193
|
15 |
|
return $this->returnIfFailed(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* |
198
|
|
|
*/ |
199
|
36 |
|
protected function validateInput($input) |
200
|
|
|
{ |
201
|
36 |
|
$input = (array) $input; |
202
|
|
|
|
203
|
|
|
/** @var Validatable $validator */ |
204
|
36 |
|
foreach ($this->rules as $rule => $validator) { |
205
|
24 |
|
foreach ($input as $value) { |
206
|
|
|
|
207
|
24 |
|
if (true !== $validator->validate($value)) { |
208
|
15 |
|
$this->handleRuleViolation($rule); |
209
|
15 |
|
return false; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
24 |
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* |
219
|
|
|
*/ |
220
|
24 |
|
protected function sanitizeInput($input) |
221
|
|
|
{ |
222
|
24 |
|
$array = is_array($input); // check whether original input is an array |
223
|
24 |
|
$input = (array) $input; // cast input to array for simplicity |
224
|
|
|
|
225
|
24 |
|
foreach ($this->filters as $filter) { |
226
|
|
|
|
227
|
9 |
|
foreach ($input as &$value) { |
228
|
9 |
|
$value = $filter($value); |
229
|
|
|
} |
230
|
9 |
|
unset($value); |
231
|
|
|
} |
232
|
|
|
|
233
|
24 |
|
return $array ? $input : $input[0]; // return single item if original input was not an array |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* |
238
|
|
|
*/ |
239
|
24 |
|
protected function returnIfPassed($input) |
240
|
|
|
{ |
241
|
24 |
|
return $input; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* |
246
|
|
|
*/ |
247
|
15 |
|
protected function returnIfFailed() |
248
|
|
|
{ |
249
|
15 |
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* |
254
|
|
|
*/ |
255
|
15 |
|
protected function handleRuleViolation($rule) |
256
|
|
|
{ |
257
|
15 |
|
$this->violations[$rule] = $this->alerts[$rule] ?? ''; |
258
|
15 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the value of violations |
262
|
|
|
* |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
24 |
|
public function getViolations(): array |
266
|
|
|
{ |
267
|
24 |
|
return $this->violations; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* |
272
|
|
|
*/ |
273
|
18 |
|
public function clearViolations() |
274
|
|
|
{ |
275
|
18 |
|
$this->violations = []; |
276
|
18 |
|
} |
277
|
|
|
} |
278
|
|
|
|