Files
mytodos/app/FolderRole.php
T
ecdown 177f122c10
tests / ci (push) Canceled after 0s
added roles and organizations
2026-10-15 20:59:32 -07:00

92 lines
2.1 KiB
PHP

<?php
namespace App;
enum FolderRole: string
{
case Admin = 'admin';
case Editor = 'editor';
case Marker = 'marker';
case Viewer = 'viewer';
/**
* Get the human-friendly label for the role.
*/
public function label(): string
{
return match ($this) {
self::Admin => 'Admin',
self::Editor => 'Editor',
self::Marker => 'Marker',
self::Viewer => 'Viewer',
};
}
/**
* Determine whether the role may change a todo's completion state.
*/
public function canMarkComplete(): bool
{
return $this !== self::Viewer;
}
/**
* Determine whether the role may create, edit or delete todos. Editors
* manage todo content only; folder structure and collaborators are an
* admin concern.
*/
public function canEditTodos(): bool
{
return in_array($this, [self::Admin, self::Editor], true);
}
/**
* Determine whether the role may manage the folder itself: its structure,
* visibility and collaborators, and new folders beneath it.
*/
public function canManage(): bool
{
return $this === self::Admin;
}
/**
* Get the folder roles a collaborator may be assigned, most privileged
* first.
*
* @return list<self>
*/
public static function assignable(): array
{
return [self::Admin, self::Editor, self::Marker, self::Viewer];
}
/**
* Get this role's rank, so the strongest of two roles can be picked.
*/
public function rank(): int
{
return match ($this) {
self::Admin => 3,
self::Editor => 2,
self::Marker => 1,
self::Viewer => 0,
};
}
/**
* Get the strongest of the two roles, ignoring missing ones.
*/
public static function higherOf(?self $left, ?self $right): ?self
{
if ($left === null) {
return $right;
}
if ($right === null) {
return $left;
}
return $left->rank() >= $right->rank() ? $left : $right;
}
}