-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.php
74 lines (56 loc) · 1.79 KB
/
validator.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
<?php
class Validator {
private $errorString;
public function isValid($var, $type, $name = NULL) {
switch($type) {
case "email":
return $this->sanitiseEmail($var);
break;
case "string":
return $this->sanitiseString($var,$name);
break;
case "text":
return $this->sanitiseText($var,$name);
break;
default:
return false;
break;
}
}
public function getError() {
return $this->errorString;
}
private function sanitiseEmail($email) {
$email = strtolower($email);
if (empty($email)) {
$this->errorString = 'Email must not be empty';
return false;
}
if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
$this->errorString = 'Email is not valid';
return false;
}
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
private function sanitiseText($string, $name) {
$string = filter_var($string,FILTER_SANITIZE_STRING);
if ( empty($string) ) {
$this->errorString = $name . ' must not be empty';
return false;
}
return $string;
}
private function sanitiseString($string, $name) {
$string = filter_var($string,FILTER_SANITIZE_STRING);
$string = str_replace( array("\r","\n",":"), array(" "," "," "), $string );
if ( empty($string) ) {
$this->errorString = $name . ' must not be empty';
return false;
}
if ( strlen($string) > 70 ) {
$this->errorString = $name . ' is too long.';
return false;
}
return $string;
}
}