Verifier.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00090 class FLEA_Helper_Verifier
00091 {
00101     function checkAll(& $data, & $rules, $skip = 0)
00102     {
00103         $result = array();
00104         foreach ($rules as $rule) {
00105             $name = $rule['name'];
00106             if ($skip === 1 || $skip === true || $skip === 'empty') {
00107                 if (!isset($data[$name]) || empty($data[$name])) { continue; }
00108             } elseif ($skip === 2 || $skip === 'noset') {
00109                 if (!isset($data[$name])) { continue; }
00110             }
00111 
00112             do {
00113                 // 如果 notNull 为 true,而 hasDefault 为 false,则表示该字段必须有内容
00114                 if (isset($rule['notNull'])) {
00115                     if ($rule['notNull']) {
00116                         if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
00117                         if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
00118                             break;
00119                         }
00120                         if (!isset($data[$name]) || is_null($data[$name])) {
00121                             $result[$name] = array('check' => 'notNull', 'rule' => $rule);
00122                             break;
00123                         }
00124                     } else {
00125                         if (!isset($data[$name]) || is_null($data[$name]) || $data[$name] === '') { break; }
00126                     }
00127                 }
00128                 if (isset($rule['notEmpty'])) {
00129                     if ($rule['notEmpty']) {
00130                         if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
00131                         if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
00132                             break;
00133                         }
00134                         if (!isset($data[$name]) || $data[$name] == '') {
00135                             $result[$name] = array('check' => 'notEmpty', 'rule' => $rule);
00136                             break;
00137                         }
00138                     } else {
00139                         if (!isset($data[$name]) || $data[$name] == '') {
00140                             break;
00141                         }
00142                     }
00143                 }
00144 
00145                 $ret = $this->check($data[$name], $rule);
00146                 if ($ret !== true) {
00147                     $result[$name] = array('check' => $ret, 'rule' => $rule);
00148                 }
00149             } while (false);
00150         }
00151         return $result;
00152     }
00153 
00162     function check($value, & $rule)
00163     {
00164         // 首先使用 simpleType 验证值(如果 simpleType 属性存在)
00165         $checkLength = false;
00166         $checkMinMax = false;
00167         $ret = 'simpleType';
00168         if (isset($rule['simpleType'])) {
00169             switch ($rule['simpleType']) {
00170             case 'C': // 长度小于等于 250 的字符串
00171                 if (strlen($value) > 250) { return $ret; }
00172                 $checkLength = true;
00173                 break;
00174             case 'N': // 数值或者浮点数
00175                 if (!is_numeric($value)) { return $ret; }
00176                 $checkMinMax = true;
00177                 break;
00178             case 'D': // 日期
00179                 $test = @strtotime($value);
00180                 if ($test === false || $test === -1) { return $ret; }
00181                 break;
00182             case 'I': // 整数
00183                 if (!is_numeric($value)) { return $ret; }
00184                 if (intval($value) != $value) { return $ret; }
00185                 $checkMinMax = true;
00186                 break;
00187             case 'X': // 长度大于 250 的字符串
00188             case 'B': // 二进制数据
00189                 $checkLength = true;
00190                 break;
00191             case 'T': // TimeStamp
00192             case 'L': // 逻辑布尔值
00193                 break;
00194             case 'R': // 自动增量或计数器
00195                 $checkMinMax = true;
00196                 break;
00197             default:
00198             }
00199         } else {
00200             $checkLength = true;
00201             $checkMinMax = true;
00202         }
00203 
00204         // 接着使用 complexType 验证值(如果 complexType 属性存在)
00205         $ret = 'complexType';
00206         if (isset($rule['complexType'])) {
00207             $func = 'is' . $rule['complexType'];
00208             if (!method_exists($this, $func)) {
00209                 FLEA::loadClass('FLEA_Exception_InvalidArguments');
00210                 __THROW(new FLEA_Exception_InvalidArguments('$rule[\'complexType\']',
00211                         $rule['complexType']));
00212                 return null;
00213             }
00214             if (!$this->{$func}($value)) { return $ret; }
00215         }
00216 
00217         // min/max/minLength/maxLength 验证
00218         if ($checkMinMax) {
00219             $ret = 'min';
00220             if (isset($rule['min']) && $value < $rule['min']) { return $ret; }
00221             $ret = 'max';
00222             if (isset($rule['max']) && $value > $rule['max']) { return $ret; }
00223         }
00224         $ret = 'length';
00225         if ($checkLength) {
00226             $ret = 'minLength';
00227             if (isset($rule['minLength']) && $rule['minLength'] > 0 &&
00228                 strlen($value) < $rule['minLength']) {
00229                 return $ret;
00230             }
00231             $ret = 'maxLength';
00232             if (isset($rule['maxLength']) && $rule['maxLength'] > 0 &&
00233                 strlen($value) > $rule['maxLength']) {
00234                 return $ret;
00235             }
00236         }
00237         $ret = null;
00238 
00239         return true;
00240     }
00241 
00245     function isNUMBER($value)
00246     {
00247         return is_numeric($value);
00248     }
00249 
00253     function isINT($value)
00254     {
00255         return strlen(intval($value)) == strlen($value) && is_numeric($value);
00256     }
00257 
00261     function isASCII($value)
00262     {
00263         $ar = array();
00264         $count = preg_match_all('/[\x20-\x7f]/', $value, $ar);
00265         return $count == strlen($value);
00266     }
00267 
00271     function isEMAIL($value)
00272     {
00273         return preg_match('/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/', $value) != 0;
00274     }
00275 
00279     function isDATE($value)
00280     {
00281         $test = @strtotime($value);
00282         return $test !== -1 && $test !== false;
00283     }
00284 
00288     function isTIME($value)
00289     {
00290         $test = strtotime($value);
00291         return $test !== -1 && $test !== false;
00292     }
00293 
00297     function isIPv4($value)
00298     {
00299         $test = ip2long($value);
00300         return $test !== -1 && $test !== false;
00301     }
00302 
00306     function isOCTAL($value)
00307     {
00308         return preg_match('/0[0-7]+/', $value) != 0;
00309     }
00310 
00314     function isBINARY($value)
00315     {
00316         return preg_match('/[01]+/', $value) != 0;
00317     }
00318 
00322     function isHEX($value)
00323     {
00324         return preg_match('/[0-9a-f]+/i', $value) != 0;
00325     }
00326 
00330     function isDOMAIN($value)
00331     {
00332         return preg_match('/[a-z0-9\.]+/i', $value) != 0;
00333     }
00334 
00338     function isANY()
00339     {
00340         return true;
00341     }
00342 
00346     function isSTRING()
00347     {
00348         return true;
00349     }
00350 
00354     function isALPHANUM($value)
00355     {
00356         return ctype_alnum($value);
00357     }
00358 
00362     function isALPHA($value)
00363     {
00364         return ctype_alpha($value);
00365     }
00366 
00370     function isALPHANUMX($value)
00371     {
00372         return preg_match('/[^a-z0-9_]/i', $value) == 0;
00373     }
00374 
00378     function isALPHAX($value)
00379     {
00380         return preg_match('/[^a-z\-]/i', $value) == 0;
00381     }
00382 }

Generated at Sat Feb 2 15:18:51 2008 for FleaPHP by  doxygen 1.5.3