PHP code example of singra / br-validation

1. Go to this page and download the library: Download singra/br-validation 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/ */

    

singra / br-validation example snippets


use Singra\BrValidation\Documents\Cpf;

Cpf::isValid('111.444.777-35');   // true

$cpf = Cpf::from('111.444.777-35');

$cpf->value();                    // '11144477735'
$cpf->formatted();                // '111.444.777-35'
$cpf->redacted();                 // '***.444.777-**'
$cpf->fiscalRegion()->states();   // [Uf::ES, Uf::RJ]

// Construction
Cpf::from(string $value): Cpf                  // throws InvalidDocument
Cpf::tryFrom(string $value): ?Cpf              // null instead of throwing
Cpf::isValid(string $value): bool

// Display, without verifying the check digits
Cpf::mask(string $value): string
Cpf::redact(string $value, Redaction $strategy = Redaction::Default): string

// Instance
$cpf->value(): string                          // '11144477735'
$cpf->formatted(): string                      // '111.444.777-35'
$cpf->redacted(Redaction $strategy = Redaction::Default): string
(string) $cpf                                  // '11144477735'
json_encode($cpf)                              // '"11144477735"'

$cnpj = Cnpj::from('11.222.333/0001-81');
$cnpj->root();             // '11222333'
$cnpj->branch();           // '0001'
$cnpj->isHeadquarters();   // true
$cnpj->isAlphanumeric();   // false

$cnpj = Cnpj::from('plbcfsrl000140');   // lowercase is normalised
$cnpj->value();            // 'PLBCFSRL000140'
$cnpj->formatted();        // 'PL.BCF.SRL/0001-40'
$cnpj->isAlphanumeric();   // true

$cep = Cep::from('01310-100');
$cep->uf();            // Uf::SP
$cep->uf()->label();   // 'São Paulo'

Cep::isValid('00123-456');   // false — no block was ever allocated there
Cep::isValid('11111-111');   // false — uniform, though inside São Paulo's block
Cep::isValid('78950-000');   // false — vacated when Rondônia was renumbered

$region = Cpf::from('111.444.777-35')->fiscalRegion();

$region;             // FiscalRegion::Seventh
$region->number();   // 7
$region->states();   // [Uf::ES, Uf::RJ]

Cpf::isValid('11144477735');           // true
Cpf::isValid('111.444.777-35');        // true
Cpf::isValid('111 444 777 35');        // true
Cpf::isValid('  111.444.777-35  ');    // true

Cpf::isValid('CPF: 111.444.777-35');   // false — letters
Cpf::isValid('1a2b3c4d5e6');           // false — letters

Cpf::tryFrom('1234567890');   // null — not silently read as '01234567890'

use Singra\BrValidation\Enums\Reason;
use Singra\BrValidation\Exceptions\InvalidDocument;

try {
    $cpf = Cpf::from($input);
} catch (InvalidDocument $e) {
    $e->document;   // 'cpf'
    $e->reason;     // Reason::WrongLength
    $e->getMessage();

    return match ($e->reason) {
        Reason::WrongLength        => 'CPF deve ter 11 dígitos',
        Reason::IllegalCharacter   => 'CPF deve conter apenas números',
        Reason::RepeatedCharacters => 'CPF inválido',
        Reason::InvalidCheckDigit  => 'CPF inválido',
        Reason::UnallocatedRange   => 'CEP não existe',
    };
}

use Singra\BrValidation\Enums\Redaction;

$cpf->redacted(Redaction::Head);   // '111.***.***-**'

use Random\Engine\Xoshiro256StarStar;
use Random\Randomizer;
use Singra\BrValidation\Enums\{FiscalRegion, Uf};

Cpf::generate(?Randomizer $randomizer = null, ?FiscalRegion $region = null): Cpf
Cnpj::generate(?Randomizer $randomizer = null, bool $alphanumeric = false): Cnpj
Cep::generate(?Randomizer $randomizer = null, ?Uf $uf = null): Cep

Cpf::generate();                               // CSPRNG

$seeded = new Randomizer(new Xoshiro256StarStar(hash('sha256', 'seed', true)));
Cpf::generate($seeded)->value();               // '01326741225', every run

Cpf::generate(region: FiscalRegion::Eighth);   // issued in São Paulo
Cep::generate(uf: Uf::PE);                     // inside a Pernambuco block
Cnpj::generate(alphanumeric: true);            // 'K9.VWW.FYR/0001-34'

Cpf::isValid('11144477700');   // false
Cpf::mask('11144477700');      // '111.444.777-00'
Cpf::redact('11144477700');    // '***.444.777-**'

Cpf::mask('1234');             // throws — shape is still enforced