customergauge/password

4.0.0 2020-12-22 10:25 UTC

This package is auto-updated.

Last update: 2024-09-07 15:58:43 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

密码强度库 🔒

此库用于验证密码强度。它由一系列规则组成,这些规则可以单独使用或通过规则链聚合。

安装

composer require customergauge/password

用法

单个规则

use Customergauge\Password\Rule\Lowercase;
use Customergauge\Password\Exception\InvalidPassword;

$validate = new Lowercase;
$password = "UPPERCASE";

try {
    $validate($password);
} catch (InvalidPassword $e) {
    echo $e->getMessage();
}

// output: Password should have at least 1 lowercase character(s) but 0 found.

规则链

use Customergauge\Password\Rule\Lowercase;
use Customergauge\Password\Rule\Uppercase;
use Customergauge\Password\Rule\Length;
use Customergauge\Password\RuleChain;
use Customergauge\Password\Exception\InvalidPassword;

$validate = new RuleChain(
  new Lowercase(2),
  new Uppercase(2),
  new Length(10),
  new Digit(3)
);

$password = "ABcd00efgh";

try {
    $validate($password);
} catch (InvalidPassword $e) {
    echo $e->getMessage();
}

// output: Password should have at least 3 digit character(s) but 2 found.

持久化规则链

当您想在规则抛出 InvalidPassword 异常的情况下继续执行时,请使用 PersistRuleChain 类。

use Customergauge\Password\Rule\Lowercase;
use Customergauge\Password\Rule\Uppercase;
use Customergauge\Password\Rule\Length;
use Customergauge\Password\RuleChain;
use Customergauge\Password\Exception\InvalidPassword;

$validate = new PersistRuleChain(
  new Lowercase(2),
  new Uppercase(2),
  new Length(10),
  new Digit(3)
);

$password = "ABcd00efgh";

if ($validate($password)) {
    echo "valid";
} else {
    echo "invalid";
    // It is possible to get all exceptions using $validate->exceptions();
}

// output: invalid

贡献

我们始终欢迎贡献,请查看我们的问题列表,看看您是否可以提供帮助。

许可证

密码强度库采用 LGPLv3 许可证。