package com.rrtx.services.common.validator;

import com.rrtx.services.common.annotation.IsMobile;
import com.rrtx.services.common.entity.RegexpConstant;
import com.rrtx.services.common.utils.ServicesUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * ClassName: MobileValidator<br>
 * Description: 校验是否为合法的手机号码 <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 5:07 PM  xugz    文件初始创建
 */
public class MobileValidator implements ConstraintValidator<IsMobile, String> {

	@Override
	public void initialize(IsMobile isMobile) {
	}

	@Override
	public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
		try {
			if (StringUtils.isBlank(s)) {
				return true;
			} else {
				String regex = RegexpConstant.MOBILE_REG;
				return ServicesUtil.match(regex, s);
			}
		} catch (Exception e) {
			return false;
		}
	}
}
