Oihana PHP System

EffectRule extends ConstantsRule

Ensures a value is a valid Effect for Casbin/RBAC permissions.

This rule validates that a given value is one of the supported Effects:

  • allow
  • deny

Usage Example

use oihana\validations\rules\JWTAlgorithmRule;
use Somnambulist\Components\Validation\Validator;

$rule = new EffectRule() ;

$validator = new Validator( ['effect' => 'deny'] , ['effect' => $rule] );
$validator->passes(); // true

$validator = new Validator( ['effect' => 'unknown'] , ['effect' => $rule] );
$validator->fails(); // true — "effect is not a valid. Allowed values are 'allow' or 'deny'."
Tags
see
Effect

The supported effects for Casbin permissions or RBAC rules.

author

Marc Alcaraz

since
1.0.0

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 EffectRule 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. Allowed values are 'allow' or 'deny'."

Methods

__construct()

Creates a new EffectRule instance.

public __construct() : mixed
Tags
example
$rule = new EffectRule();

$rule->check('allow') ; // valid
$rule->check('deny') ; // valid
$rule->check('unknown') ; // invalid
``

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
example
$rule = (new ConstantsRule(JWTAlgorithm::class))->cases(['HS256', 'HS512']);
Return values
static

check()

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
throws
ParameterException

If required parameters have not been initialized.

example
$rule = new ConstantsRule( JWTAlgorithm::class );
$rule->check('RS512'); // true
$rule->check('MD5');   // false
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.

Tags
example
$rule = (new ConstantsRule(Status::class))->className(Priority::class);
Return values
static

        
On this page

Search results