-
Notifications
You must be signed in to change notification settings - Fork 0
/
Civil.Utils.Types.CPF.pas
559 lines (457 loc) · 13.3 KB
/
Civil.Utils.Types.CPF.pas
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
(***
* Civil.Utils.Types.CPF.pas;
*
* v1.0.0 (Beta)
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Eng.º Anderson Marques Ribeiro.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
unit Civil.Utils.Types.CPF;
interface
uses
SysUtils, Classes, Math, System.Variants, Generics.Collections, AnsiStrings,
RegularExpressions, Winapi.Windows, Civil.Utils.Types, Civil.Utils.Types.Endereco;
type
ECPF = class(Exception)
public
type
TCPFErroEnum =
(
ceStringCPFInvalida
);
constructor Create(ACodigoErro: TCPFErroEnum);
strict private
var
FCodigoErro: TCPFErroEnum;
public
property CodigoErro: TCPFErroEnum read FCodigoErro;
end; { ECPF }
TCPF = packed record
public
constructor Create(ACPF: AnsiString);
strict private
type
TCPFType = packed array[0..10] of TAnsiDigitoType;
TNumeroType = packed array[0..8] of TAnsiDigitoType;
TDigitoVerifType = packed array[0..1] of TAnsiDigitoType;
class var
FDiag,
FDiagExtracao,
FDiagCalcDigito: TRegEx;
FNumeroVal: TNumeroType;
FDigitoVal: TDigitoVerifType;
strict private
class constructor CreateClass;
function GetNumero: AnsiString;
procedure SetNumero(AValor: AnsiString);
public
property Numero: AnsiString read GetNumero write SetNumero;
function Digito: AnsiString; inline;
function Formatado: AnsiString;
function Estado: TEstadoArray; inline;
strict private
class function CalcularDigito(ACPF: AnsiString): SmallInt; overload; static;
public
class function CalcularDigito(ACPF: AnsiString; out ADigito: AnsiString): Boolean; overload; static;
class function ValidarCPF(ACPF: AnsiString): Boolean; static;
class function Extrair(ATexto: AnsiString; out AResultado: TDadosExtraidosArray): Integer; static;
class operator Implicit(ACPF: AnsiString): TCPF; overload;
class operator Implicit(ACPF: TCPF): AnsiString; overload;
class operator Implicit(ACPF: TCPF): Variant; overload;
class operator Equal(ACPF1, ACPF2: TCPF): Boolean; inline;
class operator NotEqual(ACPF1, ACPF2: TCPF): Boolean; inline;
strict private
var
case Byte of
0: (FCPF: TCPFType);
1: (FNumero: TNumeroType; FDigito: TDigitoVerifType);
end; { TCPF }
TCPFArray = array of TCPF;
function VarCPFCreate(ACPF: AnsiString): Variant;
const
NULL_CPF: TCPF = (FCPF: '00000000000');
implementation
uses
Civil.Utils.VariantIds;
(*
* ECPF.
* -----------------------------------------------------------------------------
*)
constructor ECPF.Create(ACodigoErro: TCPFErroEnum);
const
TABELA_MENSAGENS: array[TCPFErroEnum] of String =
(
'String não contém um CPF válido'
);
begin
FCodigoErro := ACodigoErro;
inherited Create(TABELA_MENSAGENS[ACodigoErro]);
end;
(*
* TCPF.
* -----------------------------------------------------------------------------
*)
constructor TCPF.Create(ACPF: AnsiString);
begin
Self := ACPF;
end;
class constructor TCPF.CreateClass;
const
PRODUCAO = '(?<parte1>[0-9]{3})\.?(?<parte2>[0-9]{3})\.?(?<parte3>[0-9]{3})-?(?<digito>[0-9]{2})';
begin
FDiag := TRegEx.Create('^' + PRODUCAO + '$', [roExplicitCapture, roCompiled]);
FDiagExtracao := TRegEx.Create(PRODUCAO, [roExplicitCapture, roCompiled]);
FDiagCalcDigito := TRegEx.Create('^([0-9]{9})|([0-9]{3}.[0-9]{3}.[0-9]{3})$', [roExplicitCapture, roCompiled]);
end;
function TCPF.GetNumero: AnsiString;
begin
Result := AnsiString(FNumero);
end;
procedure TCPF.SetNumero(AValor: AnsiString);
var
intDigito: SmallInt;
begin
intDigito := CalcularDigito(AValor);
if intDigito = -1 then
raise ECPF.Create(ceStringCPFInvalida);
AnsiStrings.StrPCopy(FNumero, AValor);
AnsiStrings.strPCopy(FDigito, Format('%.2d', [intDigito]));
end;
function TCPF.Digito: AnsiString;
begin
Result := AnsiString(FDigito);
end;
function TCPF.Formatado: AnsiString;
var
strNumero: AnsiString;
begin
strNumero := AnsiString(FNumero);
Result := Copy(FNumero, 1, 3) + '.' + Copy(FNumero, 4, 3) + '.' + Copy(FNumero, 7, 3) + '-' + AnsiString(FDigito);
end;
function TCPF.Estado: TEstadoArray;
begin
case StrToInt(FNumero[8]) of
0:
begin
// 0 Rio Grande do Sul
SetLength(Result, 1);
Result[0] := eRS;
end;
1:
begin
// 1 Distrito Federal, Goiás, Mato Grosso, Mato Grosso do Sul e Tocantins
SetLength(Result, 5);
Result[0] := eDF;
Result[1] := eGO;
Result[2] := eMT;
Result[3] := eMS;
Result[4] := eTO;
end;
2:
begin
// 2 Amazonas, Pará, Roraima, Amapá, Acre e Rondônia
SetLength(Result, 6);
Result[0] := eAM;
Result[1] := ePR;
Result[2] := eRR;
Result[3] := eAP;
Result[4] := eAC;
Result[5] := eRO;
end;
3:
begin
// 3 Ceará, Maranhão e Piauí
SetLength(Result, 3);
Result[0] := eCE;
Result[1] := eMA;
Result[2] := ePI;
end;
4:
begin
// 4 Paraíba, Pernambuco, Alagoas e Rio Grande do Norte
SetLength(Result, 4);
Result[0] := ePB;
Result[1] := ePE;
Result[2] := eAL;
Result[3] := eRN;
end;
5:
begin
// 5 Bahia e Sergipe
SetLength(Result, 2);
Result[0] := eBA;
Result[1] := eSE;
end;
6:
begin
// 6 Minas Gerais
SetLength(Result, 1);
Result[0] := eMG;
end;
7:
begin
// 7 Rio de Janeiro e Espírito Santo
SetLength(Result, 2);
Result[0] := eRJ;
Result[1] := eES;
end;
8:
begin
// 8 São Paulo
SetLength(Result, 1);
Result[0] := eSP;
end;
9:
begin
// 9 Paraná e Santa Catarina
SetLength(Result, 2);
Result[0] := ePR;
Result[1] := eSC;
end;
end;
end;
class function TCPF.CalcularDigito(ACPF: AnsiString): SmallInt;
var
i, int1, int2, intComp, intCoef: SmallInt;
begin
Result := -1;
if not FDiagCalcDigito.IsMatch(ACPF) then
Exit;
intComp := Length(ACPF);
int1 := 0;
intCoef := 10;
for i := 1 to intComp do
begin
Inc(int1, intCoef * StrToInt(ACPF[i]));
Dec(intCoef);
end;
int1 := int1 mod 11;
//if int1 = 10 then int1 := 0;
if int1 in [0, 1] then
int1 := 0
else
int1 := 11 - int1;
ACPF := ACPF + IntToStr(int1);
intComp := Length(ACPF);
int2 := 0;
intCoef := 11;
for i := 1 to intComp do
begin
Inc(int2, intCoef * StrToInt(ACPF[i]));
Dec(intCoef);
end;
int2 := int2 mod 11;
if int2 in [0, 1] then
int2 := 0
else
int2 := 11 - int2;
Result := StrToInt(IntToStr(int1) + IntToStr(int2));
end;
class function TCPF.CalcularDigito(ACPF: AnsiString; out ADigito: AnsiString): Boolean;
var
intDigito: SmallInt;
begin
intDigito := CalcularDigito(ACPF);
Result := False;
if intDigito = -1 then
Exit;
ADigito := Format('%.2d', [intDigito]);
Result := True;
end;
class function TCPF.ValidarCPF(ACPF: AnsiString): Boolean;
var
intDigito: SmallInt;
match: TMatch;
strTmp: AnsiString;
begin
match := FDiag.Match(ACPF);
Result := match.Success;
if Result then
with match.Groups do
begin
strTmp := Item[1].Value + Item[2].Value + Item[3].Value;
intDigito := CalcularDigito(strTmp);
if intDigito = -1 then
begin
Result := False;
Exit;
end;
if intDigito <> StrToInt(Item[4].Value) then
begin
Result := False;
Exit;
end;
AnsiStrings.StrPCopy(FNumeroVal, strTmp);
AnsiStrings.StrPCopy(FDigitoVal, Item[4].Value);
end;
end;
class function TCPF.Extrair(ATexto: AnsiString; out AResultado: TDadosExtraidosArray): Integer;
var
matches: TMatchCollection;
i: Integer;
begin
matches := FDiagExtracao.Matches(ATexto);
SetLength(AResultado, matches.Count);
for i := 0 to matches.Count - 1 do
with AResultado[i] do
begin
Dados := matches.Item[i].Value;
Posicao := matches.Item[i].Index;
Valido := ValidarCPF(matches.Item[i].Value);
end;
Result := matches.Count;
end;
class operator TCPF.Implicit(ACPF: AnsiString): TCPF;
begin
if not ValidarCPF(ACPF) then
raise ECPF.Create(ceStringCPFInvalida);
with Result do
begin
FNumero := FNumeroVal;
FDigito := FDigitoVal;
end;
end;
class operator TCPF.Implicit(ACPF: TCPF): AnsiString;
begin
with ACPF do
Result := AnsiString(FCPF);
end;
class operator TCPF.Implicit(ACPF: TCPF): Variant;
begin
Result := VarCPFCreate(ACPF);
end;
class operator TCPF.Equal(ACPF1, ACPF2: TCPF): Boolean;
begin
Result := AnsiCompareStr(AnsiString(ACPF1), AnsiString(ACPF2)) = 0;
end;
class operator TCPF.NotEqual(ACPF1, ACPF2: TCPF): Boolean;
begin
Result := AnsiCompareStr(AnsiString(ACPF1), AnsiString(ACPF2)) <> 0;
end;
(*
* TCPFVariant.
* -----------------------------------------------------------------------------
*)
type
PCPFDataRec = ^TCPFDataRec;
TCPFDataRec = packed record // Deve conter 16 bytes.
VType: TVarType; // 2 bytes;
VCPF: TCPF; // 11 bytes;
Unused: array[0..2] of Byte; // 3 byte;
end; { TCPFDataRec }
TCPFVariant = class(TInvokeableVariantType)
public
procedure Cast(var Dest: TVarData; const Source: TVarData); override;
procedure CastTo(var Dest: TVarData; const Source: TVarData; const AVarType: TVarType); override;
procedure Compare(const Left, Right: TVarData; var Relationship: TVarCompareResult); override;
function IsClear(const Data: TVarData): Boolean; override;
procedure Clear(var Data: TVarData); override;
procedure Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean); override;
function GetProperty(var Dest: TVarData; const Value: TVarData; const Name: String): Boolean; override;
end; { TCPFVariant }
var
__cpfVar: TCPFVariant;
function VarCPFCreate(ACPF: AnsiString): Variant;
begin
VarClear(Result);
with TCPFDataRec(Result) do
begin
VType := __cpfVar.VarType;
VCPF := ACPF;
end;
end;
procedure TCPFVariant.Cast(var Dest: TVarData; const Source: TVarData);
begin
case Source.VType of
varUString, varString:
begin
Dest.VType := VarType;
TCPFDataRec(Dest).VCPF := VarDataToStr(Source);
end;
varCPF:
Dest := Source;
else
RaiseCastError;
end;
end;
procedure TCPFVariant.CastTo(var Dest: TVarData; const Source: TVarData; const AVarType: TVarType);
begin
if Source.VType = VarType then
begin
case AVarType of
varUString, varString:
VarDataFromStr(Dest, String(TCPFDataRec(Source).VCPF));
else
RaiseCastError;
end;
end
else
RaiseCastError;
end;
procedure TCPFVariant.Compare(const Left, Right: TVarData; var Relationship: TVarCompareResult);
begin
{if TVersionDataRec(Left).VVersion > TVersionDataRec(Right).VVersion then
Relationship := crGreaterThan
else if TVersionDataRec(Left).VVersion = TVersionDataRec(Right).VVersion then
Relationship := crEqual
else
Relationship := crLessThan;}
case AnsiCompareStr(AnsiString(TCPFDataRec(Left).VCPF), AnsiString(TCPFDataRec(Right).VCPF)) of
-1: Relationship := crLessThan;
0: Relationship := crEqual;
1: Relationship := crGreaterThan;
end;
end;
function TCPFVariant.IsClear(const Data: TVarData): Boolean;
begin
Result := (Data.VType = varEmpty);
end;
procedure TCPFVariant.Clear(var Data: TVarData);
begin
Data.VType := varEmpty;
end;
procedure TCPFVariant.Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean);
begin
Dest := Source;
{with TCPFDataRec(Dest) do
begin
VType := VarType;
VCPF := TCPFDataRec(Source).VCPF;
end;}
end;
function TCPFVariant.GetProperty(var Dest: TVarData; const Value: TVarData; const Name: String): Boolean;
begin
Result := True;
with TCpfDataRec(Value).VCPF do
if CompareStr(Name, 'FORMATADO') = 0 then
Variant(Dest) := Formatado
else if CompareStr(Name, 'NUMERO') = 0 then
Variant(Dest) := AnsiString(Numero)
else if CompareStr(Name, 'DIGITO') = 0 then
Variant(Dest) := Digito
else
Result := False;
end;
initialization
__cpfVar := TCPFVariant.Create(varCPF);
finalization
__cpfVar.Free;
end.