forked from Stig1/PHP-Validator-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
128 lines (112 loc) · 2.86 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
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
<?php
/**
* Validator - simple server-side variable validation.
*
* @author Stig Rune Grønnestad
* @copyright Stig Rune Grønnestad @ 2013
* @version 1.0
*
*/
class Validator {
public $Vars;
public $Rules;
/**
* Constructor
* @param array $vars Array to validate, normally $_GET or $_POST
*/
public function __construct(array $vars) {
$this->Vars = $vars;
}
/**
* Define what keys to expect and what rules the values should follow.
* @param [type] $key
* @param [type] $rule rule for the value, for example numeric, string, min, max and so forth
*/
public function Expect($key, $rule) {
$this->Rules[$key] = $rule;
}
/**
* Run the validation based on the current var array and expected keys/rules.
* @return bool false if ANY of the rules failed validation, true otherwise
*/
public function Validate() {
foreach ($this->Rules as $key => $rule) {
$rules = explode(",", $rule);
foreach ($rules as $rule) {
if (strstr($rule, "=")) {
$words = explode("=", $rule);
$rule = $words[0];
$rule_value = $words[1];
}
switch (strtoupper(trim($rule))) {
case "REQ" :
case "REQUIRED" : {
if (! isset(trim($this->Vars[$key]))) return false;
break;
}
case "NUM" :
case "NUMERIC" : {
if (isset($this->Vars[$key])) {
if (! is_numeric($this->Vars[$key])) return false;
}
break;
}
case "MIN" :
case "MINIMUM" : {
if (isset($this->Vars[$key])) {
if (! is_numeric($this->Vars[$key])) return false;
if ($this->Vars[$key] < $rule_value) return false;
}
break;
}
case "MAX" :
case "MAXIMUM" : {
if (isset($this->Vars[$key])) {
if (! is_numeric($this->Vars[$key])) return false;
if ($this->Vars[$key] > $rule_value) return false;
}
break;
}
case "MIN_LEN" :
case "MIN_LENGTH" : {
if (isset($this->Vars[$key])) {
if (strlen($this->Vars[$key]) < $rule_value) return false;
}
break;
}
case "MAX_LEN" :
case "MAX_LENGTH" : {
if (isset($this->Vars[$key])) {
if (strlen($this->Vars[$key]) > $rule_value) return false;
}
break;
}
case "EMAIL" :
case "E-MAIL" : {
if (isset($this->Vars[$key])) {
if (! filter_var($this->Vars[$key], FILTER_VALIDATE_EMAIL)) return false;
}
break;
}
}
}
}
return true;
}
/**
* Override the get magic method to work with the array inserted at construct.
* @param [type] $var the variable to lookup
* @return [type] the var if it's found else NULL
*/
public function __get($var) {
if (isset($this->$var)) {
return $this->$var;
} else {
if (isset($this->Vars[$var])) {
return $this->Vars[$var];
}
}
return NULL;
}
}
?>