Final Class Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer
| Inheritance | Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerResolverInterface |
An implementation for {@see RuleHandlerResolverInterface} using internal class instance variable as a storage of rule handlers' instances. Use it if you don't need PSR container ({@see ContainerInterface}), otherwise {@see RuleHandlerContainer} can be used instead. It's enabled by default to you don't need to additionally configure anything.
Note that you can predefine handlers by yourself:
$container = new SimpleRuleHandlerContainer(['my-handler' => $myHandlerInstance]);
$validator = new Validator(ruleHandlerResolver: $container);
This way my-handler can be used as an alias and specified in {@see \Yiisoft\Validator\RuleHandlerResolver\RuleInterface::getHandler()}.
It's also possible to replace a handler for built-in rule with a custom one:
$container = new SimpleRuleHandlerContainer([FilledAtLeast::class => new MyFilledAtLeastHandler()]);
$validator = new Validator(ruleHandlerResolver: $container);
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer | |
| resolve() | Resolves a rule handler name to a corresponding rule handler instance. | Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer |
Method Details
| public mixed __construct ( array $instances = [] ) | ||
| $instances | array | |
| throws | Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException |
If one of the {@see $instances} is not a valid rule handler. |
|---|---|---|
public function __construct(
/**
* @var RuleHandlerInterface[] A storage of rule handlers' instances - a mapping where keys are strings (the
* rule handlers' class names by default) and values are corresponding rule handlers' instances.
* @psalm-var array<string, RuleHandlerInterface>
*/
private array $instances = [],
) {
foreach ($instances as $instance) {
if (!$instance instanceof RuleHandlerInterface) {
throw new RuleHandlerInterfaceNotImplementedException($instance);
}
}
}
Resolves a rule handler name to a corresponding rule handler instance.
| 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
{
if (array_key_exists($name, $this->instances)) {
return $this->instances[$name];
}
if (!class_exists($name)) {
throw new RuleHandlerNotFoundException($name);
}
if (!is_subclass_of($name, RuleHandlerInterface::class)) {
throw new RuleHandlerInterfaceNotImplementedException($name);
}
$instance = new $name();
$this->instances[$name] = $instance;
return $instance;
}
Signup or Login in order to comment.