using Gestor.Model.Common; using Gestor.Model.Validation; using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; 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 = ValidationHelper.AddValue(name, value, true); 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 int Age(this DateTime birthDate) { DateTime date = Funcoes.GetNetworkTime().Date; int year = date.Year - birthDate.Year; if (birthDate > date.AddYears(-year)) { year--; } return year; } 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), false); if (!customAttributes.Any()) { return genericEnum.ToString(); } return ((CategoryAttribute)customAttributes.ElementAt(0)).Category; } public static string Clear(this string stringToClean) { if (stringToClean == null) { return null; } return Regex.Replace(stringToClean, "[^\\d]", string.Empty); } public static string ClearCurrency(this string stringToClean) { if (string.IsNullOrEmpty(stringToClean)) { return string.Empty; } if (stringToClean.Count((char x) => x == '.') == 1) { if (stringToClean.Count((char x) => x == ',') == 0) { stringToClean = stringToClean.Replace(".", ","); } } if (stringToClean.Count((char x) => x == '.') > 0) { if (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 num1 = stringToClean.IndexOf(".", StringComparison.CurrentCultureIgnoreCase); StringBuilder stringBuilder = new StringBuilder(stringToClean); stringBuilder[num1] = (i < num - 1 ? 'a' : ','); stringToClean = stringBuilder.ToString(); } return Regex.Replace(stringToClean, "[^\\d\\,\\-]", string.Empty); } public static string DocumentoFornecedor(this string sentence) { if (sentence == null) { return null; } return (new Regex("[^\\d]")).Replace(sentence, ""); } public static string FormataCep(this string cep) { if (string.IsNullOrWhiteSpace(cep) || Regex.IsMatch(cep, "^\\d{2}\\d{3}-\\d{3}$")) { return cep; } string str = Regex.Replace(cep, "\\D", ""); if (!Regex.IsMatch(str, "^\\d{8}$")) { return cep; } return Regex.Replace(str, "(\\d{5})(\\d{3})", "$1-$2"); } public static string FormataFipe(this string value) { if (string.IsNullOrWhiteSpace(value)) { return ""; } ulong num = Convert.ToUInt64(value.OnlyNumber()); return num.ToString("000000\\-0"); } 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}", ""); } public static string GetValueExact(this List> keyValuePairs, string keyName) { KeyValuePair keyValuePair = keyValuePairs.Find((KeyValuePair x) => x.Key.Substring(0, (x.Key.Contains("|") ? x.Key.IndexOf("|", StringComparison.Ordinal) : x.Key.Length)).Equals(keyName)); return keyValuePair.Value; } public static string Index(this string[] stringArray, int currentIndex) { if (stringArray == null || !stringArray.Any()) { return string.Empty; } if (currentIndex <= -1 || currentIndex >= (int)stringArray.Length) { return string.Empty; } return stringArray[currentIndex]; } public static string Join(this IEnumerable stringValues, string separator) { return string.Join(separator, stringValues); } public static string Length(this string stringValue, int maxLength) { if (stringValue.Length <= maxLength) { return stringValue; } return stringValue.Substring(0, maxLength); } public static string OnlyNumber(this string sentence) { if (sentence == null) { return null; } return (new Regex("[^\\d]")).Replace(sentence, ""); } public static decimal ToDecimal(this string currencyString, string currentCulture = "pt-BR") { decimal num; decimal.TryParse(currencyString.ClearCurrency(), NumberStyles.Any, new CultureInfo(currentCulture), out num); return num; } public static float ToFloat(this string stringValue) { float single; float.TryParse(stringValue, out single); return single; } public static int ToInt(this string stringValue) { int num; int.TryParse(stringValue, out num); return num; } 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 num1 = Convert.ToInt32("74185216374".Substring(i - 1, 1)); int num2 = Convert.ToInt32(cei.Substring(i - 1, 1)); num = num + num1 * num2; } int num3 = num / 10; int num4 = num - num / 10 * 10; num = num3 + num4; num4 = num - num / 10 * 10; int num5 = 10 - num4; return Convert.ToInt32(cei.Substring(11, 1)) == num5; } public static bool ValidacaoCep(this string postCode) { if (postCode == null) { return false; } return Regex.Match(postCode, "^[0-9]{5}-[0-9]{3}$").Success; } public static bool ValidacaoChassi(this string chassiNumber) { string upper; if (chassiNumber != null) { upper = chassiNumber.ToUpper(); } else { upper = null; } chassiNumber = upper; if (string.IsNullOrEmpty(chassiNumber)) { return true; } if (chassiNumber.Length != 17 || Regex.IsMatch(chassiNumber, "^0| |^.{4,}([0-9A-Z])\\1{5,}|[iIoOqQ]")) { return false; } return Regex.IsMatch(chassiNumber, "[0-9]{4}$"); } public static bool ValidacaoDataPassada(this DateTime? data) { DateTime? nullable = data; DateTime date = Funcoes.GetNetworkTime().Date; if (!nullable.HasValue) { return false; } return nullable.GetValueOrDefault() < date; } public static bool ValidacaoDocumento(this string cpfCnpj) { int j; int i; int num; string str; if (cpfCnpj != null) { str = cpfCnpj.Clear(); } else { str = null; } string str1 = str; if (string.IsNullOrEmpty(str1)) { return false; } int[] numArray = new int[14]; int[] numArray1 = new int[2]; if (new string(str1[0], str1.Length) == str1) { return false; } if (str1.Length == 11) { for (i = 0; i <= 10; i++) { numArray[i] = Convert.ToInt32(str1.Substring(i, 1)); } for (i = 0; i <= 1; i++) { num = 0; for (j = 0; j <= 8 + i; j++) { num = num + numArray[j] * (10 + i - j); } numArray1[i] = num * 10 % 11; if (numArray1[i] == 10) { numArray1[i] = 0; } } return numArray1[0] == numArray[9] & numArray1[1] == numArray[10]; } if (str1.Length != 14) { return false; } for (i = 0; i <= 13; i++) { numArray[i] = Convert.ToInt32(str1.Substring(i, 1)); } for (i = 0; i <= 1; i++) { num = 0; for (j = 0; j <= 11 + i; j++) { num = num + numArray[j] * Convert.ToInt32("6543298765432".Substring(j + 1 - i, 1)); } numArray1[i] = num * 10 % 11; if (numArray1[i] == 10) { numArray1[i] = 0; } } return numArray1[0] == numArray[12] & numArray1[1] == numArray[13]; } public static bool ValidacaoEmail(this string mail) { if (mail == null) { return false; } if (mail.StartsWith("'") || mail.EndsWith("'")) { string str = ""; mail = Regex.Replace((new Regex("[\\']")).Replace(mail, str), "\\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 ValidacaoEstado(this string state) { if (string.IsNullOrEmpty(state)) { return false; } return (new string[] { "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()); } public static bool ValidacaoFabricacao(this string ano) { int num; if (!int.TryParse(ano, out num)) { return false; } if (num <= 1900) { return false; } return num <= Funcoes.GetNetworkTime().Year; } public static bool ValidacaoModelo(this string ano) { int num; if (!int.TryParse(ano, out num)) { return false; } if (num <= 1900) { return false; } return num <= Funcoes.GetNetworkTime().Year + 1; } public static bool ValidacaoOrgao(this string orgao) { if (string.IsNullOrEmpty(orgao)) { return true; } return orgao.Length < 7; } public static bool ValidacaoPlaca(this string numeroPlaca) { if (numeroPlaca == null) { return false; } numeroPlaca.FormataPlaca(); return true; } public static bool ValidacaoPrefixo(this string prefix) { int num; if (!int.TryParse(prefix, out num)) { return false; } if (num != 20 && num != 23 && num != 25 && num != 26 && num != 29 && num != 30 && num != 36 && num != 39 && num != 40 && num != 50 && num != 52 && num != 56 && num != 57 && num != 58 && num != 59 && num != 60 && num != 70 && num != 72 && num != 76 && num != 78 && num != 80 && num != 90 && num >= 11 && num <= 99) { return true; } 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.ValidacaoPrefixo(); } return number.Trim().Length == 0; } public static bool ValidacaoRne(this string rne) { return true; } public static bool ValidacaoTelefone(this string number) { if (number == null) { return false; } return Regex.Match(number, "^(\\d{8,9}$|\\d{4,5}[- ]\\d{4}$|\\d{4}\\s\\d{3}\\s\\d{4})$").Success; } public static bool ValidacaoTelefone(this string number, TipoTelefone tipo) { if (tipo == TipoTelefone.Internacional || tipo == TipoTelefone.Whatsapp) { if (number == null) { return false; } return Regex.IsMatch(number.Trim(), "^[0-9]{7,16}$"); } if (tipo == TipoTelefone.Gratuita || tipo == TipoTelefone.TarifaUnica) { if (number == null) { return false; } return Regex.IsMatch(number.Trim(), "^[0-9]{4,7}$"); } if (tipo != TipoTelefone.Celular) { return number.ValidacaoTelefone(); } if (number == null) { return false; } return Regex.IsMatch(number.Trim(), "^\\d{4,5}[- ]?\\d{4}$"); } public static bool ValidaFipe(this string value) { if ((new Regex("^\\d{6}\\-\\d{1}$")).IsMatch(value)) { return true; } return false; } public static bool ValidateCaepf(this string caepf) { return !string.IsNullOrEmpty(caepf); } } }