0 follower

Final Class Yiisoft\Validator\Rule\CallbackHandler

InheritanceYiisoft\Validator\Rule\CallbackHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Validates a value using a callback.

See also Yiisoft\Validator\Rule\Callback.

Public Methods

Hide inherited methods

Method Description Defined By
validate() Yiisoft\Validator\Rule\CallbackHandler

Method Details

Hide inherited methods

validate() public method

public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context )
$value mixed
$rule Yiisoft\Validator\RuleInterface
$context Yiisoft\Validator\ValidationContext
throws Yiisoft\Validator\Exception\InvalidCallbackReturnTypeException

                public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
    if (!$rule instanceof Callback) {
        throw new UnexpectedRuleException(Callback::class, $rule);
    }
    $callback = $rule->getCallback();
    if ($callback === null) {
        throw new InvalidArgumentException('Using method outside of attribute scope is prohibited.');
    }
    $result = $callback($value, $rule, $context);
    if (!$result instanceof Result) {
        throw new InvalidCallbackReturnTypeException($result);
    }
    return $result;
}