PHP code example of lacus / cpf-utils

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



// Using class-based resource
use Lacus\CpfUtils\CpfUtils;

// Or using function-based approach
use function Lacus\CpfUtils\cpf_fmt;
use function Lacus\CpfUtils\cpf_gen;
use function Lacus\CpfUtils\cpf_val;

$cpfUtils = new CpfUtils();
$cpf = '11144477735';

// Format CPF
echo $cpfUtils->format($cpf);       // returns '111.444.777-35'

// Validate CPF
echo $cpfUtils->isValid($cpf);      // returns true

// Generate CPF
echo $cpfUtils->generate();         // returns '12345678901'

$cpfUtils = new CpfUtils(
    formatter: [
        'hidden' => true,
        'hiddenKey' => '#',
        'hiddenStart' => 3,
        'hiddenEnd' => 9
    ],
    generator: [
        'format' => true
    ]
);

$cpf = '11144477735';
echo $cpfUtils->format($cpf);       // returns '111.###.###-##'
echo $cpfUtils->generate();         // returns '123.456.789-01'

$cpf = '11144477735';

// Format CPF
echo cpf_fmt($cpf);                 // returns '111.444.777-35'

// Validate CPF
echo cpf_val($cpf);                 // returns true

// Generate CPF
echo cpf_gen();                     // returns '12345678901'

cpf_fmt(
    string $cpfString,
    ?bool $escape = null,
    ?bool $hidden = null,
    ?string $hiddenKey = null,
    ?int $hiddenStart = null,
    ?int $hiddenEnd = null,
    ?string $dotKey = null,
    ?string $dashKey = null,
    ?Closure $onFail = null,
): string

$cpf = '11144477735';

// Basic formatting
echo cpf_fmt($cpf);                 // '111.444.777-35'

// With hidden digits
echo cpf_fmt($cpf, hidden: true);   // '111.***.***-**'

// Custom delimiters
echo cpf_fmt($cpf, dotKey: '', dashKey: '_');  // '111444777_35'

// Custom hidden range
echo cpf_fmt($cpf, hidden: true, hiddenStart: 0, hiddenEnd: 6, hiddenKey: '#');  // '###.###.777-35'

cpf_gen(
    ?bool $format = null,
    ?string $prefix = null,
): string

// Generate random CPF
echo cpf_gen();                     // '12345678901'

// Generate formatted CPF
echo cpf_gen(format: true);         // '123.456.789-01'

// Complete a prefix
echo cpf_gen(prefix: '123456789');  // '12345678901'

// Complete and format
echo cpf_gen(prefix: '123456789', format: true);  // '123.456.789-01'

cpf_val(string $cpfString): bool

// Valid CPF
echo cpf_val('11144477735');        // true
echo cpf_val('111.444.777-35');     // true

// Invalid CPF
echo cpf_val('11144477736');        // false

$cpfUtils = new CpfUtils();

// Get individual components
$formatter = $cpfUtils->getFormatter();
$generator = $cpfUtils->getGenerator();
$validator = $cpfUtils->getValidator();

// Use them directly
$formatter->format('11144477735', hidden: true);
$generator->generate(format: true);
$validator->isValid('11144477735');

$cpf = '123'; // Invalid length

// Custom fallback
echo cpf_fmt($cpf, onFail: fn($v) => "Invalid CPF: {$v}");  // 'Invalid CPF: 123'

// Return original value
echo cpf_fmt($cpf);  // '123'