Telefone constructor

Telefone([
  1. dynamic numero
])

Construtor da classe Telefone.

numero é um parâmetro (int ou string) que representa o número de telefone. Se numero for fornecido e for válido, o número será formatado e atribuído às propriedades ddd, prefixo e sufixo.

Implementation

Telefone([dynamic numero]) {
  if (numero is Telefone) {
    ddd = numero.ddd;
    prefixo = numero.prefixo;
    sufixo = numero.sufixo;
    return;
  }
  ddd = "";
  prefixo = "";
  sufixo = "";
  if (Brasil.validarTelefone(numero)) {
    String t = "$numero".onlyNumbers;
    if (t.length > 11) {
      t = t.substring(0, 11);
    }

    if (t.length == 11) {
      ddd = t.substring(0, 2);
      prefixo = t.substring(2, 7);
      sufixo = t.substring(7, 11);
    } else if (t.length == 10) {
      ddd = t.substring(0, 2);
      prefixo = t.substring(2, 6);
      sufixo = t.substring(6, 10);
    } else if (t.length == 9) {
      prefixo = t.substring(0, 5);
      sufixo = t.substring(5, 9);
    } else if (t.length == 8) {
      prefixo = t.substring(0, 4);
      sufixo = t.substring(4, 8);
    }
  }
}