PHP code example of lacus / cpf-dv

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




use Lacus\BrUtils\Cpf\CpfCheckDigits;

$checkDigits = new CpfCheckDigits('054496519');

$checkDigits->first;    // '1'
$checkDigits->second;   // '0'
$checkDigits->both;     // '10'
$checkDigits->cpf;      // '05449651910'



use Lacus\BrUtils\Cpf\CpfCheckDigits;
use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsException;
use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputInvalidException;
use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputLengthException;
use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputTypeError;

// Input type (e.g. integer not allowed)
try {
    new CpfCheckDigits(12345678901);
} catch (CpfCheckDigitsInputTypeError $e) {
    echo $e->getMessage();
}

// Length (must be 9–11 digits after sanitization)
try {
    new CpfCheckDigits('12345678');
} catch (CpfCheckDigitsInputLengthException $e) {
    echo $e->getMessage();
}

// Invalid (e.g. repeated digits)
try {
    new CpfCheckDigits(['999', '999', '999']);
} catch (CpfCheckDigitsInputInvalidException $e) {
    echo $e->getMessage();
}

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