<?php
/**
 * 校验输入参数
 */

namespace app\modules\common;

class Verify
{
    /**
     * 验证电话号码
     * @param unknown $strMobile
     */
    public static function verifyMobile($strMobile)
    {
        if (preg_match("/^(13|14|15|16|17|18|19)[0-9]{9}$/", $strMobile)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证邮箱
     * @param unknown $strEmail
     * @return boolean|mixed
     */
    public static function verifyEmail($strEmail)
    {
        if (empty($strEmail)) {
            return false;
        }

        return filter_var($strEmail, FILTER_VALIDATE_EMAIL);
    }

    /**
     * 验证URL
     * @param unknown $strUrl
     * @return boolean|mixed
     */
    public static function verifyUrl($strUrl)
    {
        if (empty($strUrl)) {
            return false;
        }

        return filter_var($strUrl, FILTER_VALIDATE_URL);
    }
    //校验字符组成种类数
    public static function passwordScore($str)
    {
        $score = 0;
        if (preg_match('/[a-zA-Z]/i', $str)) {
            $score++;
        }
        if (preg_match("/[0-9]/", $str)) {
            $score++;
        }
        if (preg_match("/(.[^a-zA-Z0-9])/i", $str)) {
            $score++;
        }
        return $score;
    }

    /*     13(老)号段：130、131、132、133、134、135、136、137、138、139
      14(新)号段：1410、1440、145、146、147、148、149
      15(新)号段：150、151、152、153、154、155、156、157、158、159
      16(新)号段：166
      17(新)号段：170、171、173、175、176、177、178、1740（0-5）、1740（6-9）、1740（10-12）
      18(3G)号段：180、181、182、183、184、185、186、187、188、189
      19(新)号段：198、199
     * */
    public static function phoneFormat($phone)
    {
        if (preg_match("/^(13|14|15|16|17|18|19)[0-9]{9}$/", $phone)) {
            return true;
        } else {
            return false;
        }
    }

    // 18位身份证校验码有效性检查
    public static function idCardChecksum18($idcard)
    {
        if (strlen($idcard) != 18) {
            return false;
        }
        $idcard_base = substr($idcard, 0, 17);
        if (self::idCardVerifyNumber($idcard_base) != strtoupper(substr($idcard, 17, 1))) {
            return false;
        } else {
            return true;
        }
    }
    //校验车牌是否合法
    public static function  isCarLicense($license)
    {
        if (empty($license)) {
            return false;
        }
        /*匹配民用车牌和使馆车牌
        判断标准
        1，第一位为汉字省份缩写
        2，第二位为大写字母城市编码
        3，后面是5位仅含字母和数字的组合*/
        $match = [];
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使]{1}[A-Z]{1}[0-9a-zA-Z]{5}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }

        /*匹配特种车牌(挂,警,学,领,港,澳)
        参考 https://wenku.baidu.com/view/4573909a964bcf84b9d57bc5.html*/
        $match = [];
        $regular = '/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{4}[挂警学领港澳]{1}$/u';
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }

        /*匹配武警车牌
        参考 https://wenku.baidu.com/view/7fe0b333aaea998fcc220e48.html*/
        $match = [];
        $regular = '/^WJ[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]?[0-9a-zA-Z]{5}$/ui';
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }

        /*匹配军牌
        参考 http://auto.sina.com.cn/service/2013-05-03/18111149551.shtml*/
        $match = [];
        $regular = "/[A-Z]{2}[0-9]{5}$/";
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }
        /*匹配新能源车辆6位车牌
        参考 https://baike.baidu.com/item/%E6%96%B0%E8%83%BD%E6%BA%90%E6%B1%BD%E8%BD%A6%E4%B8%93%E7%94%A8%E5%8F%B7%E7%89%8C
        小型新能源车*/
        $match = [];
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[DF]{1}[0-9a-zA-Z]{5}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }
        /*大型新能源车*/
        $match = [];
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{5}[DF]{1}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0])) {
            return true;
        }
        return false;
    }
    // 计算身份证校验码，根据国家标准GB 11643-1999
    public static function idCardVerifyNumber($idcard_base)
    {
        if (strlen($idcard_base) != 17) {
            return false;
        }
        //加权因子
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //校验码对应值
        $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        $checksum = 0;
        for ($i = 0; $i < strlen($idcard_base); $i++) {
            $checksum += substr($idcard_base, $i, 1) * $factor[$i];
        }
        $mod = $checksum % 11;
        $verify_number = $verify_number_list[$mod];
        return $verify_number;
    }


    //用户真实姓名校验
    public static function realNameFormat($username)
    {
        //最长姓名15个字
        if (!preg_match("/^[\x{4e00}-\x{9fa5}·]{2,15}$/u", $username)) {
            return false;
        } else {
            return true;
        }
    }

    //服务密码验证 6 位纯数字
    public static function servicePwdFormat($pwd)
    {
        if (!preg_match("/^[0-9]{6}$/u", $pwd)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 分页的页数参数验证
     *
     * @param $pageNum string|int 当前页数
     * @param int|string $defaultPageNum 默认值
     * @return int
     */
    public static function verifyPageNum($pageNum, $defaultPageNum = 1)
    {
        if (!empty($pageNum) && $pageNum > 0) {
            $defaultPageNum = $pageNum;
        }

        return (int)$defaultPageNum;
    }

    /**
     * 分页的每页显示的条数验证
     *
     * @param $pageSize string|int 条数
     * @param int|string $defaultPageSize 默认值
     * @return int
     */
    public static function verifyPageSize($pageSize, $defaultPageSize = 15)
    {
        if (!empty($pageSize) && $pageSize > 0 && $pageSize < 30) {

            $defaultPageSize = $pageSize;
        }

        return (int)$defaultPageSize;
    }


    /**
     * vin码的校验位
     * @param $strVin
     * @return bool
     */
    public static function verifyVin($strVin)
    {
        //数字值
        $strChar = '0123456789ABCDEFGHJKLMNPRSTUVWXYZ';
        $valChar = '012345678912345678123457923456789';

        //加权系数
        $coef = [
            8, 7, 6,
            5, 4, 3,
            2, 10, 0,
            9, 8, 7,
            6, 5, 4,
            3, 2
        ];
        //被除数
        $mod = 11;

        $len = strlen($strVin);
        if ($len != 17) {
            return false; //长度不对
        }

        $sum = 0;
        for ($i = 0; $i < $len; $i++) {
            $ind = strpos($strChar, substr($strVin, $i, 1));
            if ($ind === false) {
                return false;
            }

            $sum += substr($valChar, $ind, 1) * $coef[$i];
        }

        $r = $sum % $mod;
        if ($r == 10) {
            $r = 'X';
        }
        $code = substr($strVin, 8, 1);
        return $r == $code;
    }

    /**
     * 邮箱验证
     * @param string $email
     * @return bool
     */
    public static function checkEmail($email = '')
    {
        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            return true;
        } else {
            return false;
        }
    }
}
