Mudanças entre as edições de "Tiny Types"
De Grupo Acert
Linha 227: | Linha 227: | ||
end; | end; | ||
− | { TCPF } | + | { TCPF } |
− | class operator TCPF.Implicit(const CPF: string): TCPF; | + | class operator TCPF.Implicit(const CPF: string): TCPF; |
− | begin | + | begin |
− | + | Result.FUnformatted := Clear(CPF); | |
− | end; | + | end; |
− | function TCPF.GetFormatted: string; | + | function TCPF.GetFormatted: string; |
− | begin | + | begin |
− | + | Result := MaskUtils.FormatMaskText('000.000.000-00;0; ', FUnformatted); | |
− | end; | + | end; |
− | class operator TCPF.Implicit(const CPF: TCPF): string; | + | class operator TCPF.Implicit(const CPF: TCPF): string; |
− | begin | + | begin |
− | + | Result := CPF.FUnformatted; | |
− | end; | + | end; |
− | function TCPF.IsValid: Boolean; | + | function TCPF.IsValid: Boolean; |
− | const | + | const |
− | + | BLACKLIST: array[0..9] of string = ('00000000000', | |
− | + | '11111111111', | |
− | + | '22222222222', | |
− | + | '33333333333', | |
− | + | '44444444444', | |
− | + | '55555555555', | |
− | + | '66666666666', | |
− | + | '77777777777', | |
− | + | '88888888888', | |
− | + | '99999999999'); | |
− | var | + | var |
− | + | IsInvalidSize: Boolean; | |
− | + | IsBlacklisted: Boolean; | |
− | + | I: Integer; | |
− | + | D1, D2: Integer; | |
− | begin | + | begin |
− | + | IsInvalidSize := Length(FUnformatted) <> 11; | |
− | + | IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0; | |
− | + | if IsInvalidSize or IsBlacklisted then | |
− | + | begin | |
− | + | Result := False; | |
− | + | end | |
− | + | else | |
− | + | begin | |
− | + | D1 := 0; | |
− | + | for I := 1 to 9 do | |
− | + | D1 := D1 + (StrToInt(FUnformatted[I]) * (11 - I)); | |
− | + | D1 := 11 - (D1 mod 11); | |
− | + | if D1 >= 10 then | |
− | + | D1 := 0; | |
− | + | D2 := D1 * 2; | |
− | + | for I := 1 to 9 do | |
− | + | D2 := D2 + (StrToInt(FUnformatted[I]) * (12 - I)); | |
− | + | D2 := 11 - (D2 mod 11); | |
− | + | if D2 >= 10 then | |
− | + | D2 :=0; | |
− | + | Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 10, 2); | |
− | + | end; | |
− | end; | + | end; |
− | end. | + | end. |