ConstantsRule extends Rule
Rule: Validates that a given value is part of the constants defined in a class using {@see ConstantsTrait}.
Usage
This generic rule ensures that the provided value matches one of the allowed constants from any class that uses the ConstantsTrait.
Examples
use oihana\validations\rules\ConstantsRule;
use Somnambulist\Components\Validation\Validator;
use xyz\oihana\schema\constants\JWTAlgorithm;
// Validate against all constants in a class
$rule = new ConstantsRule(JWTAlgorithm::class);
$validator = new Validator
(
['alg' => 'HS256'],
['alg' => [$rule]]
);
$validator->passes(); // true
// Validate against a subset of constants
$rule = new ConstantsRule(JWTAlgorithm::class, ['HS256', 'RS256']);
$validator = new Validator(
['alg' => 'RS512'],
['alg' => [$rule]]
);
$validator->fails(); // true
Custom Error Messages
You can customize the error message:
$rule = new ConstantsRule(Status::class);
$rule->message(':attribute must be a valid status.');
Tags
Table of Contents
Constants
- CASES = 'cases'
- The parameter key used to store the valid constants list.
- CLASS_NAME = 'className'
- The parameter key for the constants class name.
Properties
- $cases : array<string|int, mixed>
- The list of valid constant values used by this rule.
- $fillableParams : array<string|int, string>
- The parameters that must be present for the rule to function.
- $message : string
- The internal message pattern.
Methods
- __construct() : mixed
- Creates a new ConstantsRule instance.
- cases() : static
- Sets or overrides the list of valid constant values.
- check() : bool
- Checks if the given value is one of the allowed constant values.
- className() : static
- Sets the class name that provides the constants.
Constants
CASES
The parameter key used to store the valid constants list.
public
mixed
CASES
= 'cases'
CLASS_NAME
The parameter key for the constants class name.
public
mixed
CLASS_NAME
= 'className'
Properties
$cases
The list of valid constant values used by this rule.
protected
array<string|int, mixed>
$cases
= []
$fillableParams
The parameters that must be present for the rule to function.
protected
array<string|int, string>
$fillableParams
= [self::CLASS_NAME, self::CASES]
$message
The internal message pattern.
protected
string
$message
= ":attribute is not a valid value."
Methods
__construct()
Creates a new ConstantsRule instance.
public
__construct(ConstantsTrait> $className[, array<string|int, mixed>|null $cases = null ]) : mixed
Parameters
- $className : ConstantsTrait>
-
The fully qualified class name that uses ConstantsTrait.
- $cases : array<string|int, mixed>|null = null
-
Optional list of allowed constant values. Defaults to all values from the class's enums() method.
Tags
cases()
Sets or overrides the list of valid constant values.
public
cases([array<string|int, mixed>|null $cases = null ]) : static
If $cases is null or empty, it defaults to all enums from the class.
Parameters
- $cases : array<string|int, mixed>|null = null
-
The constant values to allow in this rule.
Tags
Return values
staticcheck()
Checks if the given value is one of the allowed constant values.
public
check(mixed $value) : bool
Parameters
- $value : mixed
-
The value to validate.
Tags
Return values
bool —True if the value is a valid algorithm, false otherwise.
className()
Sets the class name that provides the constants.
public
className(ConstantsTrait> $className) : static
Parameters
- $className : ConstantsTrait>
-
The fully qualified class name.