php可扩展的验证类实例(可对邮件、手机号、URL等验证)

时间:2022-09-24 19:57:35

本文实例讲述了php可扩展的验证类。分享给大家供大家参考。具体分析如下:

这里介绍一个可扩展的php验证类,
类里面可以的各类验证可自行调整实现,现在为基本实现方式。
需要添加规则的话, 直接定义方法,方法名即为规则名称。具体参考使用方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require_once('./Validator.class.php');
$data = array(
  'nickname' => 'heno' ,
  'realname' => 'steven',
  'age' => 25,
  'mobile' => '1521060426');
$validator = new Validator($data);
$validator->setRule('nickname', 'required');
$validator->setRule('realname', array('length' => array(1,6), 'required'));
$validator->setRule('age', array('required', 'digit'));
$validator->setRule('mobile', array('mobile'));
$result = $validator->validate();
var_dump($result);
var_dump($validator->getResultInfo());

Validator.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
<?php
/**
 * Validator 数据验证类
 * @package library
 * @category library
 * @author Steven
 * @version 1.0
 */
/**
 * Validator 数据验证类
 * @package library
 * @category library
 * @author Steven
 * @version 1.0
 */
class Validator {
 /**
  * 待校验数据
  * @var array
  */
 private $_data;
 /**
  * 校验规则
  * @var array
  */
 private $_ruleList = null;
 /**
  * 校验结果
  * @var bool
  */
 private $_result = null;
 /**
  * 校验数据信息
  * @var array
  */
 private $_resultInfo = array();
 /**
  * 构造函数
  * @param array $data 待校验数据
  */
 public function __construct($data = null)
 {
  if ($data) {
   $this->_data = $data;
  }
 }
 /**
  * 设置校验规则
  * @param string $var 带校验项key
  * @param mixed $rule 校验规则
  * @return void
  */
 public function setRule($var, $rule)
 {
  $this->_ruleList[$var] = $rule;
 }
 /**
  * 检验数据
  * @param array $data
  * <code>
  * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);
  * $validator = new Validator($data);
  * $validator->setRule('nickname', 'required');
  * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));
  * $validator->setRule('age', array('required', 'digit'));
  * $result = $validator->validate();
  * var_dump($validator->getResultInfo());
  * </code>
  * @return bool
  */
 public function validate($data = null)
 {
  $result = true;
  /* 如果没有设置校验规则直接返回 true */
  if ($this->_ruleList === null || !count($this->_ruleList)) {
   return $result;
  }
  /* 已经设置规则,则对规则逐条进行校验 */
  foreach ($this->_ruleList as $ruleKey => $ruleItem) {
   /* 如果检验规则为单条规则 */
   if (!is_array($ruleItem)) {
    $ruleItem = trim($ruleItem);
    if (method_exists($this, $ruleItem)) {
     /* 校验数据,保存校验结果 */
     $tmpResult = $this->$ruleItem($ruleKey);
     if (!$tmpResult) {
      $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult;
      $result = false;
     }
    }
    continue;
   }
   /* 校验规则为多条 */
   foreach ($ruleItem as $ruleItemKey => $rule) {
    if (!is_array($rule)) {
     $rule = trim($rule);
     if (method_exists($this, $rule)) {
      /* 校验数据,设置结果集 */
      $tmpResult = $this->$rule($ruleKey);
      if (!$tmpResult) {
       $this->_resultInfo[$ruleKey][$rule] = $tmpResult;
       $result = false;
      }
     }
    } else {
     if (method_exists($this, $ruleItemKey)) {
      /* 校验数据,设置结果集 */
      $tmpResult = $this->$ruleItemKey($ruleKey, $rule);
      if (!$tmpResult) {
       $this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult;
       $result = false;
      }
     }
    }
   }
  }
  return $result;
 }
 /**
  * 获取校验结果数据
  * @return [type] [description]
  */
 public function getResultInfo()
 {
  return $this->_resultInfo;
 }
 /**
  * 校验必填参数
  * @param string $varName 校验项
  * @return bool
  */
 public function required($varName)
 {
  $result = false;
  if (is_array($this->_data) && isset($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 /**
  * 校验参数长度
  *
  * @param string $varName 校验项
  * @param array $lengthData array($minLen, $maxLen)
  * @return bool
  */
 public function length($varName, $lengthData)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $varLen = mb_strlen($this->_data[$varName]);
   $minLen = $lengthData[0];
   $maxLen = $lengthData[1];
   if ($varLen < $minLen || $varLen > $maxLen) {
    $result = true;
   }
  }
  return $result;
 }
 /**
  * 校验邮件
  * @param string $varName 校验项
  * @return bool
  */
 public function email($varName)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $email = trim($this->_data[$varName]);
   if (preg_match('/^[-\w]+?@[-\w.]+?$/', $email)) {
    $result = false;
   }
  }
  return $result;
 }
 /**
  * 校验手机
  * @param string $varName 校验项
  * @return bool
  */
 public function mobile($varName)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $mobile = trim($this->_data[$varName]);
   if (!preg_match('/^1[3458]\d{10}$/', $mobile)) {
    $result = false;
   }
  }
  return $result;
 }
 /**
  * 校验参数为数字
  * @param string $varName 校验项
  * @return bool
  */
 public function digit($varName)
 {
  $result = false;
  if ($this->required($varName) && is_numeric($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 /**
  * 校验参数为身份证
  * @param string $varName 校验项
  * @return bool
  */
 public function ID($ID)
 {
 }
 /**
  * 校验参数为URL
  * @param string $varName 校验项
  * @return bool
  */
 public function url($url)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $url = trim($this->_data[$varName]);
   if(!preg_match('/^(http[s]?::)?\w+?(\.\w+?)$/', $url)) {
    $result = false;
   }
  }
  return $result;
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。