Search by

cancio-labs / cpf-validation-function

nilosoares

A function to validate a CPF (individual taxpayer identification number in Brazil)

Package info

github.com/canciolabs/cpf-validation-function

pkg:composer/cancio-labs/cpf-validation-function

Statistics

Installs: 122

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.1 2026-09-11 22:26 UTC

This package is not auto-updated.

Last update: 2026-09-11 22:27:08 UTC


README

This tiny package contains a few functions to validate a CPF (individual taxpayer identification number in Brazil).

Requirements

PHP 8.5 or greater is required.

Installation

composer require cancio-labs/cpf-validation-function

Functions

  1. is_valid_cpf
  2. assert_cpf

How to use it

is_valid_cpf(?string $cpf): bool

Returns true if the CPF is valid, false otherwise.

use function CancioLabs\Functions\Cpf\is_valid_cpf;

// Passing formatted CPFs
is_valid_cpf('170.317.330-90'); // returns true
is_valid_cpf('170.317.330-00'); // returns false

// Passing raw CPFs
is_valid_cpf('17031733090'); // returns true
is_valid_cpf('17031733000'); // returns false

assert_cpf(?string $cpf): void

Validates the CPF and throw an InvalidArgumentException if the CPF is not valid.

use function CancioLabs\Functions\Cpf\assert_cpf;

// These 2 example will execute normally
assert_cpf('170.317.330-90');
assert_cpf('17031733090');

// These 4 examples throw InvalidArgumentException
assert_cpf(null);
assert_cpf('');
assert_cpf('foo');
assert_cpf('17031733000');

Running Tests

  • From the project root, run: vendor/bin/phpunit tests