Final Class Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer
| Inheritance | Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerResolverInterface |
An implementation for {@see RuleHandlerResolverInterface} acting as a wrapper over dependency injection container ({@see ContainerInterface}) throwing more specific exceptions and executing additional checks during resolving a rule handler name to make sure that if a handler was found, then it's indeed a valid handler to work with.
To use it, make sure to change config.php like so:
use Yiisoft\Validator\RuleHandlerResolverInterface;
use Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer;
[
RuleHandlerResolverInterface::class => RuleHandlerContainer::class,
];
If you don't need DI container, {@see \Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer} can be used instead. It's enabled by default, you don't need to additionally configure anything.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer | |
| resolve() | Resolves a rule handler name to a corresponding rule handler instance. The actual resolving is delegated to {@see $container}. Throws more specific exceptions and executes additional checks to make sure that if a handler was found, then it's indeed a valid handler to work with. | Yiisoft\Validator\RuleHandlerResolver\RuleHandlerContainer |
Method Details
| public mixed __construct ( \Psr\Container\ContainerInterface $container ) | ||
| $container | \Psr\Container\ContainerInterface | |
public function __construct(
/**
* @var ContainerInterface An instance of dependency injection container.
*/
private readonly ContainerInterface $container,
) {}
Resolves a rule handler name to a corresponding rule handler instance. The actual resolving is delegated to {@see $container}. Throws more specific exceptions and executes additional checks to make sure that if a handler was found, then it's indeed a valid handler to work with.
| public Yiisoft\Validator\RuleHandlerInterface resolve ( string $name ) | ||
| $name | string |
A rule handler name ({@see \Yiisoft\Validator\RuleHandlerResolver\RuleInterface}). |
| return | Yiisoft\Validator\RuleHandlerInterface |
A corresponding rule handler instance. |
|---|---|---|
| throws | Yiisoft\Validator\Exception\RuleHandlerNotFoundException |
If a rule handler instance was not found. |
| throws | Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException |
If a found instance is not a valid rule handler. |
public function resolve(string $name): RuleHandlerInterface
{
try {
$ruleHandler = $this->container->get($name);
} catch (NotFoundExceptionInterface $e) {
throw new RuleHandlerNotFoundException($name, previous: $e);
}
if (!$ruleHandler instanceof RuleHandlerInterface) {
throw new RuleHandlerInterfaceNotImplementedException($ruleHandler);
}
return $ruleHandler;
}
Signup or Login in order to comment.