/** * Retorna CPF ou CNPJ formatado * * @param $str * * @return string */ public static function formataCPFCNPJ($str) { $str = preg_replace('![^\\d]!', '', $str); if (11 == strlen($str) || 14 > strlen($str)) { $str = CPF::formatar($str); } elseif (14 <= strlen($str)) { $str = CNPJ::formatar($str); } return $str; }
/** * Verifica se o CPF está válido. */ public function testValidacao() { $this->assertTrue(CPF::validar("656.765.370-19")); $this->assertTrue(CPF::validar("656.765.370-190")); $this->assertFalse(CPF::validar("679.057.601-04")); $this->assertFalse(CPF::validar("828.583.3A7-06")); for ($i = 0; $i < 10; $i++) { $this->assertFalse(CPF::validar("{$i}{$i}{$i}.{$i}{$i}{$i}.{$i}{$i}{$i}-{$i}{$i}")); } for ($i = 0; $i < 100; $i++) { $this->assertTrue(CPF::validar(CPF::gerar())); } }