cancio-labs/cpf-validation-function

验证巴西身份证(CPF)的功能

1.0.0 2024-06-30 17:10 UTC

This package is not auto-updated.

Last update: 2024-09-23 16:43:13 UTC


README

此小巧的包包含一些验证巴西身份证(CPF)的函数

需求

需要PHP 7.4或更高版本,没有其他要求。

安装

composer require cancio-labs/cpf-validation-function

函数

  1. is_valid_cpf
  2. assert_cpf

如何使用

is_valid_cpf(string $cpf): bool

如果CPF有效则返回true,否则返回false。

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

验证CPF,如果CPF无效则抛出InvalidArgumentException异常。

use function CancioLabs\Functions\Cpf\assert_cpf;

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

// These 3 examples throw InvalidArgumentException
assert_cpf('')
assert_cpf('foo')
assert_cpf('17031733000')

运行测试

  • 从项目根目录运行:vendor/bin/phpunit .