-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.SimpleValidate.php
212 lines (180 loc) · 4.29 KB
/
class.SimpleValidate.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
<?php
/*
* Simple PHP Input Validation Class
*
* This is a simple PHP class with the most common input validations.
* Supported Validations:
* - alphaNumeric : [A-Z] and [0-9]
* - alphaSpace : [A-Z] plus [SPACE]
* - alpha : [A-Z]
* - any : Any character
* - numeric, integer, float, email, ip, url, subDomain, domain
*
* Usage:
*
* $validations = array(
* [KEY] => array(
* [VALIDATION],
* [false/true], # Is the field required, default is true
* [CUSTOM ERROR], # Your Custom error
* ),
* );
*
* eg:
* require "class.SimpleValidate.php";
*
* $validations = array(
* "name" => array( "alphaSpace" ),
* "phone" => array( "integer", false ,"Phone number is invalid" ),
* );
*
* $result = SimpleValidate::validate( $_POST, $validations );
* if( $result==false )
* {
* $errors = SimpleValidate::getErrors();
* }
*
* https://github.com/twizdev/simple-php-input-validation
*
* Copyright 2017, TwizDev
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
Class SimpleValidate
{
private static $errors = array();
private static $defaultError = "field is invalid";
private static $defaultErrorRequired = "field required";
public static function validate( $inputs, $validations )
{
if( empty( $validations ) )
{
return true;
}
// RESET ERRORS
self::$errors = array();
// IF INPUT IS NOT ARRAY, CONVERT TO ARRAY
if( is_array( $inputs )===false )
{
$inputs = array( $inputs );
}
// ARRAY LOOP
foreach( $validations as $name=>$validation )
{
$custom_error = self::$defaultError;
$required = true;
if( is_array( $validation ) )
{
$thisValidation = $validation[0];
}else{
$thisValidation = $validation;
}
// REQUIRED
if( isset( $validation[1] ) )
{
$required = $validation[1];
}
// CUSTOM ERROR
if( isset( $validation[2] ) )
{
$custom_error = $validation[2];
}
// CHECK REQUIRED
if( $required && ( !isset( $inputs[ $name ] ) || empty( $inputs[ $name ] ) ) )
{
self::$errors[] = array(
$name => self::$defaultErrorRequired
);
continue;
// NOT REQUIRED CONTINUE
}elseif( !$required && ( !isset( $inputs[ $name ] ) || empty( $inputs[ $name ] ) ) ){
continue;
}
// CHECK IF VALIDATION EXISTS
if( is_callable( array( "self", "_{$thisValidation}" ) )===false )
{
self::$errors[] = array(
$name => "Unknown validation"
);
continue;
}
// VALIDATE
if( !call_user_func( "self::_{$thisValidation}", $inputs[ $name ] ) )
{
self::$errors[] = array(
$name => $custom_error
);
continue;
}
}
if( empty( self::$errors ) )
{
return true;
}else{
return false;
}
}
public static function getErrors()
{
return self::$errors;
}
protected static function _any( $input )
{
return true;
}
protected static function _alphaNumeric( $input )
{
return preg_match( "/^[a-z0-9À-ÿ]+$/i", $input );
}
protected static function _alphaSpace( $input )
{
return preg_match( "/^[ a-zÀ-ÿ]+$/i", $input );
}
protected static function _alpha( $input )
{
return preg_match( "/^[a-zÀ-ÿ]+$/i", $input );
}
protected static function _numeric( $input )
{
return is_numeric($input);
}
protected static function _integer( $input )
{
if( is_int( $input ) )
{
return true;
}elseif( is_string( $input ) ){
return ctype_digit( $input );
}
return false;
}
protected static function _float( $input )
{
if( is_float( $input + 0 ) )
{
return true;
}
return false;
}
protected static function _email( $input )
{
return filter_var( $input, FILTER_VALIDATE_EMAIL );
}
protected static function _ip( $input )
{
return filter_var( $input, FILTER_VALIDATE_IP );
}
protected static function _url( $input )
{
return filter_var( $input, FILTER_VALIDATE_URL );
}
protected static function _subDomain( $input )
{
return preg_match( "/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/i", $input );
}
protected static function _domain( $input )
{
return preg_match( "/^(?!\-)[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.(?!\d+)[a-zA-Z\d]{1,63}$/i", $input );
}
}