Mudanças entre as edições de "Tiny Types"

De Grupo Acert
Ir para: navegação, pesquisa
 
Linha 2: Linha 2:
  
 
Vamos usar como exemplo códigos fictícios, porém muito comum de encontrar em diversos projetos:
 
Vamos usar como exemplo códigos fictícios, porém muito comum de encontrar em diversos projetos:
 
  
 
   TPessoa = class
 
   TPessoa = class
Linha 12: Linha 11:
  
 
Com essa classe em mãos conseguimos usar o código normalmente.
 
Com essa classe em mãos conseguimos usar o código normalmente.
 
  
 
   var
 
   var
Linha 25: Linha 23:
  
 
Uma alternativa, que é a mais usada, seria uma classe utilitária onde teremos diversas funções para resolver esses pequenos problemas.
 
Uma alternativa, que é a mais usada, seria uma classe utilitária onde teremos diversas funções para resolver esses pequenos problemas.
 
  
 
   TClasseUtils = class
 
   TClasseUtils = class
Linha 35: Linha 32:
  
 
Dessa forma podemos validar o CNPJ em qualquer lugar do sistema, porém criamos dependência dessa TClasseUtils.
 
Dessa forma podemos validar o CNPJ em qualquer lugar do sistema, porém criamos dependência dessa TClasseUtils.
 
  
 
   if TClasseUtils.ValidaCNPJ(Pessoa.CNPJ) then
 
   if TClasseUtils.ValidaCNPJ(Pessoa.CNPJ) then
Linha 43: Linha 39:
  
 
Uma outra alternativa, que na minha opinião deixa o código mais elegante e expressivo, é através do uso de Tiny Types. Então, que tal isso:
 
Uma outra alternativa, que na minha opinião deixa o código mais elegante e expressivo, é através do uso de Tiny Types. Então, que tal isso:
 
  
 
   TPessoaNova = class
 
   TPessoaNova = class
Linha 50: Linha 45:
 
     property CNPJ: TCNPJ read FCNPJ write FCNPJ;
 
     property CNPJ: TCNPJ read FCNPJ write FCNPJ;
 
   end;
 
   end;
 
+
 
 
   var
 
   var
 
     Pessoa: TPessoaNova;
 
     Pessoa: TPessoaNova;
Linha 78: Linha 73:
  
 
   unit DocsBR;
 
   unit DocsBR;
 
+
 
 
   interface
 
   interface
 
+
 
 
   type
 
   type
 
+
 
 
     TCNPJ = record
 
     TCNPJ = record
      private
+
    private
 
       FUnformatted: string;
 
       FUnformatted: string;
 
       function GetFormatted: string;
 
       function GetFormatted: string;
 
+
 
 
     public
 
     public
 
       class operator Implicit(const CNPJ: string): TCNPJ;
 
       class operator Implicit(const CNPJ: string): TCNPJ;
 
       class operator Implicit(const CNPJ: TCNPJ): string;
 
       class operator Implicit(const CNPJ: TCNPJ): string;
 
    function IsValid(): Boolean;
 
 
    property Unformatted: string read FUnformatted;
 
    property Formatted: string read GetFormatted;
 
 
  end;
 
 
  TCPF = record
 
  private
 
    FUnformatted: string;
 
 
    
 
    
    function GetFormatted: string;
+
      function IsValid(): Boolean;
   public
+
 
     class operator Implicit(const CPF: string): TCPF;
+
      property Unformatted: string read FUnformatted;
    class operator Implicit(const CPF: TCPF): string;
+
      property Formatted: string read GetFormatted;
 
+
 
    function IsValid(): Boolean;
+
    end;
 
+
 
    property Unformatted: string read FUnformatted;
+
    TCPF = record
    property Formatted: string read GetFormatted;
+
    private
 
+
      FUnformatted: string;
   end;
+
    
 
+
      function GetFormatted: string;
 +
     
 +
     public
 +
      class operator Implicit(const CPF: string): TCPF;
 +
      class operator Implicit(const CPF: TCPF): string;
 +
 
 +
      function IsValid(): Boolean;
 +
 
 +
      property Unformatted: string read FUnformatted;
 +
      property Formatted: string read GetFormatted;
 +
    
 +
    end;
 +
 
 
   implementation
 
   implementation
 
