summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Model/Model.Helper/ValidationHelper.cs
blob: 2420e7c351a9573794f76238d2737dec177b7fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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<KeyValuePair<T, T2>> AddValue<T, T2>(this List<KeyValuePair<T, T2>> keyValuePairs, T name, T2 value, bool condition = true)
		{
			if (name == null || name.ToString() == string.Empty || !condition)
			{
				return keyValuePairs;
			}
			if (keyValuePairs == null)
			{
				keyValuePairs = ValidationHelper.AddValue<T, T2>(name, value, true);
				return keyValuePairs;
			}
			keyValuePairs.Add(new KeyValuePair<T, T2>(name, value));
			return keyValuePairs;
		}

		public static List<KeyValuePair<T, T2>> AddValue<T, T2>(T name, T2 value, bool condition = true)
		{
			if (name == null || name.ToString() == string.Empty || !condition)
			{
				return new List<KeyValuePair<T, T2>>();
			}
			return new List<KeyValuePair<T, T2>>()
			{
				new KeyValuePair<T, T2>(name, value)
			};
		}

		public static List<KeyValuePair<string, string>> AddValue()
		{
			return new List<KeyValuePair<string, string>>();
		}

		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<object>())
			{
				return genericEnum.ToString();
			}
			return ((CategoryAttribute)customAttributes.ElementAt<object>(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>((char x) => x == '.') == 1)
			{
				if (stringToClean.Count<char>((char x) => x == ',') == 0)
				{
					stringToClean = stringToClean.Replace(".", ",");
				}
			}
			if (stringToClean.Count<char>((char x) => x == '.') > 0)
			{
				if (stringToClean.Count<char>((char x) => x == ',') == 1)
				{
					stringToClean = stringToClean.Replace(".", "");
				}
			}
			if (stringToClean.Count<char>((char x) => x == '.') <= 1)
			{
				return Regex.Replace(stringToClean, "[^\\d\\,\\-]", string.Empty);
			}
			int num = stringToClean.Count<char>((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<KeyValuePair<string, string>> keyValuePairs, string keyName)
		{
			KeyValuePair<string, string> keyValuePair = keyValuePairs.Find((KeyValuePair<string, string> 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<string>())
			{
				return string.Empty;
			}
			if (currentIndex <= -1 || currentIndex >= (int)stringArray.Length)
			{
				return string.Empty;
			}
			return stringArray[currentIndex];
		}

		public static string Join(this IEnumerable<string> 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<string>(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);
		}
	}
}