detectDocumentType static method

int detectDocumentType(
  1. String numero
)

Detecta automaticamente o tipo do documento baseado no número Retorna 1 para CPF (11 dígitos) e 2 para CNPJ (14 dígitos) Lança ArgumentError se o documento não for válido

Implementation

static int detectDocumentType(String numero) {
  if (numero.isEmpty) {
    throw ArgumentError('Número de documento não pode ser vazio');
  }

  final cleanNumber = cleanDocumentNumber(numero);

  if (cleanNumber.length == tamanhoCpf) {
    return tipoCpf;
  } else if (cleanNumber.length == tamanhoCnpj) {
    return tipoCnpj;
  } else {
    // Caso quando for lista de cpfs ou cnpjs (EVENTOSATUALIZACAO)
    if (cleanNumber.length % 11 == 0) return 3;
    if (cleanNumber.length % 14 == 0) return 4;
    throw ArgumentError(
      'Número de documento inválido. Deve conter $tamanhoCpf dígitos (CPF) ou $tamanhoCnpj dígitos (CNPJ). '
      'Recebido: $cleanNumber (${cleanNumber.length} dígitos)',
    );
  }
}