PHP code example of lacus / cnpj-dv

1. Go to this page and download the library: Download lacus/cnpj-dv 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/ */

    

lacus / cnpj-dv example snippets




use Lacus\BrUtils\Cnpj\CnpjCheckDigits;

$checkDigits = new CnpjCheckDigits('914157320007');

$checkDigits->first;    // '9'
$checkDigits->second;   // '3'
$checkDigits->both;     // '93'
$checkDigits->cnpj;     // '91415732000793'



use Lacus\BrUtils\Cnpj\CnpjCheckDigits;

$checkDigits = new CnpjCheckDigits('MGKGMJ9X0001');

$checkDigits->first;    // '6'
$checkDigits->second;   // '8'
$checkDigits->both;     // '68'
$checkDigits->cnpj;     // 'MGKGMJ9X000168'



use Lacus\BrUtils\Cnpj\CnpjCheckDigits;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputInvalidException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputLengthException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputTypeError;

// Input type (e.g. integer not allowed)
try {
    new CnpjCheckDigits(12345678000100);
} catch (CnpjCheckDigitsInputTypeError $e) {
    echo $e->getMessage();
}

// Length (must be 12–14 alphanumeric characters after sanitization)
try {
    new CnpjCheckDigits('12345678901');
} catch (CnpjCheckDigitsInputLengthException $e) {
    echo $e->getMessage();
}

// Invalid (e.g. all-zero base or branch, or repeated numeric digits)
try {
    new CnpjCheckDigits('000000000001');
} catch (CnpjCheckDigitsInputInvalidException $e) {
    echo $e->getMessage();
}

// Any data exception from the package
try {
    // risky code
} catch (CnpjCheckDigitsException $e) {
    // handle
}