ADD - DateValidator and PhoneNumberValidator
This commit is contained in:
parent
a0675ad7f1
commit
c36f2dccb5
@ -0,0 +1,82 @@
|
|||||||
|
package com.banesco.infraestructure.utils;
|
||||||
|
|
||||||
|
import java.time.DateTimeException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class DateValidator {
|
||||||
|
private final DateTimeFormatter dateFormatter;
|
||||||
|
private final Pattern datePattern;
|
||||||
|
private final int minYear;
|
||||||
|
private final int maxYear;
|
||||||
|
|
||||||
|
private DateValidator(Builder builder) {
|
||||||
|
this.dateFormatter = builder.dateFormatter;
|
||||||
|
this.datePattern = Pattern.compile(builder.dateFormatRegex);
|
||||||
|
this.minYear = builder.minYear;
|
||||||
|
this.maxYear = builder.maxYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||||
|
private String dateFormatRegex = "(\\d{4})/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])";
|
||||||
|
private int minYear = 1900;
|
||||||
|
private int maxYear = 2100;
|
||||||
|
|
||||||
|
public Builder withDateFormatter(String dateFormatterPattern) {
|
||||||
|
this.dateFormatter = DateTimeFormatter.ofPattern(dateFormatterPattern);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withDateFormatRegex(String dateFormatRegex) {
|
||||||
|
this.dateFormatRegex = dateFormatRegex;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withMinYear(int minYear) {
|
||||||
|
this.minYear = minYear;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder withMaxYear(int maxYear) {
|
||||||
|
this.maxYear = maxYear;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateValidator build() {
|
||||||
|
return new DateValidator(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidDateFormat(String date) {
|
||||||
|
return date != null && datePattern.matcher(date).matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidDate(String date) {
|
||||||
|
if (!isValidDateFormat(date)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
LocalDate parsedDate = LocalDate.parse(date, dateFormatter);
|
||||||
|
String formattedDate = parsedDate.format(dateFormatter);
|
||||||
|
return date.equals(formattedDate);
|
||||||
|
} catch (DateTimeException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidDate(int year, int month, int day) {
|
||||||
|
return year >= minYear && year <= maxYear && month >= 1 && month <= 12 && isValidDayOfMonth(year, month, day);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidDayOfMonth(int year, int month, int day) {
|
||||||
|
try {
|
||||||
|
LocalDate.of(year, month, day);
|
||||||
|
return true;
|
||||||
|
} catch (DateTimeException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package com.banesco.infraestructure.utils;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class PhoneNumberValidator {
|
||||||
|
|
||||||
|
private final Pattern pattern;
|
||||||
|
|
||||||
|
public PhoneNumberValidator(String phonePattern) {
|
||||||
|
pattern = Pattern.compile(phonePattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the phone number format.
|
||||||
|
*
|
||||||
|
* @param phoneNumber the phone number to validate.
|
||||||
|
* @return true if the phone number is valid, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isValid(String phoneNumber) {
|
||||||
|
if (phoneNumber == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return pattern.matcher(phoneNumber).matches();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user