<?php

namespace app\enterprise\validate;

use think\Validate;

/**
 * Description of IntegralValidator
 * @author sgq
 */
class IntegralValidator extends Validate {

    protected $rule = [
        "name" => "regex:/^[\x{4e00}-\x{9fa5}]+$/u",
        "card_type" => "between:1,3",
        "card_number" => "checkCardNumber",
        "phone" => "mobile",
        "email" => "email"
    ];
    protected $message = [
        "name.regex" => "姓名只能是中文",
        "card_type.between" => "不存在的证件类型，请在列表中选择",
        "phone.mobile" => "请填写正确的手机号",
        "email.email" => "电子邮箱格式错误"
    ];

    protected function checkCardNumber($value, $rule, $data = []) {
        if (!$data["card_type"]) {
            return "填写证件号码前请先选择证件类型";
        }
        if ($data["card_type"] == 1) {
            $num = strlen($value);
            switch ($num) {
                case 15:
                case 18:
                    return \app\common\api\IdCardApi::isValid($value) ?: "身份证号码不合法";
                    break;
                default:
                    //return \app\common\api\IdCardApi::isValidExceptMainland($value) ?: "身份证号码不合法";
                    break;
            }
        }
        return "身份证号码不合法";
    }

}