+
 
 
   uses
 
   uses
 
     MaskUtils, StrUtils, SysUtils;
 
     MaskUtils, StrUtils, SysUtils;
 
+
 
 
   function Clear(const Doc: string): string;
 
   function Clear(const Doc: string): string;
 
   var
 
   var
 
     Letter: Char;
 
     Letter: Char;
 
   begin
 
   begin
 
+
 
 
     Result := '';
 
     Result := '';
 
+
 
 
     for Letter in Doc do
 
     for Letter in Doc do
 
     begin
 
     begin
 
+
 
 
       if Letter in ['0'..'9'] then
 
       if Letter in ['0'..'9'] then
 
         Result := Result + Letter;
 
         Result := Result + Letter;
 
+
 
 
     end;
 
     end;
 
+
 
 
   end;
 
   end;
 
+
 
 
   { TCNPJ }
 
   { TCNPJ }
 
+
 
 
   class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ;
 
   class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ;
 
   begin
 
   begin
 
 
     Result.FUnformatted := Clear(CNPJ);
 
     Result.FUnformatted := Clear(CNPJ);
 
 
   end;
 
   end;
 
+
 
 
   function TCNPJ.GetFormatted: string;
 
   function TCNPJ.GetFormatted: string;
 
   begin
 
   begin
 
 
     Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted);
 
     Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted);
 
 
   end;
 
   end;
 
+
 
 
   class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string;
 
   class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string;
 
   begin
 
   begin
 
 
     Result := CNPJ.FUnformatted;
 
     Result := CNPJ.FUnformatted;
 
 
   end;
 
   end;
 
+
 
 
   function TCNPJ.IsValid: Boolean;
 
   function TCNPJ.IsValid: Boolean;
 
   const
 
   const
Linha 178: Linha 168:
 
     D1, D2: Integer;
 
     D1, D2: Integer;
 
   begin
 
   begin
 
+
   
 
     IsInvalidSize := Length(FUnformatted) <> 14;
 
     IsInvalidSize := Length(FUnformatted) <> 14;
 
     IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0;
 
     IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0;
 +
   
 
     if IsInvalidSize or IsBlacklisted then
 
     if IsInvalidSize or IsBlacklisted then
 
     begin
 
     begin
Linha 187: Linha 178:
 
     else  
 
     else  
 
     begin
 
     begin
 
+
 
 
       D1 := 0;
 
       D1 := 0;
 
+
 
 
       for I := 1 to 12 do
 
       for I := 1 to 12 do
 
       begin
 
       begin
 
+
 
 
         if I < 5 then
 
         if I < 5 then
 
           D1 := D1 + (StrToInt(FUnformatted[I]) * (6 - I))
 
           D1 := D1 + (StrToInt(FUnformatted[I]) * (6 - I))
 
         else
 
         else
 
           D1 := D1 + (StrToInt(FUnformatted[I]) * (14 - I));
 
           D1 := D1 + (StrToInt(FUnformatted[I]) * (14 - I));
 
+
 
 
       end;
 
       end;
 
+
 
 
       D1 := 11 - (D1 mod 11);
 
       D1 := 11 - (D1 mod 11);
 
+
 
 
       if D1 >= 10 then
 
       if D1 >= 10 then
 
         D1 := 0;
 
         D1 := 0;
   
+
     
 
       D2:= D1 * 2;
 
       D2:= D1 * 2;
 
        
 
        
 
       for I := 1 to 12 do
 
       for I := 1 to 12 do
 
       begin
 
       begin
 
+
       
 
         if I < 6 then
 
         if I < 6 then
 
           D2 := D2 + (StrToInt(FUnformatted[I]) * (7 - I))
 
           D2 := D2 + (StrToInt(FUnformatted[I]) * (7 - I))
 
         else
 
         else
 
           D2 := D2 + (StrToInt(FUnformatted[I]) * (15 - I));
 
           D2 := D2 + (StrToInt(FUnformatted[I]) * (15 - I));
 
+
         
 
       end;
 
       end;
 
