package com.ruoyi.ext.api.validate;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 自定义手机号码校验注解实现
 * 
 * @author ruoyi
 */
public class MobileValidator implements ConstraintValidator<MobileV, String>
{
    String pattern;
    Pattern compile;

    @Override
    public void initialize(MobileV constraintAnnotation) {
        pattern = "^1[3|4|5|6|7|8|9][0-9]\\d{8}$";
//        pattern = "^((\\(\\d{3}\\))|(\\d{3}\\-))?13\\d{9}$";

        compile = Pattern.compile(pattern);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)){
            return true;
        }
        return compile.matcher(value).matches();
    }
}