-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidation.php
More file actions
148 lines (117 loc) · 3.53 KB
/
Copy pathValidation.php
File metadata and controls
148 lines (117 loc) · 3.53 KB
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
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Validation
*
* @author gatakka
*/
namespace GF;
class Validation {
private $_rules = array();
private $_errors = array();
public function setRule($rule, $value, $params = null, $name = null) {
$this->_rules[] = array('val' => $value, 'rule' => $rule, 'par' => $params, 'name' => $name);
return $this;
}
public function validate() {
$this->_errors = array();
if (count($this->_rules) > 0) {
foreach ($this->_rules as $v) {
if (!$this->$v['rule']($v['val'], $v['par'])) {
if ($v['name']) {
$this->_errors[] = $v['name'];
} else {
$this->_errors[] = $v['rule'];
}
}
}
}
return (bool) !count($this->_errors);
}
public function getErrors() {
return $this->_errors;
}
public function __call($a, $b) {
throw new \Exception('Invalid validation rule', 500);
}
public static function required($val) {
if (is_array($val)) {
return !empty($val);
} else {
return $val != '';
}
}
public static function matches($val1, $val2) {
return $val1 == $val2;
}
public static function matchesStrict($val1, $val2) {
return $val1 === $val2;
}
public static function different($val1, $val2) {
return $val1 != $val2;
}
public static function differentStrict($val1, $val2) {
return $val1 !== $val2;
}
public static function minlength($val1, $val2) {
return (mb_strlen($val1) >= $val2);
}
public static function maxlength($val1, $val2) {
return (mb_strlen($val1) <= $val2);
}
public static function exactlength($val1, $val2) {
return (mb_strlen($val1) == $val2);
}
public static function gt($val1, $val2) {
return ($val1 > $val2);
}
public static function lt($val1, $val2) {
return ($val1 < $val2);
}
public static function alpha($val1) {
return (bool) preg_match('/^([a-z])+$/i', $val1);
}
public static function alphanum($val1) {
return (bool) preg_match('/^([a-z0-9])+$/i', $val1);
}
public static function alphanumdash($val1) {
return (bool) preg_match('/^([-a-z0-9_-])+$/i', $val1);
}
public static function numeric($val1) {
return is_numeric($val1);
}
public static function email($val1) {
return filter_var($val1, FILTER_VALIDATE_EMAIL) !== false;
}
public static function emails($val1) {
if (is_array($val1)) {
foreach ($val1 as $v) {
if (!self::email($val1)) {
return false;
}
}
} else {
return false;
}
return true;
}
public static function url($val1) {
return filter_var($val1, FILTER_VALIDATE_URL) !== false;
}
public static function ip($val1) {
return filter_var($val1, FILTER_VALIDATE_IP) !== false;
}
public static function regexp($val1, $val2) {
return (bool) preg_match($val2, $val1);
}
public static function custom($val1, $val2) {
if ($val2 instanceof \Closure) {
return (boolean) call_user_func($val2, $val1);
} else {
throw new \Exception('Invalid validation function', 500);
}
}
}