+
     
 
       D2 := 11 - (D2 mod 11);
 
       D2 := 11 - (D2 mod 11);
 
+
     
 
       if D2 >= 10 then D2 :=0;
 
       if D2 >= 10 then D2 :=0;
 
+
     
 
       Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2);
 
       Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2);
 
+
     
 
     end;
 
     end;
 
+
   
 
   end;
 
   end;
 
{ TCPF }
 
class operator TCPF.Implicit(const CPF: string): TCPF;
 
begin
 
 
  Result.FUnformatted := Clear(CPF);
 
 
end;
 
 
function TCPF.GetFormatted: string;
 
begin
 
 
  Result := MaskUtils.FormatMaskText('000.000.000-00;0; ', FUnformatted);
 
 
end;
 
 
class operator TCPF.Implicit(const CPF: TCPF): string;
 
begin
 
 
  Result := CPF.FUnformatted;
 
 
end;
 
 
function TCPF.IsValid: Boolean;
 
const
 
  BLACKLIST: array[0..9] of string = ('00000000000',
 
                                      '11111111111',
 
                                      '22222222222',
 
                                      '33333333333',
 
                                      '44444444444',
 
                                      '55555555555',
 
                                      '66666666666',
 
                                      '77777777777',
 
                                      '88888888888',
 
                                      '99999999999');
 
var
 
  IsInvalidSize: Boolean;
 
  IsBlacklisted: Boolean;
 
  I: Integer;
 
  D1, D2: Integer;
 
begin
 
 
    
 
    
   IsInvalidSize := Length(FUnformatted) <> 11;
+
   { TCPF }
   IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0;
+
  class operator TCPF.Implicit(const CPF: string): TCPF;
 +
   begin
 +
    Result.FUnformatted := Clear(CPF);
 +
  end;
 
    
 
    
   if IsInvalidSize or IsBlacklisted then
+
   function TCPF.GetFormatted: string;
 
   begin
 
   begin
     Result := False;
+
     Result := MaskUtils.FormatMaskText('000.000.000-00;0; ', FUnformatted);
   end
+
   end;
   else
+
    
 +
  class operator TCPF.Implicit(const CPF: TCPF): string;
 
   begin
 
   begin
 
+
    Result := CPF.FUnformatted;
     D1 := 0;
+
  end;
 
+
 
     for I := 1 to 9 do
+
  function TCPF.IsValid: Boolean;
      D1 := D1 + (StrToInt(FUnformatted[I]) * (11 - I));
+
  const
 +
    BLACKLIST: array[0..9] of string = ('00000000000',
 +
                                        '11111111111',
 +
                                        '22222222222',
 +
                                        '33333333333',
 +
                                        '44444444444',
 +
                                        '55555555555',
 +
                                        '66666666666',
 +
                                        '77777777777',
 +
                                        '88888888888',
 +
                                        '99999999999');
 +
  var
 +
    IsInvalidSize: Boolean;
 +
    IsBlacklisted: Boolean;
 +
    I: Integer;
 +
     D1, D2: Integer;
 +
  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);
+
      D1 := 11 - (D1 mod 11);
 
      
 
      
    if D1 >= 10 then
+
      if D1 >= 10 then
 
+
 
    D1 := 0;
+
      D1 := 0;
 
      
 
      
    D2 := D1 * 2;
+
      D2 := D1 * 2;
 
      
 
      
    for I := 1 to 9 do
+
      for I := 1 to 9 do
      D2 := D2 + (StrToInt(FUnformatted[I]) * (12 - I));
+
        D2 := D2 + (StrToInt(FUnformatted[I]) * (12 - I));
 
+
 
    D2 := 11 - (D2 mod 11);
+
      D2 := 11 - (D2 mod 11);
 
      
 
      
    if D2 >= 10 then
+
      if D2 >= 10 then
      D2 :=0;
+
        D2 :=0;
 
+
 
    Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 10, 2);
+
      Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 10, 2);
 
+
 
 +
    end;
 +
 
 
   end;
 
   end;
 
+
 
end;
+
  end.
 
+
end.
+

Edição atual tal como às 10h13min de 7 de janeiro de 2015