Skip to content

Commit

Permalink
Improve compatibility of item's type with Yii 2
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Oct 8, 2024
1 parent 9cee78e commit 3c980f5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -41,9 +41,9 @@ final public function __construct(private string $name)
}

/**
* @return string Type of the item.
* @return int Type of the item.
*/
abstract public function getType(): string;
abstract public function getType(): int;

/**
* @return string Authorization item name.
Expand Down
2 changes: 1 addition & 1 deletion src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Permission extends Item
{
public function getType(): string
public function getType(): int
{
return self::TYPE_PERMISSION;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Role extends Item
{
public function getType(): string
public function getType(): int
{
return self::TYPE_ROLE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private function updateItemName(string $name, Item $item): void
*
* @psalm-return ($type is Item::TYPE_PERMISSION ? array<string, Permission> : array<string, Role>)
*/
private function getItemsByType(string $type): array
private function getItemsByType(int $type): array
{
return array_filter(
$this->getAll(),
Expand All @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions tests/Common/ManagerLogicTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand Down

0 comments on commit 3c980f5

Please sign in to comment.