审查视图

lib/Verify.php 10.5 KB
1  
邓超 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

namespace Lib;


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

    /**
1  
邓超 authored
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
     * @var array
     */
    public array $data;

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

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

    /**
1  
邓超 authored
32 33 34 35 36 37
     * 验证邮箱
     * @param $email
     * @return false|int
     * @author:dc
     * @time 2023/3/10 16:04
     */
1  
邓超 authored
38
    public static function sEmail(string $email){
邓超 authored
39 40 41 42 43 44 45 46 47
        if(preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email)){
            return true;
        }

        if(preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/',$email)){
            return true;
        }
        return false;
1  
邓超 authored
48 49
    }
1  
邓超 authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    /**
     * 验证手机
     * @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
     */
1  
邓超 authored
68
    public static function sDomain(string $domain){
1  
邓超 authored
69 70 71 72 73 74 75 76 77 78
        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
     */
1  
邓超 authored
79
    public static function sUrl(string $url){
1  
邓超 authored
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        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
1  
邓超 authored
99 100 101
     * @param array $data
     * @return array|false|float|int
     * @throws Err
1  
邓超 authored
102
     * @author:dc
1  
邓超 authored
103
     * @time 2023/4/6 10:27
1  
邓超 authored
104
     */
1  
邓超 authored
105
    public static function checks(array $rule, array $message=[], array $data = []){
1  
邓超 authored
106 107 108
        $verify = new static();
        $verify->message = $message;
1  
邓超 authored
109 110 111
        if(!$data){
            $data=  app()->request();
        }
1  
邓超 authored
112 113 114 115 116 117 118 119 120 121
        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){
1  
邓超 authored
122 123 124 125 126 127
                // 是否是匿名函数
                if($av instanceof \Closure){
                    $av($data[$key[0]], $key[0]);
                    continue;
                }
                else if(is_numeric($ak)){
1  
邓超 authored
128 129
                    $param = [$key[0]];
                    $ak = $av;
1  
邓超 authored
130 131
                }
                else{
1  
邓超 authored
132 133
                    $param = [$key[0],$av];
                }
1  
邓超 authored
134 135 136 137 138 139 140 141 142 143 144 145 146

                // 特殊验证
                switch ($ak){
                    case 'array|string':{
                        $ak = 'array_or_string';
                        break;
                    }
                    case 'string|array':{
                        $ak = "array_or_string";
                        break;
                    }
                }
1  
邓超 authored
147 148 149 150 151 152
                // 验证规则,不能以下划线开始
                if(str_starts_with($ak, '_') || !method_exists($verify,$ak)){
                    throw new Err($ak.':验证规则不存在',600);
                }
                if(!$verify->{$ak}(...$param)){
                    app()->e(
1  
邓超 authored
153
                        $verify->message[$key[0]][$ak]??['verify_'.$ak, [$verify->alias[$key[0]]??$key[0],is_array($av) ? implode(',',$av) : $av]],
1  
邓超 authored
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                        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]??'';
    }

    /**
1  
邓超 authored
175 176 177 178 179 180 181 182 183 184 185
     * 是否存在字段
     * @param $key
     * @return bool
     * @author:dc
     * @time 2023/3/13 14:16
     */
    private function _has($key){
        return isset($this->data[$key]);
    }

    /**
1  
邓超 authored
186 187
     * 必须
     * @param $key
1  
邓超 authored
188
     * @return bool
1  
邓超 authored
189
     * @author:dc
1  
邓超 authored
190
     * @time 2023/3/13 17:14
1  
邓超 authored
191 192
     */
    public function required($key){
1  
邓超 authored
193
        return $this->_get($key) !== '';
1  
邓超 authored
194 195
    }
1  
邓超 authored
196 197 198 199 200 201 202 203 204 205 206 207
    /**
     * 文件必须
     * @param $key
     * @return false|UploadFile[]
     * @author:dc
     * @time 2023/4/6 13:45
     */
    public function required_file($key){
        return app()->file($key);
    }

1  
邓超 authored
208 209 210 211 212 213 214 215 216 217

    /**
     * 验证长度
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:11
     */
    public function max($key,$value):bool {
1  
邓超 authored
218 219 220
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
221 222 223 224 225 226 227 228 229 230 231 232
        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 {
1  
邓超 authored
233 234 235 236
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;

1  
邓超 authored
237 238 239 240 241 242 243 244 245 246 247 248
        return mb_strlen($this->_get($key)) >= $value;
    }


    /**
     * 是否是数组
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:11
     */
1  
邓超 authored
249 250 251 252 253 254
    public function array($key):bool {
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;


        return is_array($this->_get($key));
1  
邓超 authored
255 256 257 258 259 260 261 262 263 264 265 266
    }


    /**
     * 整数
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:15
     */
    public function integer($key,$value):bool {
1  
邓超 authored
267 268 269 270
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;

1  
邓超 authored
271 272 273 274 275 276 277 278 279 280 281 282 283
        return is_integer($value);
    }


    /**
     * 数字,整数,浮点,负数
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:16
     */
    public function number($key,$value):bool {
1  
邓超 authored
284 285 286
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
287 288 289 290 291 292 293 294 295 296 297 298 299
        return is_numeric($value);
    }


    /**
     * 正则
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 10:20
     */
    public function reg($key,$value):bool {
1  
邓超 authored
300 301 302
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
303 304 305 306 307 308 309 310 311 312 313 314 315
        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){
1  
邓超 authored
316 317 318
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
319 320 321 322 323 324 325 326 327 328 329 330 331
        $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){
1  
邓超 authored
332 333 334
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        $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){
1  
邓超 authored
355 356 357
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;
1  
邓超 authored
358 359 360 361 362 363 364 365 366 367 368 369
        $mobiles = $this->_get($key);
        if(is_array($mobiles)){
            foreach ($mobiles as $mobile){
                if(!static::sEmail($mobile)){
                    return false;
                }
            }
        }else{
            return static::sEmail($mobiles);
        }
    }
1  
邓超 authored
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    /**
     * 是否在数组里面
     * @param $key
     * @param $value
     * @return bool
     * @author:dc
     * @time 2023/3/13 11:56
     */
    public function in($key,$value):bool {
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;

        return in_array($this->_get($key),$value);
    }

    /**
     * 是否是字符串
     * @param $key
     * @return bool
     * @author:dc
     * @time 2023/3/13 13:42
     */
    public function string($key):bool {
        // 如果字段不存在,则不验证
        if(!$this->_has($key)) return true;

        return is_string($this->_get($key));
    }

    /**
     * 数组或者字符串
     * @param $key
     * @return bool
     * @author:dc
     * @time 2023/3/13 14:04
     */
    public function array_or_string($key){
        return $this->array($key) || $this->string($key);
    }
1  
邓超 authored
410
1  
邓超 authored
411 412 413 414 415 416 417 418
    /**
     * 文件
     * @param $key
     * @param $value
     * @author:dc
     * @time 2023/3/13 14:27
     */
    public function file($key,$value){
1  
邓超 authored
419 420 421 422 423 424 425 426 427 428 429
        $files = app()->file($key);
        if($files){
            foreach ($files as $file){
                // 后缀
                if(!empty($value['ext'])){
                    $value['ext'] = is_string($value['ext']) ? [$value['ext']] : [];
                    if (!in_array($file->ext,$value['ext'])){
                        app()->e(['verify_file.ext',[$this->alias[$key],$file->name,implode('|',$value['ext'])]],600);
                        return false;
                    }
                }
1  
邓超 authored
430
1  
邓超 authored
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
                // 类型
                if(!empty($value['mine'])){
                    $value['mine'] = is_string($value['mine']) ? [$value['mine']] : [];
                    if (!in_array($file->mime,$value['mine'])){
                        app()->e(['verify_file.mine',[$this->alias[$key],$file->name,implode('|',$value['mine'])]],600);
                        return false;
                    }
                }

                // 大小
                if(!empty($value['size'])){
                    if ($file->size > $value['size']){
                        app()->e(['verify_file.size',[$this->alias[$key],$file->name,$value['size']]],600);
                        return false;
                    }
                }
            }
        }
        return true;
1  
邓超 authored
450
    }
1  
邓超 authored
451 452 453


}