*/ public static function optionsFor(UserType $type): array { return array_values(array_filter(self::cases(), fn (self $page): bool => $page->appliesTo($type))); } public function appliesTo(UserType $type): bool { return match ($this) { self::Clients, self::Activity => $type === UserType::Staff, default => true, }; } /** * What an account of this type needs to open the page, or null when * every account of that type can. */ public function requiredPermission(UserType $type): ?Permission { $staff = $type === UserType::Staff; return match ($this) { self::Dashboard => null, self::Files => $staff ? Permission::Upload : null, self::Upload => Permission::Upload, self::Groups => $staff ? Permission::ManageGroups : null, self::Clients => Permission::ManageClients, self::Activity => Permission::ViewActionsLog, }; } public function routeName(UserType $type): string { $staff = $type === UserType::Staff; return match ($this) { self::Dashboard => 'dashboard', self::Files => $staff ? 'files.index' : 'my-files.index', self::Upload => $staff ? 'files.create' : 'my-files.upload.create', self::Groups => $staff ? 'groups.index' : 'my-groups.index', self::Clients => 'clients.index', self::Activity => 'activity.index', }; } /** * English, and the translation key: the same words the navigation * already uses for each page, so they are already translated. */ public function label(UserType $type): string { $staff = $type === UserType::Staff; return match ($this) { self::Dashboard => 'Dashboard', self::Files => $staff ? 'Files' : 'My files', self::Upload => 'Upload files', self::Groups => $staff ? 'Groups' : 'My groups', self::Clients => 'Clients', self::Activity => 'Activity log', }; } public function isReachableBy(User $user): bool { if (! $this->appliesTo($user->type)) { return false; } $permission = $this->requiredPermission($user->type); return $permission === null || $user->can($permission->value); } }