0 follower

Final Class Yiisoft\Validator\Exception\UnexpectedRuleException

InheritanceYiisoft\Validator\Exception\UnexpectedRuleException » InvalidArgumentException

An exception used by rule handlers to guarantee that passed rule have desired type. Every handler's validation code must start with this check. An example for MyRule and MyRuleHandler:

use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

final class MyRuleHandler implements RuleHandlerInterface
{
    public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
    {
        if (!$rule instanceof MyRule) {
            throw new UnexpectedRuleException(MyRule::class, $rule);
        }

        // ...
        $result = new Result();
        // ...

        return $result;
    }
}

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|string[] $expectedClassName, object $actualObject, integer $code 0, Throwable|null $previous null )
$expectedClassName string|string[]

Expected class name(s) of a rule.

$actualObject object

An actual given object that's not an instance of $expectedClassName.

$code integer

The Exception code.

$previous Throwable|null

The previous throwable used for the exception chaining.

                public function __construct(
    string|array $expectedClassName,
    object $actualObject,
    int $code = 0,
    ?Throwable $previous = null,
) {
    parent::__construct(
        sprintf(
            'Expected "%s", but "%s" given.',
            implode('", "', (array) $expectedClassName),
            $actualObject::class,
        ),
        $code,
        $previous,
    );
}