using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using Gestor.Model.Common; using Gestor.Model.Validation; namespace Gestor.Model.Helper; public static class ValidationHelper { public static List> AddValue(this List> keyValuePairs, T name, T2 value, bool condition = true) { if (name == null || name.ToString() == string.Empty || !condition) { return keyValuePairs; } if (keyValuePairs == null) { keyValuePairs = AddValue(name, value); return keyValuePairs; } keyValuePairs.Add(new KeyValuePair(name, value)); return keyValuePairs; } public static List> AddValue(T name, T2 value, bool condition = true) { if (name == null || name.ToString() == string.Empty || !condition) { return new List>(); } return new List> { new KeyValuePair(name, value) }; } public static List> AddValue() { return new List>(); } public static string Clear(this string stringToClean) { if (stringToClean != null) { return Regex.Replace(stringToClean, "[^\\d]", string.Empty); } return null; } public static string Categoria(this Enum genericEnum) { MemberInfo[] member = genericEnum.GetType().GetMember(genericEnum.ToString()); if (member.Length == 0) { return genericEnum.ToString(); } object[] customAttributes = member[0].GetCustomAttributes(typeof(CategoryAttribute), inherit: false); if (!customAttributes.Any()) { return genericEnum.ToString(); } return ((CategoryAttribute)customAttributes.ElementAt(0)).Category; } public static string Length(this string stringValue, int maxLength) { if (stringValue.Length > maxLength) { return stringValue.Substring(0, maxLength); } return stringValue; } public static string Join(this IEnumerable stringValues, string separator) { return string.Join(separator, stringValues); } public static int Age(this DateTime birthDate) { DateTime date = Funcoes.GetNetworkTime().Date; int num = date.Year - birthDate.Year; if (birthDate > date.AddYears(-num)) { num--; } return num; } public static string GetValueExact(this List> keyValuePairs, string keyName) { return keyValuePairs.Find((KeyValuePair x) => x.Key.Substring(0, x.Key.Contains("|") ? x.Key.IndexOf("|", StringComparison.Ordinal) : x.Key.Length).Equals(keyName)).Value; } public static bool ValidacaoDocumento(this string cpfCnpj) { string text = cpfCnpj?.Clear(); if (string.IsNullOrEmpty(text)) { return false; } int[] array = new int[14]; int[] array2 = new int[2]; if (new string(text[0], text.Length) == text) { return false; } if (text.Length == 11) { for (int i = 0; i <= 10; i++) { array[i] = Convert.ToInt32(text.Substring(i, 1)); } for (int i = 0; i <= 1; i++) { int num = 0; for (int j = 0; j <= 8 + i; j++) { num += array[j] * (10 + i - j); } array2[i] = num * 10 % 11; if (array2[i] == 10) { array2[i] = 0; } } return (array2[0] == array[9]) & (array2[1] == array[10]); } if (text.Length != 14) { return false; } for (int i = 0; i <= 13; i++) { array[i] = Convert.ToInt32(text.Substring(i, 1)); } for (int i = 0; i <= 1; i++) { int num = 0; for (int j = 0; j <= 11 + i; j++) { num += array[j] * Convert.ToInt32("6543298765432".Substring(j + 1 - i, 1)); } array2[i] = num * 10 % 11; if (array2[i] == 10) { array2[i] = 0; } } return (array2[0] == array[12]) & (array2[1] == array[13]); } public static string Index(this string[] stringArray, int currentIndex) { if (stringArray == null || !stringArray.Any()) { return string.Empty; } if (currentIndex <= -1 || currentIndex >= stringArray.Length) { return string.Empty; } return stringArray[currentIndex]; } public static bool ValidacaoChassi(this string chassiNumber) { chassiNumber = chassiNumber?.ToUpper(); if (!string.IsNullOrEmpty(chassiNumber)) { if (chassiNumber.Length == 17 && !Regex.IsMatch(chassiNumber, "^0| |^.{4,}([0-9A-Z])\\1{5,}|[iIoOqQ]")) { return Regex.IsMatch(chassiNumber, "[0-9]{4}$"); } return false; } return true; } public static bool ValidacaoPlaca(this string numeroPlaca) { if (numeroPlaca == null) { return false; } numeroPlaca.FormataPlaca(); return true; } public static string FormataFipe(this string value) { if (!string.IsNullOrWhiteSpace(value)) { return Convert.ToUInt64(value.OnlyNumber()).ToString("000000\\-0"); } return ""; } public static string OnlyNumber(this string sentence) { if (sentence == null) { return null; } return new Regex("[^\\d]").Replace(sentence, ""); } public static bool ValidaFipe(this string value) { if (new Regex("^\\d{6}\\-\\d{1}$").IsMatch(value)) { return true; } return false; } public static bool ValidacaoFabricacao(this string ano) { if (!int.TryParse(ano, out var result)) { return false; } if (result > 1900) { return result <= Funcoes.GetNetworkTime().Year; } return false; } public static bool ValidacaoModelo(this string ano) { if (!int.TryParse(ano, out var result)) { return false; } if (result > 1900) { return result <= Funcoes.GetNetworkTime().Year + 1; } return false; } public static bool ValidacaoEmail(this string mail) { if (mail == null) { return false; } if (mail.StartsWith("'") || mail.EndsWith("'")) { string replacement = ""; mail = Regex.Replace(new Regex("[\\']").Replace(mail, replacement), "\\s+", ""); } if (new Regex("[à-ú]").IsMatch(mail)) { return false; } return new Regex("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$").IsMatch(mail); } public static bool ValidacaoPrefixo(this string prefix) { if (!int.TryParse(prefix, out var result)) { return false; } if (result == 20 || result == 23 || result == 25 || result == 26 || result == 29 || result == 30 || result == 36 || result == 39 || result == 40 || result == 50 || result == 52 || result == 56 || result == 57 || result == 58 || result == 59 || result == 60 || result == 70 || result == 72 || result == 76 || result == 78 || result == 80 || result == 90 || result < 11 || result > 99) { return false; } return true; } public static bool ValidacaoTelefone(this string number) { if (number != null) { return Regex.Match(number, "^(\\d{8,9}$|\\d{4,5}[- ]\\d{4}$|\\d{4}\\s\\d{3}\\s\\d{4})$").Success; } return false; } public static bool ValidacaoPrefixo(this string number, TipoTelefone tipo) { if (tipo == TipoTelefone.Internacional || tipo == TipoTelefone.Whatsapp || tipo == TipoTelefone.Gratuita || tipo == TipoTelefone.TarifaUnica) { return number.Trim().Length == 0; } return number.ValidacaoPrefixo(); } public static bool ValidacaoTelefone(this string number, TipoTelefone tipo) { switch (tipo) { case TipoTelefone.Internacional: case TipoTelefone.Whatsapp: if (number != null) { return Regex.IsMatch(number.Trim(), "^[0-9]{7,16}$"); } return false; case TipoTelefone.Gratuita: case TipoTelefone.TarifaUnica: if (number != null) { return Regex.IsMatch(number.Trim(), "^[0-9]{4,7}$"); } return false; case TipoTelefone.Celular: if (number != null) { return Regex.IsMatch(number.Trim(), "^\\d{4,5}[- ]?\\d{4}$"); } return false; default: return number.ValidacaoTelefone(); } } public static bool ValidacaoCep(this string postCode) { if (postCode != null) { return Regex.Match(postCode, "^[0-9]{5}-[0-9]{3}$").Success; } return false; } public static bool ValidacaoEstado(this string state) { if (!string.IsNullOrEmpty(state)) { return new string[27] { "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO" }.Contains(state.ToUpper()); } return false; } public static bool ValidacaoDataPassada(this DateTime? data) { return data < Funcoes.GetNetworkTime().Date; } public static bool ValidacaoOrgao(this string orgao) { if (!string.IsNullOrEmpty(orgao)) { return orgao.Length < 7; } return true; } public static bool ValidacaoCei(this string cei) { if (string.IsNullOrEmpty(cei)) { return false; } cei = cei.Trim(); cei = Regex.Replace(cei, "[^\\d]", ""); if (cei.Length != 12) { return false; } int num = 0; for (int i = 1; i < 12; i++) { int num2 = Convert.ToInt32("74185216374".Substring(i - 1, 1)); int num3 = Convert.ToInt32(cei.Substring(i - 1, 1)); num += num2 * num3; } int num4 = num / 10; int num5 = num - num / 10 * 10; num = num4 + num5; num5 = num - num / 10 * 10; int num6 = Convert.ToInt32(cei.Substring(11, 1)); int num7 = 10 - num5; return num6 == num7; } public static bool ValidacaoRne(this string rne) { return true; } public static bool ValidateCaepf(this string caepf) { return !string.IsNullOrEmpty(caepf); } public static int ToInt(this string stringValue) { int.TryParse(stringValue, out var result); return result; } public static string ClearCurrency(this string stringToClean) { if (string.IsNullOrEmpty(stringToClean)) { return string.Empty; } if (stringToClean.Count((char x) => x == '.') == 1 && stringToClean.Count((char x) => x == ',') == 0) { stringToClean = stringToClean.Replace(".", ","); } if (stringToClean.Count((char x) => x == '.') > 0 && stringToClean.Count((char x) => x == ',') == 1) { stringToClean = stringToClean.Replace(".", ""); } if (stringToClean.Count((char x) => x == '.') <= 1) { return Regex.Replace(stringToClean, "[^\\d\\,\\-]", string.Empty); } int num = stringToClean.Count((char x) => x == '.'); for (int i = 0; i < num; i++) { int num2 = stringToClean.IndexOf(".", StringComparison.CurrentCultureIgnoreCase); stringToClean = new StringBuilder(stringToClean) { [num2] = ((i < num - 1) ? 'a' : ',') }.ToString(); } return Regex.Replace(stringToClean, "[^\\d\\,\\-]", string.Empty); } public static decimal ToDecimal(this string currencyString, string currentCulture = "pt-BR") { decimal.TryParse(currencyString.ClearCurrency(), NumberStyles.Any, new CultureInfo(currentCulture), out var result); return result; } public static string DocumentoFornecedor(this string sentence) { if (sentence == null) { return null; } return new Regex("[^\\d]").Replace(sentence, ""); } public static float ToFloat(this string stringValue) { float.TryParse(stringValue, out var result); return result; } public static string FormataCep(this string cep) { if (string.IsNullOrWhiteSpace(cep) || Regex.IsMatch(cep, "^\\d{2}\\d{3}-\\d{3}$")) { return cep; } string input = Regex.Replace(cep, "\\D", ""); if (!Regex.IsMatch(input, "^\\d{8}$")) { return cep; } return Regex.Replace(input, "(\\d{5})(\\d{3})", "$1-$2"); } public static string FormataPlaca(this string placa) { if (string.IsNullOrWhiteSpace(placa) || Regex.IsMatch(placa.Trim(), "[^a-zA-Z0-9]{1,7}")) { return placa.Trim(); } return Regex.Replace(placa.Trim(), "[^a-zA-Z0-9]{1,7}", ""); } }