Get support for yiisoft/validator-rule-handler-container

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

yiisoft/validator-rule-handler-container

⚠️ This package is deprecated. Its contents were moved to the validator package itself.

Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

The package is a container for resolving Yii Validator rule handlers. It provides rule handler container based on Yii Factory and uses Yii Definitions syntax.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/validator-rule-handler-container --prefer-dist

General usage

Direct interaction with rule handler container

use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\RuleHandlerInterface;
use Yiisoft\Validator\ValidationContext;
use Yiisoft\Validator\Rule\Handler\Container\RuleHandlerContainer;

/**
 * Validates that the value is a Pi.
 */
final class PiHandler implements RuleHandlerInterface
{
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
    {
        $result = new Result();

        if (!(\abs($value - M_PI) < PHP_FLOAT_EPSILON)) {
            $result->addError('Value must be Pi.', ['value' => $value]);
        }

        return $result;
    }
}

$ruleHandlerContainer = new RuleHandlerContainer(new MyContainer());
$ruleHandler = $ruleHandlerContainer->resolve(PiHandler::class);
  • MyContainer is a container for resolving dependencies and must be an instance of Psr\Container\ContainerInterface. Yii Dependency Injection implementation also can be used.
  • You can optionally set definitions and disable their validation if needed.

Basically, the arguments are the same as in Yii Factory. Please refer to its docs for more details.

Rule handlers are created only once, then cached and reused for repeated calls.

$ruleHandler = $ruleHandlerContainer->create(PiHandler::class); // Returned from cache

Using Yii config

use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Validator\RuleHandlerResolverInterface;
use Yiisoft\Validator\Rule\Handler\Container\RuleHandlerContainer;

// Need to be defined in params.php
$params = [
    'yiisoft/validator-rule-handler-container' => [
        'handlers' => [PiHandler::class => PiHandler::class],
        'validate' => true,
    ],
];

// Need to be defined in common.php
$config = [
    RuleHandlerResolverInterface::class => [
        'class' => RuleHandlerContainer::class,
        '__construct()' => [
            'definitions' => $params['yiisoft/validator-rule-handler-container']['handlers'],
            'validate' => $params['yiisoft/validator-rule-handler-container']['validate'],
        ],
    ]
];

$containerConfig = ContainerConfig::create()->withDefinitions($config); 
$container = new Container($containerConfig);
$ruleHandlerResolver = $container->get(RuleHandlerResolverInterface::class);        
$ruleHandler = $ruleHandlerResolver->resolve(PiHandler::class);

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 Rule Handler Container is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2