Utility function/class to generate valid CPF (Brazilian personal ID).
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
# using Composer
$ composer require lacus/cpf-gen<?php
// Using class-based resource
use Lacus\CpfGen\CpfGenerator;
// Or using function-based one
use function Lacus\CpfGen\cpf_gen;$generator = new CpfGenerator();
$cpf = $generator->generate(); // returns '47844241055'
// With options
$cpf = $generator->generate(
format: true
); // returns '478.442.410-55'
$cpf = $generator->generate(
prefix: '528250911'
); // returns '52825091138'
$cpf = $generator->generate(
prefix: '528250911',
format: true
); // returns '528.250.911-38'The options can be provided to the constructor or the generate() method. If passed to the constructor, the options will be attached to the CpfGenerator instance. When passed to the generate() method, it only applies the options to that specific call.
$generator = new CpfGenerator(format: true);
$cpf1 = $generator->generate(); // '478.442.410-55' (uses instance options)
$cpf2 = $generator->generate(format: false); // '47844241055' (overrides instance options)
$cpf3 = $generator->generate(); // '123.456.789-01' (uses instance options again)The helper function cpf_gen() is just a functional abstraction. Internally it creates an instance of CpfGenerator and calls the generate() method right away.
$cpf = cpf_gen(); // returns '47844241055'
$cpf = cpf_gen(format: true); // returns '478.442.410-55'
$cpf = cpf_gen(prefix: '528250911'); // returns '52825091138'
$cpf = cpf_gen(prefix: '528250911', format: true); // returns '528.250.911-38'| Parameter | Type | Default | Description |
|---|---|---|---|
format |
?bool |
false |
Whether to format the output with dots and dash |
prefix |
?string |
'' |
If you have CPF initials and want to complete it with valid digits. The string provided must contain between 0 and 9 digits! |
We welcome contributions! Please see our Contributing Guidelines for details. But if you find this project helpful, please consider:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG for a list of changes and version history.
Made with ❤️ by Lacus Solutions
