0 follower

Final Class Yiisoft\Rbac\CompositeRule

InheritanceYiisoft\Rbac\CompositeRule
ImplementsYiisoft\Rbac\RuleInterface

Composite rule allows combining multiple rules.

// Fresh and owned
$compositeRule = new CompositeRule(CompositeRule::AND, [FreshRule::class, OwnedRule::class]);

// Fresh or owned
$compositeRule = new CompositeRule(CompositeRule::OR, [FreshRule::class, OwnedRule::class]);

Constants

Hide inherited constants

Constant Value Description Defined By
AND 'and' Yiisoft\Rbac\CompositeRule
OR 'or' Yiisoft\Rbac\CompositeRule

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $operator, string[] $ruleNames )
$operator string

Operator to be used. Could be CompositeRule::AND or CompositeRule::OR.

$ruleNames string[]

Array of rule names.

                public function __construct(
    private readonly string $operator,
    private readonly array $ruleNames,
) {
    if (!in_array($operator, [self::AND, self::OR], true)) {
        throw new InvalidArgumentException(
            sprintf(
                'Operator could be either %1$s::AND or %1$s::OR, "%2$s" given.',
                self::class,
                $operator,
            ),
        );
    }
}

            
execute() public method

public boolean execute ( string|null $userId, Yiisoft\Rbac\Item $item, Yiisoft\Rbac\RuleContext $context )
$userId string|null
$item Yiisoft\Rbac\Item
$context Yiisoft\Rbac\RuleContext

                public function execute(?string $userId, Item $item, RuleContext $context): bool
{
    if (empty($this->ruleNames)) {
        return true;
    }
    foreach ($this->ruleNames as $ruleName) {
        $result = $context->createRule($ruleName)->execute($userId, $item, $context);
        if ($this->operator === self::AND && $result === false) {
            return false;
        }
        if ($this->operator === self::OR && $result === true) {
            return true;
        }
    }
    return $this->operator === self::AND;
}