diff --git a/src/Item.php b/src/Item.php index 88d87b62..82e034ab 100644 --- a/src/Item.php +++ b/src/Item.php @@ -10,8 +10,8 @@ */ abstract class Item { - public const TYPE_ROLE = 'role'; - public const TYPE_PERMISSION = 'permission'; + public const TYPE_ROLE = 1; + public const TYPE_PERMISSION = 2; /** * @var string The item description. @@ -41,9 +41,10 @@ final public function __construct(private string $name) } /** - * @return string Type of the item. + * @return int Type of the item. + * @psalm-return Item::TYPE_* */ - abstract public function getType(): string; + abstract public function getType(): int; /** * @return string Authorization item name. @@ -139,7 +140,7 @@ final public function hasUpdatedAt(): bool * name: string, * description: string, * rule_name: string|null, - * type: string, + * type: Item::TYPE_*, * updated_at: int|null, * created_at: int|null, * } diff --git a/src/Permission.php b/src/Permission.php index c3d7a493..fdde75b2 100644 --- a/src/Permission.php +++ b/src/Permission.php @@ -6,7 +6,7 @@ final class Permission extends Item { - public function getType(): string + public function getType(): int { return self::TYPE_PERMISSION; } diff --git a/src/Role.php b/src/Role.php index 2aef818a..aef4c55a 100644 --- a/src/Role.php +++ b/src/Role.php @@ -6,7 +6,7 @@ final class Role extends Item { - public function getType(): string + public function getType(): int { return self::TYPE_ROLE; } diff --git a/src/SimpleItemsStorage.php b/src/SimpleItemsStorage.php index 4646a796..7e6c946d 100644 --- a/src/SimpleItemsStorage.php +++ b/src/SimpleItemsStorage.php @@ -239,7 +239,7 @@ private function updateItemName(string $name, Item $item): void * * @psalm-return ($type is Item::TYPE_PERMISSION ? array : array) */ - private function getItemsByType(string $type): array + private function getItemsByType(int $type): array { return array_filter( $this->getAll(), @@ -250,7 +250,7 @@ private function getItemsByType(string $type): array /** * @psalm-param Item::TYPE_* $type */ - private function removeItemsByType(string $type): void + private function removeItemsByType(int $type): void { foreach ($this->getItemsByType($type) as $item) { $this->remove($item->getName()); diff --git a/tests/Common/ManagerLogicTestTrait.php b/tests/Common/ManagerLogicTestTrait.php index 0cf012a6..e1c416c5 100644 --- a/tests/Common/ManagerLogicTestTrait.php +++ b/tests/Common/ManagerLogicTestTrait.php @@ -14,6 +14,7 @@ use Yiisoft\Rbac\Exception\ItemAlreadyExistsException; use Yiisoft\Rbac\Exception\RuleInterfaceNotImplementedException; use Yiisoft\Rbac\Exception\RuleNotFoundException; +use Yiisoft\Rbac\Item; use Yiisoft\Rbac\Permission; use Yiisoft\Rbac\Role; use Yiisoft\Rbac\RuleInterface; @@ -727,7 +728,7 @@ public function testAddRole(): void 'name' => 'new role', 'description' => 'new role description', 'rule_name' => TrueRule::class, - 'type' => 'role', + 'type' => Item::TYPE_ROLE, 'updated_at' => 1_642_026_148, 'created_at' => 1_642_026_147, ], @@ -792,7 +793,7 @@ public function testAddPermission(): void 'name' => 'edit post', 'description' => 'edit a post', 'rule_name' => null, - 'type' => 'permission', + 'type' => Item::TYPE_PERMISSION, 'updated_at' => 1_642_026_148, 'created_at' => 1_642_026_147, ],