vjik/yii-validator-symfony-rule

Symfony validation constraints adapter for Yii Validator

1.0.0 2023-02-22 13:32 UTC

This package is auto-updated.

Last update: 2024-10-22 17:39:50 UTC


README

     

Yii Validator Symfony Rule


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

The package provides validator rule SymfonyRule that allow use Symfony validation constraints in Yii Validator.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:

composer require vjik/yii-validator-symfony-rule

General usage

Wrap a symfony constraints to Yii rule SymfonyRule enough. For example:

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 rule parameters

$constraint — Single or array of Symfony validation constraints. Required.

$skipOnEmpty — Whether skip rule on empty value or not, and which value consider as empty. Defaults to null.

$skipOnError — A boolean value where true means to skip rule when the previous one errored and false — do not skip. Defaults to false.

$when — The closure that allow to apply rule under certain conditions only. Defaults to null.

SymfonyRuleHandler parameters

$symfonyValidator — Symfony validator instance. Defaults to validator created by Symfony\Component\Validator\Validation::createValidator().

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

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

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Validator Symfony Rule is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.