0 follower

Final Class Yiisoft\Validator\Rule\InEnum

InheritanceYiisoft\Validator\Rule\InEnum
ImplementsYiisoft\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 check that the value is one of the values (or names) contained in an enum of the specified class.

If the {@see \Yiisoft\Validator\Rule\In::$not} is set, the validation logic is inverted and the rule will ensure that the value is NOT one of them.

See also Yiisoft\Validator\Rule\InEnumHandler.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\InEnum
getHandler() Yiisoft\Validator\Rule\InEnum
getMessage() Get error message when the value is not in a set of {@see $values}. Yiisoft\Validator\Rule\InEnum
getName() Yiisoft\Validator\Rule\InEnum
getOptions() Yiisoft\Validator\Rule\InEnum
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getValues() Get a set of values to check against. Yiisoft\Validator\Rule\InEnum
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
isNot() Whether to invert the validation logic. Defaults to false. If set to true, the value must NOT be among the list of {@see $values}. Yiisoft\Validator\Rule\InEnum
isStrict() Whether the comparison is strict (both type and value must be the same). Yiisoft\Validator\Rule\InEnum
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $class, boolean $useNames false, boolean $strict false, boolean $not false, string $message '{Property} is not in the list of acceptable values.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$class string

Class of the enum to user.

$useNames boolean

Whether to use names for backed enums instead of value.

$strict boolean

Whether the comparison to each value in the set is strict:

Defaults to false meaning non-strict mode is used.

$not boolean

Whether to invert the validation logic. Defaults to false. If set to true, the value must NOT be among the list of {@see $values}.

$message string

Error message when the value is not in a set of value.

You may use the following placeholders in the message:

  • {property}: the name of the attribute.
  • {Property}: the capitalized name of the attribute.
$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}.

                public function __construct(
    private string $class,
    private bool $useNames = false,
    private bool $strict = false,
    private bool $not = false,
    private string $message = '{Property} is not in the list of acceptable values.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->skipOnEmpty = $skipOnEmpty;
    if (!is_subclass_of($this->class, UnitEnum::class)) {
        throw new InvalidArgumentException(
            sprintf('Class should be an enum class string, %s provided.', get_debug_type($this->class)),
        );
    }
}

            
getHandler() public method

public string getHandler ( )

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

            
getMessage() public method

Get error message when the value is not in a set of {@see $values}.

public string getMessage ( )
return string

Error message.

                public function getMessage(): string
{
    return $this->message;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return 'inEnum';
}

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'values' => $this->getValues(),
        'strict' => $this->strict,
        'not' => $this->not,
        'message' => [
            'template' => $this->message,
            'parameters' => [],
        ],
        '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;
}

            
getValues() public method

Get a set of values to check against.

public array getValues ( )
return array

A set of values.

                public function getValues(): array
{
    if (is_subclass_of($this->class, BackedEnum::class) && !$this->useNames) {
        return array_column($this->class::cases(), 'value');
    }
    /**
     * @psalm-suppress InvalidStringClass
     * @psalm-var array<array-key, mixed> $cases
     */
    $cases = $this->class::cases();
    return array_column($cases, 'name');
}

            
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;
}

            
isNot() public method

Whether to invert the validation logic. Defaults to false. If set to true, the value must NOT be among the list of {@see $values}.

public boolean isNot ( )
return boolean

Whether to invert the validation logic.

                public function isNot(): bool
{
    return $this->not;
}

            
isStrict() public method

Whether the comparison is strict (both type and value must be the same).

public boolean isStrict ( )
return boolean

Whether the comparison is strict.

                public function isStrict(): bool
{
    return $this->strict;
}

            
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;
}