Verify.php 6.1 KB
<?php

namespace Lib;


/**
 * 验证
 * @author:dc
 * @time 2023/3/10 16:03
 * Class Verify
 * @package Lib
 */
class Verify {

    /**
     * @var array
     */
    public array $data;

    /**
     * @var array
     */
    public array $message;

    /**
     * 别名
     * @var array
     */
    public array $alias;

    /**
     * 验证邮箱
     * @param $email
     * @return false|int
     * @author:dc
     * @time 2023/3/10 16:04
     */
    public static function sEmail($email){
        return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email);
    }

    /**
     * 验证手机
     * @param $mobile
     * @return false|int
     * @author:dc
     * @time 2023/3/13 11:00
     */
    public static function sMobile($mobile){
        return preg_match('/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/',$mobile);
    }

    /**
     * 验证域名
     * @param $domain
     * @return false|int
     * @author:dc
     * @time 2023/3/13 10:59
     */
    public static function sDomain($domain){
        return preg_match('/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/',$domain);
    }

    /**
     * 验证url
     * @param $url
     * @return false|int
     * @author:dc
     * @time 2023/3/13 11:00
     */
    public static function sUrl($url){
        return preg_match('/[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$/',$url);
    }

    /**
     * 验证身份证
     * @param $idCard
     * @return false|int
     * @author:dc
     * @time 2023/3/13 11:01
     */
    public static function sIdCard($idCard){
        return preg_match('/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/',$idCard);
    }




    /**
     * 验证数据
     * @param array $rule
     * @param array $message
     * @return array|false|float|int|mixed|null
     * @author:dc
     * @time 2023/3/10 17:56
     */
    public static function checks(array $rule, array $message=[]){
        $verify = new static();
        $verify->message = $message;

        $data=  app()->request();
        if(is_array($data)){
            $verify->data = &$data;
        }

        foreach ($rule as $key=>$item){
            // 别名
            $key = explode('|',$key);
            $verify->alias[$key[0]] = $key[1]??$key[0];
            // 验证规则
            foreach ($item as $ak=>$av){
                if(is_numeric($ak)){
                    $param = [$key[0]];
                    $ak = $av;
                }else{
                    $param = [$key[0],$av];
                }
                // 验证规则,不能以下划线开始
                if(str_starts_with($ak, '_') || !method_exists($verify,$ak)){
                    throw new Err($ak.':验证规则不存在',600);
                }
                if(!$verify->{$ak}(...$param)){
                    app()->e(
                        $verify->message[$key[0]][$ak]??['verify_'.$ak, [$verify->alias[$key[0]]??$key[0],print_r($av,true)]],
                        600
                    );
                }
            }
        }

        return $verify->data;
    }

    /**
     * 获取表单值
     * @param $key
     * @return mixed|string
     * @author:dc
     * @time 2023/3/10 18:03
     */
    private function _get($key){
        return $this->data[$key]??'';
    }

    /**
     * 必须
     * @param $key
     * @return false
     * @author:dc
     * @time 2023/3/13 10:11
     */
    public function required($key){
        return $this->_get($key) !== '';
    }


    /**
     * 验证长度
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:11
     */
    public function max($key,$value):bool {
        return mb_strlen($this->_get($key)) <= $value;
    }

    /**
     * 最小字符
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:11
     */
    public function min($key,$value):bool {
        return mb_strlen($this->_get($key)) >= $value;
    }


    /**
     * 是否是数组
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:11
     */
    public function array($key,$value):bool {
        return is_array($value);
    }


    /**
     * 整数
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:15
     */
    public function integer($key,$value):bool {
        return is_integer($value);
    }


    /**
     * 数字,整数,浮点,负数
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:16
     */
    public function number($key,$value):bool {
        return is_numeric($value);
    }


    /**
     * 正则
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:20
     */
    public function reg($key,$value):bool {
        return (bool) preg_match($value,$this->_get($key));
    }


    /**
     * 之间
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:31
     */
    public function between($key,$value){
        $data = $this->_get($key);
        return $data >= $value[0] && $data <= $value[1];
    }


    /**
     * 邮箱
     * @param $key
     * @return false|int
     * @author:dc
     * @time 2023/3/13 10:55
     */
    public function email($key){
        $emails = $this->_get($key);
        if(is_array($emails)){
            foreach ($emails as $email){
                if(!static::sEmail($email)){
                    return false;
                }
            }
        }else{
            return static::sEmail($emails);
        }
    }

    /**
     * 手机号
     * @param $key
     * @return false|int
     * @author:dc
     * @time 2023/3/13 11:06
     */
    public function mobile($key){
        $mobiles = $this->_get($key);
        if(is_array($mobiles)){
            foreach ($mobiles as $mobile){
                if(!static::sEmail($mobile)){
                    return false;
                }
            }
        }else{
            return static::sEmail($mobiles);
        }
    }




}