Esempio n. 1
0
 /**
  * 检查数据
  *
  * @param array|null $data 要验证的数据,默认为null验证表单数据
  *
  * @return array|bool|null
  */
 public final function check($data = null)
 {
     $this->data = $data ? $data : $this->request->getPostData();
     $rules = $this->rules;
     $token = Session::get('token');
     $post_token = $this->data['token'];
     if (false === array_search($post_token, $token)) {
         $this->error = 'token失效';
         return false;
     }
     $validator = new ValidateRule($this->data);
     foreach ($rules as $field => $rule) {
         if (array_key_exists('required', $rule) && !array_key_exists($field, $this->data)) {
             //必填项
             $validator->checkRequired($field, '', $rule['required']);
             return false;
         }
         $value = $this->data[$field];
         $xss = true;
         foreach ($rule as $k => $v) {
             if ($k == 'xss') {
                 $xss = $v;
                 continue;
             }
             $method = 'check' . ucfirst($k);
             if (!method_exists($validator, $method)) {
                 if (is_callable($v[0])) {
                     //如果第一个参数是函数则使用回调函数
                     $method = 'checkCallback';
                 }
             }
             if (is_array($v)) {
                 array_unshift($v, $field, $value);
                 $re = call_user_func_array(array($validator, $method), $v);
             } else {
                 $re = call_user_func_array(array($validator, $method), array($field, $value, $v));
             }
             if ($re !== true) {
                 $this->error = $re;
                 return false;
             }
         }
         if ($xss) {
             $this->cleanXss($this->data[$field]);
         }
     }
     return $this->data;
 }