码迷,mamicode.com
首页 > Web开发 > 详细

php批量验证类

时间:2018-04-25 20:05:31      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:php批量验证类

  • 代码:
    <?php
    /**
    * 批量验证类
    * @author [lee] <[<www.dollarphp.com>]>
    * 1、验证是否为空
    * 2、验证数据类型
    * 3、验证长度是否达标
    * 4、验证是否符合正则匹配
    */
    class validation{
    private $data;
    private $msg = array();
    public function __construct($main){
        foreach($main as $k=>$v){
            $this->data = $k;
            $bool = $v[0];
            $type = $v[1];
            $len_arr = $v[2];
            $preg = $v[3];
            $this->isEmpty($bool)->dataType($type)->dataLength($len_arr)->pregMath($preg);
        }
    }
    private function isEmpty($bool){
        $data = $this->data;
        if($bool){
            if(empty($data)){
                $msg = "‘{$data}‘不能为空";
                array_push($this->msg,$msg);
            }
        }
        return $this;
    }
    private function dataType($type){
        $data = $this->data;
        if($type==‘bool‘){
            if(!is_bool($data)){
                $msg = "‘{$data}‘不能为非布尔值";
                array_push($this->msg,$msg);
            }
        }elseif($type==‘string‘){
            if(!is_string($data)){
                $msg = "‘{$data}‘不能为非字符串值";
                array_push($this->msg,$msg);
            }
        }elseif($type==‘int‘){
            if(!is_int($data)){
                $msg = "‘{$data}‘不能为非整型值";
                array_push($this->msg,$msg);
            }
        }elseif($type==‘float‘){
            if(!is_float($data)){
                $msg = "‘{$data}‘不能为非浮点型值";
                array_push($this->msg,$msg);
            }
        }elseif($type==‘array‘){
            if(!is_array($data)){
                $msg = "‘{$data}‘不能为非数组值";
                array_push($this->msg,$msg);
            }
        }elseif($type==‘object‘){
            if(!is_object($data)){
                $msg = "‘{$data}‘不能为非对象值";
                array_push($this->msg,$msg);
            }
        }
        return $this;
    }
    private function dataLength($len_arr){
        $data = $this->data;
        $min = abs($len_arr[0]);
        $max = abs($len_arr[1]);
        $type = gettype($data);
        if($type==‘string‘){
            $len = strlen($data);
        }elseif($type==‘integer‘){
            $len = strlen($data);
        }elseif($type==‘double‘){
            $len0 = strlen($data);
            $len1 = explode(‘.‘,$len0);
            $len = $len1[1];
        }elseif($type==‘array‘){
            $len = count($data);
        }elseif($type==‘object‘){
            $data = (array)$data;
            $len = count($data);
        }
        if(!($len >= $min && $len <= $max)){
            $msg = "‘{$data}‘长度不能小于{$min},不能大于{$max}";
            array_push($this->msg,$msg);
        }
        return $this;
    }
    private function pregMath($preg){
        $data = $this->data;
        if(!empty($preg)){
            if(!preg_match($preg,$data)){
                $msg = "‘{$data}‘格式不匹配‘{$preg}‘";
                array_push($this->msg,$msg);
            }
        }
    }
    public function checkData(){
        $msg = $this->msg;
        return $msg;
    }
    }
    $data = array(
        ‘lee‘=>array(
                true,
                ‘string‘,
                array(5,20),
                ‘/^a.*/‘
            ),
        ‘hello‘=>array(
                false,
                ‘int‘,
                array(8,20),
                ‘/^a\s*/‘
            )
    );
    $validation = new validation($data);
    $ret = $validation->checkData();
    var_dump($ret);
  • 输出:
    array(5) {
    [0]=>
    string(41) "‘lee‘长度不能小于5,不能大于20"
    [1]=>
    string(28) "‘lee‘格式不匹配‘/^a.*/‘"
    [2]=>
    string(28) "‘hello‘不能为非整型值"
    [3]=>
    string(43) "‘hello‘长度不能小于8,不能大于20"
    [4]=>
    string(31) "‘hello‘格式不匹配‘/^a\s*/‘"
    }
  • php批量验证类

    标签:php批量验证类

    原文地址:http://blog.51cto.com/12173069/2107822

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!