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: 123

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.2 2026-09-22 02:10 UTC

This package is not auto-updated.

Last update: 2026-11-03 02:43:27 UTC


README

PHP 8.5+ License: GPL v3

Small, dependency-free PHP functions for validating Brazilian CPF (Cadastro de Pessoas Fisicas) numbers. Use a boolean check when invalid input is expected, or an assertion when invalid input should stop execution.

Requirements

  • PHP 8.5 or later

Installation

Install the package with Composer:

composer require cancio-labs/cpf-validation-function

Composer autoloads both functions automatically.

Usage

The validator accepts CPF values in either of these exact formats:

  • Raw: 99999999999
  • Formatted: 999.999.999-99

It rejects malformed input, repeated-digit CPFs such as 000.000.000-00, and values whose check digits do not match.

is_valid_cpf

Use is_valid_cpf(?string $cpf): bool when you need to test a value without handling an exception.

<?php

use function CancioLabs\Functions\Cpf\is_valid_cpf;

is_valid_cpf('94537020059');     // true
is_valid_cpf('945.370.200-59');  // true

is_valid_cpf('94537020000');     // false: invalid check digits
is_valid_cpf('000.000.000-00');  // false: repeated digits
is_valid_cpf(null);              // false

assert_cpf

Use assert_cpf(?string $cpf): void to enforce a valid CPF. It throws InvalidArgumentException when the value is invalid.

<?php

use function CancioLabs\Functions\Cpf\assert_cpf;

assert_cpf('033.039.290-50'); // Continues normally.

try {
    assert_cpf('03303929000');
} catch (InvalidArgumentException $exception) {
    // Handle an invalid CPF.
}

Validation behavior

Input Result
94537020059 Valid
945.370.200-59 Valid
945 370 200 59 Invalid: unsupported format
945.370.200-00 Invalid: incorrect check digits
11111111111 Invalid: repeated digits
null or '' Invalid

Development

Install development dependencies and run the test suite:

composer install
vendor/bin/phpunit tests

Contributing

Contributions are welcome. Please include tests for behavior changes and keep the public API backward compatible where possible.

License

This project is licensed under the GNU General Public License v3.0 or later.