package com.kxs.common.core.valid.impl;

import com.kxs.common.core.valid.NormMonth;
import com.kxs.common.core.valid.Phone;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验处理器：做时间格式验证的核心类
 *
 * @author Pota1ovO
 * @date 2024-04-26
 */
public class NormMonthValidator implements ConstraintValidator<NormMonth, String> {

    // 注解对象
    private NormMonth normMonth;

    // 初始化【NormMonth】对象
    @Override
    public void initialize(NormMonth constraintAnnotation) {
        normMonth = constraintAnnotation;
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.isEmpty()){
            return false;
        }
        // 获取【Phone】对象的手机格式验证表达式
        String pattern = normMonth.pattern();
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(value);
        return matcher.matches();
    }
}

