Final Class Yiisoft\Rbac\CompositeRule
| Inheritance | Yiisoft\Rbac\CompositeRule |
|---|---|
| Implements | Yiisoft\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]);
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Rbac\CompositeRule | |
| execute() | Yiisoft\Rbac\CompositeRule |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| AND | 'and' | Yiisoft\Rbac\CompositeRule | |
| OR | 'or' | Yiisoft\Rbac\CompositeRule |
Method Details
| public mixed __construct ( string $operator, string[] $ruleNames ) | ||
| $operator | string |
Operator to be used. Could be |
| $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,
),
);
}
}
| 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;
}
Signup or Login in order to comment.