vjik/yii-validator-symfony-rule

Symfony验证约束适配器,用于Yii Validator

1.0.0 2023-02-22 13:32 UTC

This package is auto-updated.

Last update: 2024-09-22 17:31:57 UTC


README

     

Yii Validator Symfony Rule


Latest Stable Version Total Downloads Build status Mutation testing badge type-coverage static analysis psalm-level

该包提供了验证规则SymfonyRule,允许在Yii Validator中使用Symfony验证约束。

要求

  • PHP 8.0 或更高版本。

安装

可以使用 composer 安装此包

composer require vjik/yii-validator-symfony-rule

通用用法

将symfony约束包装到Yii规则 SymfonyRule 中。例如

use Symfony\Component\Validator\Constraints\CssColor;
use Symfony\Component\Validator\Constraints\NotEqualTo;
use Symfony\Component\Validator\Constraints\Positive;
use Vjik\Yii\ValidatorSymfonyRule\SymfonyRule;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Required;

final class Car
{
    #[Required]
    #[HasLength(min: 3, skipOnEmpty: true)]
    public string $name = '';

    #[Required]
    #[SymfonyRule(
        new CssColor(CssColor::RGB), // Symfony constraint
        skipOnEmpty: true,
    )]
    public string $cssColor = '#1123';

    #[SymfonyRule([
        new Positive(), // Symfony constraint
        new NotEqualTo(13), // Symfony constraint
    ])]
    public int $number = 13;
}

SymfonyRule 规则参数

$constraint — 单个或数组形式的Symfony验证约束。必需。

$skipOnEmpty — 是否在空值上跳过规则,以及将哪个值视为空。默认为 null

$skipOnError — 一个布尔值,其中 true 表示在之前的规则出错时跳过此规则,而 false — 不跳过。默认为 false

$when — 允许仅在特定条件下应用规则的闭包。默认为 null

SymfonyRuleHandler 参数

$symfonyValidator — Symfony验证器实例。默认为通过 Symfony\Component\Validator\Validation::createValidator() 创建的验证器。

测试

单元测试

该包使用 PHPUnit 进行测试。要运行测试

./vendor/bin/phpunit

突变测试

该包的测试通过 Infection 突变框架和 Infection Static Analysis Plugin 进行检查。要运行它

./vendor/bin/roave-infection-static-analysis-plugin

静态分析

代码使用 Psalm 进行静态分析。要运行静态分析

./vendor/bin/psalm

许可证

Yii Validator Symfony Rule 是免费软件。它根据BSD许可证发布。有关更多信息,请参阅 LICENSE