validatie-klasse
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Validator Class
* Clean's input.
* Validates input.
*
* @author Thijs Damen
* @version 0.1
*
*/
Class Validator {
private $m_aCleanVars;
private $m_aErrors;
public function __construct() {
$this->m_aCleanVars = array();
$this->m_aErrors = array();
}
/**
* Function clean
* **Warning** This function cleans the input. makes sure var. types are set correctly. It does not however validate the input.
* Use function Validate() for that.
*
* @param array $p_aUncleanVars
* @return array $m_aCleanVars
*/
public function clean($p_aUncleanVars) {
foreach ($p_aUncleanVars as $aData) {
switch ($aData[0]) {
case "type_int":
$aData[1] = intval($aData[1]);
break;
case "type_uint":
($aData[1] = intval($aData[1])) < 0 ? 0 : $aData[1];
break;
case "type_num":
$aData[1] = strval($aData[1]) + 0;
break;
case "type_unum":
$aData[1] = strval($aData[1]) + 0;
$aData[1] = ($aData[1] < 0) ? 0 : $aData[1];
break;
case "type_str":
$aData[1] = trim(strval($aData[1]));
break;
case "type_bool":
$aData[1] = (bool) $aData[1];
break;
case "type_array":
$aData[1] = (is_array($aData[1])) ? $aData[1] : array();
break;
case "type_nohtml":
$aData[1] = htmlspecialchars(trim(strval($aData[1])));
break;
}
$this->m_aCleanVars[$aData[2]] = $aData[1];
}
return $this->m_aCleanVars;
}
/**
* Function validate
* Validates input.
*
* @param array $p_aUnvalidatedVars
*
*/
public function validate($p_aUnvalidatedVars) {
foreach ($p_aUnvalidatedVars as $aData) {
switch ($aData[0]) {
case "str_length":
if (!$this->strLength($aData[1], $aData[2], $aData[3])) {
$this->m_aErrors[] = $aData[4];
}
break;
case "is_empty":
if (!$this->isEmpty($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_num":
if (!$this->isNum($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_email":
if (!$this->isEmail($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_inrange":
if (!$this->isInRange($aData[1], $aData[2], $aData[3])) {
$this->m_aErrors[] = $aData[4];
}
break;
case "is_alpha":
if (!$this->isAlpha($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
}
}
}
/**
* function isEmpty
* checks if the given Value is empty. Handles Strings, Integers and Arrays
*
* @param mixed $p_mData
* @return false on failure., true on succes
*/
private function isEmpty($p_mData) {
if (is_string($p_mData)) {
if (trim($p_mData) == "") {
return false;
}
}
if (is_array($p_mData)) {
if (empty($p_mData)) {
return false;
}
}
if (is_integer($p_mData)) {
if (empty($p_mData)) {
return false;
}
}
if (empty($p_mData)) {
return false;
}
return true;
}
/**
* function isNum
* Checks if the given value is numeric. Can be either string or Int.
*
* @param mixed $p_mData
* @return false on failure, true on succes
*/
private function isNum($p_mData) {
if (!is_numeric($p_mData)) {
return false;
}
return true;
}
/**
* function isEmail
* Checks if the given value is an email.
*
* @param String $p_mData
* @return false on failure, true on succes
*/
private function isEmail($p_sData) {
if (is_string($p_sData)) {
$pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
if(!preg_match($pattern, $p_sData)) {
return false;
}
} else{
return false;
}
return true;
}
/**
* function isAlpha
* checks if the given value are only Alphabetic (a-Z) values.
*
* @param string $p_sData
* @return false on failure, true on succes
*/
private function isAlpha($p_sData) {
if (is_string($p_sData)) {
$pattern = "/^[a-zA-Z]+$/";
if (!preg_match($pattern, $p_sData)) {
return false;
}
} else {
return false;
}
return true;
}
/**
* function strLength
* Checks the given value against the given min and max length.
*
* @param string $p_sStr
* @param integer $p_iStart
* @param integer $p_iEnd
* @return false on failure, true on succes
*/
private function strLength($p_sStr, $p_iStart, $p_iEnd) {
if (is_string($p_sStr) && is_numeric($p_iStart) && is_numeric($p_iEnd)) {
if (strlen($p_sStr) >= $p_iStart && strlen($p_sStr) <= $p_iEnd) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* function isInRange
* Checks if the numeric value is in the given range of values. Can be String or Int
*
* @param mixed $p_mInt
* @param mixed $p_mMin
* @param mixed $p_mMax
* @return false on failure, true on succes
*/
private function isInRange($p_mInt, $p_mMin, $p_mMax) {
if (is_numeric ($p_mInt) && is_numeric($p_mMin) && is_numeric($p_mMax)) {
if (!($p_mInt >= $p_mMin) OR !($p_mInt <= $p_mMax)) {
return false;
}
} else {
return false;
}
return true;
}
/**
* function getErrors
* Returns errors
*/
public function getErrors() {
if (!empty($this->m_aErrors)) {
$sErrors = '';
foreach ($this->m_aErrors as $sError) {
$sErrors .= $sError . '<br />';
}
}
return $sErrors;
}
}
[/code]
Hoe te gebruiken:
Cleaner:
[code]
[code]<?php
$sString = 123;
$iInt = '1';
$bBool = 'waar';
$aArray = '123';
$unumNumber = -2;
$oValidator = new Validator;
$aClean = $oValidator->clean(array(
array('type_str', $sString, 'myString'),
array('type_int', $iInt, 'myInt'),
array('type_bool', $bBool, 'myBool'),
array('type_array', $aArray, 'myArray'),
array('type_unum', $unumNumber, 'myunsignedNum')
));
var_dump($aClean);
[/code]
Output:
[code]
array(5) {
["myString"]=>
string(3) "123"
["myInt"]=>
int(1)
["myBool"]=>
bool(true)
["myArray"]=>
array(0) {
}
["myunsignedNum"]=>
int(0)
}
[/code]
Zoals je ziet. alle typen zijn netjes de typen gewoorden waar ik om vroeg :) (ps. unsigned/signed is niet in php zelf uit te vinden. daarom op die manier.)
En de Validator:
[code]
[code]<?php
$sString = 'Langerdanzes';
$sAlpha = 'NietAlphabetisch123_+)';
$oValidator = new Validator;
$oValidator->Validate(array(
array('str_length', $sString, 1, 6, '$sString moet langer zijn dan 1 en korter dan 6'),
array('is_alpha', $sAlpha, '$sAlpha mag alleen alphabetische karakters bevatten')
));
if ($oValidator->getErrors()) {
echo $oValidator->getErrors();
}
[/code]
En de result:
[code]
$sString moet langer zijn dan 1 en korter dan 6
$sAlpha mag alleen alphabetische karakters bevatten
[/code]
Simpeler kan niet zou ik zeggen.
/**
* Validator Class
* Clean's input.
* Validates input.
*
* @author Thijs Damen
* @version 0.1
*
*/
Class Validator {
private $m_aCleanVars;
private $m_aErrors;
public function __construct() {
$this->m_aCleanVars = array();
$this->m_aErrors = array();
}
/**
* Function clean
* **Warning** This function cleans the input. makes sure var. types are set correctly. It does not however validate the input.
* Use function Validate() for that.
*
* @param array $p_aUncleanVars
* @return array $m_aCleanVars
*/
public function clean($p_aUncleanVars) {
foreach ($p_aUncleanVars as $aData) {
switch ($aData[0]) {
case "type_int":
$aData[1] = intval($aData[1]);
break;
case "type_uint":
($aData[1] = intval($aData[1])) < 0 ? 0 : $aData[1];
break;
case "type_num":
$aData[1] = strval($aData[1]) + 0;
break;
case "type_unum":
$aData[1] = strval($aData[1]) + 0;
$aData[1] = ($aData[1] < 0) ? 0 : $aData[1];
break;
case "type_str":
$aData[1] = trim(strval($aData[1]));
break;
case "type_bool":
$aData[1] = (bool) $aData[1];
break;
case "type_array":
$aData[1] = (is_array($aData[1])) ? $aData[1] : array();
break;
case "type_nohtml":
$aData[1] = htmlspecialchars(trim(strval($aData[1])));
break;
}
$this->m_aCleanVars[$aData[2]] = $aData[1];
}
return $this->m_aCleanVars;
}
/**
* Function validate
* Validates input.
*
* @param array $p_aUnvalidatedVars
*
*/
public function validate($p_aUnvalidatedVars) {
foreach ($p_aUnvalidatedVars as $aData) {
switch ($aData[0]) {
case "str_length":
if (!$this->strLength($aData[1], $aData[2], $aData[3])) {
$this->m_aErrors[] = $aData[4];
}
break;
case "is_empty":
if (!$this->isEmpty($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_num":
if (!$this->isNum($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_email":
if (!$this->isEmail($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
case "is_inrange":
if (!$this->isInRange($aData[1], $aData[2], $aData[3])) {
$this->m_aErrors[] = $aData[4];
}
break;
case "is_alpha":
if (!$this->isAlpha($aData[1])) {
$this->m_aErrors[] = $aData[2];
}
break;
}
}
}
/**
* function isEmpty
* checks if the given Value is empty. Handles Strings, Integers and Arrays
*
* @param mixed $p_mData
* @return false on failure., true on succes
*/
private function isEmpty($p_mData) {
if (is_string($p_mData)) {
if (trim($p_mData) == "") {
return false;
}
}
if (is_array($p_mData)) {
if (empty($p_mData)) {
return false;
}
}
if (is_integer($p_mData)) {
if (empty($p_mData)) {
return false;
}
}
if (empty($p_mData)) {
return false;
}
return true;
}
/**
* function isNum
* Checks if the given value is numeric. Can be either string or Int.
*
* @param mixed $p_mData
* @return false on failure, true on succes
*/
private function isNum($p_mData) {
if (!is_numeric($p_mData)) {
return false;
}
return true;
}
/**
* function isEmail
* Checks if the given value is an email.
*
* @param String $p_mData
* @return false on failure, true on succes
*/
private function isEmail($p_sData) {
if (is_string($p_sData)) {
$pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
if(!preg_match($pattern, $p_sData)) {
return false;
}
} else{
return false;
}
return true;
}
/**
* function isAlpha
* checks if the given value are only Alphabetic (a-Z) values.
*
* @param string $p_sData
* @return false on failure, true on succes
*/
private function isAlpha($p_sData) {
if (is_string($p_sData)) {
$pattern = "/^[a-zA-Z]+$/";
if (!preg_match($pattern, $p_sData)) {
return false;
}
} else {
return false;
}
return true;
}
/**
* function strLength
* Checks the given value against the given min and max length.
*
* @param string $p_sStr
* @param integer $p_iStart
* @param integer $p_iEnd
* @return false on failure, true on succes
*/
private function strLength($p_sStr, $p_iStart, $p_iEnd) {
if (is_string($p_sStr) && is_numeric($p_iStart) && is_numeric($p_iEnd)) {
if (strlen($p_sStr) >= $p_iStart && strlen($p_sStr) <= $p_iEnd) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* function isInRange
* Checks if the numeric value is in the given range of values. Can be String or Int
*
* @param mixed $p_mInt
* @param mixed $p_mMin
* @param mixed $p_mMax
* @return false on failure, true on succes
*/
private function isInRange($p_mInt, $p_mMin, $p_mMax) {
if (is_numeric ($p_mInt) && is_numeric($p_mMin) && is_numeric($p_mMax)) {
if (!($p_mInt >= $p_mMin) OR !($p_mInt <= $p_mMax)) {
return false;
}
} else {
return false;
}
return true;
}
/**
* function getErrors
* Returns errors
*/
public function getErrors() {
if (!empty($this->m_aErrors)) {
$sErrors = '';
foreach ($this->m_aErrors as $sError) {
$sErrors .= $sError . '<br />';
}
}
return $sErrors;
}
}
[/code]
Hoe te gebruiken:
Cleaner:
[code]
[code]<?php
$sString = 123;
$iInt = '1';
$bBool = 'waar';
$aArray = '123';
$unumNumber = -2;
$oValidator = new Validator;
$aClean = $oValidator->clean(array(
array('type_str', $sString, 'myString'),
array('type_int', $iInt, 'myInt'),
array('type_bool', $bBool, 'myBool'),
array('type_array', $aArray, 'myArray'),
array('type_unum', $unumNumber, 'myunsignedNum')
));
var_dump($aClean);
[/code]
Output:
[code]
array(5) {
["myString"]=>
string(3) "123"
["myInt"]=>
int(1)
["myBool"]=>
bool(true)
["myArray"]=>
array(0) {
}
["myunsignedNum"]=>
int(0)
}
[/code]
Zoals je ziet. alle typen zijn netjes de typen gewoorden waar ik om vroeg :) (ps. unsigned/signed is niet in php zelf uit te vinden. daarom op die manier.)
En de Validator:
[code]
[code]<?php
$sString = 'Langerdanzes';
$sAlpha = 'NietAlphabetisch123_+)';
$oValidator = new Validator;
$oValidator->Validate(array(
array('str_length', $sString, 1, 6, '$sString moet langer zijn dan 1 en korter dan 6'),
array('is_alpha', $sAlpha, '$sAlpha mag alleen alphabetische karakters bevatten')
));
if ($oValidator->getErrors()) {
echo $oValidator->getErrors();
}
[/code]
En de result:
[code]
$sString moet langer zijn dan 1 en korter dan 6
$sAlpha mag alleen alphabetische karakters bevatten
[/code]
Simpeler kan niet zou ik zeggen.