PHP code example of t2softwaregroup / cnpjvalidator

1. Go to this page and download the library: Download t2softwaregroup/cnpjvalidator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

t2softwaregroup / cnpjvalidator example snippets


use T2SoftwareGroup\Cnpjvalidator\CnpjValidator;

// Só limpar máscara (não valida)
$limpo = CnpjValidator::removePontuacaoCnpjAlfaNumerico('12.ABC.345/6789-90');
// ex.: '12ABC345678990' (exemplo ilustrativo; o DV precisa estar correto para isValid)

// Validar sem exceção
if (CnpjValidator::isValid('11.444.777/0001-61')) {
    // ...
}

// Validar e obter string normalizada ou falhar
try {
    $cnpjLimpo = CnpjValidator::create('11.444.777/0001-61'); // '11444777000161'
} catch (\InvalidArgumentException $e) {
    // CNPJ inválido ou tipo incorreto
}

// Máscara para exibição
$mascarado = CnpjValidator::format('11444777000161'); // '11.444.777/0001-61'

// CPF ou CNPJ bruto (ex.: coluna SQL) → exibição
$doc = CnpjValidator::formatCpfOuCnpjParaExibicao('12345678901');   // '123.456.789-01'
$cnpjTela = CnpjValidator::formatCpfOuCnpjParaExibicao('11444777000161'); // '11.444.777/0001-61'

// Várias colunas de uma linha
$row = CnpjValidator::formatResultadoSqlDocumentos([
    'nome' => 'Empresa',
    'cpf' => '12345678901',
    'cnpj' => '11444777000161',
]);

// Busca (14 alfanuméricos vs 11 dígitos)
$chave = CnpjValidator::normalizeDocumentoBusca('11.444.777/0001-61'); // '11444777000161'

// Auditoria / log
$audit = CnpjValidator::maskForAudit('11.444.777/0001-61'); // '11.***.***/****-61'
bash
php vendor/bin/pest