«

laravel参数验证规则扩展示例

时间:2024-2-19 17:22     作者:韩俊     分类: PHP


laravel参数验证规则扩展示例,如下:

<?php

namespace App\Library;

use App\Models\ProvinceCityCounty;
use Input;
use Validator;

class ValidatorRules {
    public static function extend() {
        self::identitycards();
        self::cloudFileId();
        self::id();
        self::strwidth();
        self::equal();
        self::mod();
        self::mobile();
        self::surname(); // 真是姓名
        self::video();
        self::idString();
        self::countyCode();
        self::existIsInt();
        self::existIsFloat();
        self::gtThan();
    }

    protected static function gtThan() {
        Validator::extend('gtThan', function ($attribute, $value, $parameters, $validator) {
            if (!is_numeric($value) || $value < Input::get($parameters[0])) {
                return false;
            }

            return true;
        }, '值错误');
    }

    protected static function identitycards() {
        //扩展身份证验证规则
        Validator::extend('identitycards', function ($attribute, $value, $parameters) {
            return preg_match('/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/', $value);
        });
        Validator::extend('telphone', function ($attribute, $value, $parameters) {
            return preg_match('/^1[34578][0-9]{9}$/', $value);
        });
    }

    protected static function cloudFileId() {
        Validator::extend('cloud_fileid', function ($attribute, $value, $parameters, $validator) {
            if (!is_string($value)) {
                return false;
            }

            $cloud_ids = Util::explode(',', $value);
            foreach ($cloud_ids as $cloud_id) {
                if (!Util::isCloudFileId($cloud_id)) {
                    return false;
                }
            }
            return true;
        }, '无效的云ID');
    }

    protected static function id() {
        Validator::extend('id', function ($attribute, $value, $parameters, $validator) {
            return !!preg_match('/^[1-9][\d]*$/', $value);
        }, '不是有效的ID类型');
    }

    protected static function strwidth() {
        Validator::extend('strwidth', function ($attribute, $value, $parameters, $validator) {
            $value = trim($value);
            Input::merge([$attribute => $value]);

            if (count($parameters) == 1) {
                $min = 0;
                $max = $parameters[0];
            } elseif (count($parameters) > 1) {
                $min = $parameters[0];
                $max = $parameters[1];
            } else {
                return false;
            }
            $strwidth = mb_strwidth($value, 'utf8');

            return ($min <= $strwidth) && ($strwidth <= $max);
        }, '字符长度超出了定义的长度');
    }

    protected static function equal() {
        Validator::extend('equal', function ($attribute, $value, $parameters, $validator) {
            return $value == $parameters[0];
        });
    }

    protected static function mod() {
        Validator::extend('mod', function ($attribute, $value, $parameters, $validator) {
            return 0 === ($value % $parameters[0]);
        });
    }

    protected static function mobile() {
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            $value = trim($value);

            $mobile = getValidMobile($value);

            if ($mobile) {
                Input::merge([$attribute => $mobile]);
                return true;
            }

            return false;
        }, '无效的手机号');
    }

    protected static function surname() {
        Validator::extend('surname', function ($attribute, $value, $parameters, $validator) {
            $value = trim($value);
            Input::merge([$attribute => $value]);

            return preg_match(Constant::get('REGEX.REAL_NAME'), $value);
        }, '请输入真实姓名');
    }

    protected static function video() {
        Validator::extend('video', function ($attribute, $value, $parameters, $validator) {
            if (!is_string($value)) {
                return false;
            }
            $value = trim($value);

            $video = json_decode($value);
            if (json_last_error()) {
                return false;
            }

            // 视频云ID
            if (empty($video->fileid) || !Util::isCloudFileId($video->fileid)) {
                return false;
            }

            // 视频名称
            if (empty($video->filename)) {
                return false;
            }

            // 视频大小
            if (empty($video->filesize) || (int)$video->filesize <= 0) {
                return false;
            }

            $video->filesize = (int)$video->filesize;

            // 视频截图云ID
            if (empty($video->thumb_fileid) || !Util::isCloudFileId($video->thumb_fileid)) {
                return false;
            }

            $video->is_h264 = isset($video->is_h264) ? (int)$video->is_h264 : 0;
            $video->allow_download = isset($video->allow_download) ? (int)$video->allow_download : 1;
            $video->duration = isset($video->duration) ? (int)$video->duration : 0;

            Input::merge([$attribute => $video]);

            return true;
        }, '视频信息无效');
    }

    protected static function idString() {
        Validator::extend('id_string', function ($attribute, $value, $parameters, $validator) {
            if (!is_string($value)) {
                return false;
            }
            $value = trim($value);

            $idList = Util::explode(',', $value);

            foreach ($idList as $id) {
                if (!(string)(int)$id) {
                    return false;
                }
            }

            Input::merge([$attribute => $value]);

            return true;
        }, '无效的ID字符串');
    }

    protected static function countyCode() {
        Validator::extend('county_code', function ($attribute, $value, $parameters, $validator) {
            return !!(new ProvinceCityCounty())->getByCountyCode($value);
        }, '无效的地区编码');
    }

    protected static function existIsInt() {
        Validator::extend('exist_is_int', function ($attribute, $value, $parameters, $validator) {
            return is_null($value) || is_numeric($value);
        }, '无效int型值');
    }

    protected static function existIsFloat() {
        Validator::extend('exist_is_float', function ($attribute, $value, $parameters, $validator) {
            return is_null($value) || is_numeric($value);
        }, '无效float型值');
    }
}

标签: php php教程

热门推荐