0 follower

Final Class Yiisoft\Validator\Rule\Callback

InheritanceYiisoft\Validator\Rule\Callback
ImplementsYiisoft\Validator\AfterInitAttributeEventInterface, Yiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Defines validation options to validating the value using a callback.

See also Yiisoft\Validator\Rule\CallbackHandler.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( callable|null $callback null, string|null $method null, boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$callback callable|null

Callable with the function ($value, $rule, $context): Result signature that performs the validation. Mutually exclusive with {@see $method}.

$method string|null

Name of a validated object method with the function ($value, $rule, $context): Result signature that performs the validation. Mutually exclusive with {@see $callback}.

$skipOnEmpty boolean|callable|null

Whether to skip this rule if the value validated is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}.

$when Closure|null

A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}.

throws InvalidArgumentException

When neither {@see $callback} nor {@see $method} is specified or both are specified at the same time.

                public function __construct(
    private mixed $callback = null,
    private ?string $method = null,
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    if ($this->callback === null && $this->method === null) {
        throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
    }
    if ($this->callback !== null && $this->method !== null) {
        throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
    }
    $this->skipOnEmpty = $skipOnEmpty;
}

            
afterInitAttribute() public method

public void afterInitAttribute ( object $object )
$object object

                public function afterInitAttribute(object $object): void
{
    if ($this->method === null) {
        return;
    }
    $method = $this->method;
    $reflection = new ReflectionObject($object);
    if (!$reflection->hasMethod($method)) {
        throw new InvalidArgumentException(
            sprintf(
                'Method "%s" does not exist in class "%s".',
                $method,
                $object::class,
            ),
        );
    }
    /** @psalm-suppress MixedMethodCall */
    $this->callback = Closure::bind(fn(mixed ...$args): mixed => $object->{$method}(...$args), $object, $object);
}

            
getCallback() public method

Get the callable that performs validation.

public callable|null getCallback ( )
return callable|null

The callable that performs validation.

                public function getCallback(): ?callable
{
    return $this->callback;
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return CallbackHandler::class;
}

            
getMethod() public method

Get a name of a validated object method that performs the validation.

public string|null getMethod ( )
return string|null

Name of a method that performs the validation.

                public function getMethod(): ?string
{
    return $this->method;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return self::class;
}

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'method' => $this->method,
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ];
}

            
getSkipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()

A getter for $skipOnEmpty property.

public boolean|callable|null getSkipOnEmpty ( )
return boolean|callable|null

A current raw (non-normalized) value.

                public function getSkipOnEmpty(): bool|callable|null
{
    return $this->skipOnEmpty;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()

An immutable setter to change $skipOnEmpty property.

public $this skipOnEmpty ( boolean|callable|null $value )
$value boolean|callable|null

A new value.

return $this

The new instance with a changed value.

                public function skipOnEmpty(bool|callable|null $value): static
{
    $new = clone $this;
    $new->skipOnEmpty = $value;
    return $new;
}

            
skipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()

An immutable setter to change $skipOnError property.

public $this skipOnError ( boolean $value )
$value boolean

A new value. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()

An immutable setter to change $when property.

public $this when ( Closure|null $value )
$value Closure|null

A new value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}