-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidate.class.php
306 lines (272 loc) · 8 KB
/
Validate.class.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
<?php
Class Validate {
private $fields = array();
private $rules = array();
private $error;
function __construct( $data, $rules )
{
$this->fields = $this->sanitize( $data );
$this->rules = $rules;
}
/**
* Finds errors by using the validation rules
* and other variables.
*/
public function validate()
{
try
{
// if there if no fields set an error;
if( empty( $this->fields ) )
throw new Exception( "There was no data to process" );
// if the fields are not an array something is wrong
if( !is_array( $this->fields ) )
throw new Exception( "There was no data to process" );
// Validate each form field
foreach ( $this->fields as $field => $value )
{
// if the field is empty check if it should be.
// if it shouldnt be throw an error.
if( empty( $value ) )
if( !empty( $this->rules[$field]['required'] ) )
throw new Exception( $this->formattedError( $field, "cannot be blank" ) );
// Else, if the field has a value and is declared in Rules
if ( !empty( $this->rules[$field] ) )
{
unset( $this->rules[$field]['required'] );
foreach( $this->rules[$field] as $rule => $ruleVal )
{
// find the right function to use within this instance
$func_find = array( $this, $rule );
// variables to pass into this function
$func_vars = array( $field, $value, $ruleVal );
// if a function with the same name as our rule is found run it
// otherwise throw an error
if( is_callable( $func_find ) )
call_user_func_array( $func_find, $func_vars );
else
throw new Exception( "Invalid validation rule" );
}
}
}
// catch any errors that were thrwon
} catch ( Exception $e )
{
// set the error to be called.
$this->error = $e->getMessage();
return false;
}
// end of the line it all works.
return true;
} // end function
/**
* Sanitize an array or string
* @param array|string $data form submited data to be sanitized
* @return array|string $data data that has been sanitized
*/
private function sanitize( $data )
{
$data = preg_replace('/\s+/', ' ', $data);
if( is_array( $data ) )
{
$data = array_map( 'trim', $data );
$data = filter_var_array( $data, FILTER_SANITIZE_STRING );
return $data;
} else {
$data = trim( $data );
$data = filter_var( $data, FILTER_SANITIZE_STRING );
return $data;
}
}
/**
* Set the error in a dispaly friendly format.
* @param string $field the form field
* @param string $msg the desired messege to show for the error.
* @param string $this->error formated error.
* @return string
*/
public function formattedError( $field = '', $msg )
{
$field = str_replace( "_", " ", $field );
$field = ucfirst( $field );
return empty( $field ) ? $field . ': ' . $msg : $msg;
}
/**
* Get the error messege set by setError()
* @param string $this->error error messege.
* @return string $this->error
*/
public function getError()
{
return $this->error;
}
/**
* Get the form fiels that have been hopefuly sanitized
* @return array fields that orginaliy came form the form
*/
public function getFields()
{
return $this->fields;
}
/* ----------------------- Rules Functions -----------------------*/
/**
* Throw Exception if field contains
* anything other than alphabetical characters.
*
* Usage: $rules = array( 'example_field' => array ( 'alpha' => true ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function alpha( $field, $value )
{
if( !preg_match( '/^[A-Z.]+$/i', $value ) )
throw new Exception( $this->formattedError( $field, "should contain only alphabetical characters" ) );
}
/**
* Throw Exception if field contains
* anything other than alphabetical characters and spaces.
*
* Usage: $rules = array( 'example_field' => array ( 'alpha_space' => true ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
public function alpha_space( $field, $value )
{
if( ctype_alpha( str_replace( ' ', '', $value ) ) === false )
throw new Exception( $this->formattedError( $field, "must contain letters and spaces only" ) );
}
/**
* Throw Exception if field contains
* anything other than numerical characters.
*
* Usage: $rules = array( 'example_field' => array ( 'numeric' => $number_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function numeric($field, $value)
{
if( !is_numeric( $value ) )
throw new Exception( $this->formattedError( $field, "should be a numeric value" ) );
}
/**
* Throw Exception if field contains
* anything other than an interval.
*
* Usage: $rules = array( 'example_field' => array ( 'validate_int' => $int_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function validate_int( $field, $value )
{
if( !filter_var( $value, FILTER_VALIDATE_INT ) )
throw new Exception( $this->formattedError( $field, "should be an integer value" ) );
}
/**
* Throw Exception if field contains
* anything other than a boolean value.
*
* Usage: $rules = array( 'example_field' => array ( 'validate_bool' => $bool_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function validate_bool( $field, $value )
{
if( !filter_var( $value, FILTER_VALIDATE_BOOLEAN ) )
throw new Exception( $this->formattedError( $field, "should be a true or false value" ) );
}
/**
* Throw Exception if field contains
* anything other than a float value.
*
* Usage: $rules = array( 'example_field' => array ( 'validate_float' => $float_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function validate_float( $field, $value )
{
if( !filter_var( $value, FILTER_VALIDATE_FLOAT ) )
throw new Exception( $this->formattedError( $field, "should be a float value" ) );
}
/**
* Throw Exception if field contains
* anything other than a valid url.
*
* Usage: $rules = array( 'example_field' => array ( 'validate_url' => $url_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function validate_url( $field, $value )
{
if( !filter_var( $value, FILTER_VALIDATE_URL ) )
throw new Exception( $this->formattedError( $field, "is not a valid url" ) );
}
/**
* Throw Exception if field contains
* less characters than the specified amount.
*
* Usage: $rules = array( 'example_field' => array ( 'min_length' => $number_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function min_length( $field, $value, $min_length )
{
if ( strlen( $value ) < $min_length )
throw new Exception( $this->formattedError( $field, "should be {$min_length} characters or longer" ) );
}
/**
* Throw Exception if field contains
* more characters than the specified amount.
*
* Usage: $rules = array( 'example_field' => array ( 'max_length' => $number_value ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function max_length( $field, $value, $max_length )
{
if( strlen($value) > $max_length )
throw new Exception( $this->formattedError( $field, "should be {$max_length} characters or shorter" ) );
}
/**
* Throw Exception if field contains
* anything other than the values within the provided array.
*
* Usage: $rules = array( 'example_field' => array ( 'from_array' => $array ) );
*
* @param callable $callback
* @param string $field
* @param string $value
* @return Exception
*/
private function from_array( $field, $value, $array )
{
if( !in_array( $value, $array ) )
throw new Exception( $this->formattedError( $field, "is not part of the allowed list" ) );
}
} // end class