|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Module\PlayerSetting\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
use Stu\Component\Game\ModuleEnum; |
|
7
|
|
|
use Stu\Component\Player\Settings\UserSettingsProviderInterface; |
|
8
|
|
|
use Stu\Component\Player\UserCssClassEnum; |
|
9
|
|
|
use Stu\Component\Player\UserRpgBehaviorEnum; |
|
10
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
class UserSettingWrapper |
|
13
|
|
|
{ |
|
14
|
1 |
|
public function __construct( |
|
15
|
|
|
private readonly UserInterface $user, |
|
16
|
|
|
private readonly UserSettingEnum $type, |
|
17
|
|
|
private readonly bool $isAdmin, |
|
18
|
|
|
private readonly UserSettingsProviderInterface $userSettingsProvider |
|
19
|
1 |
|
) {} |
|
20
|
|
|
|
|
21
|
1 |
|
public function getType(): UserSettingEnum |
|
22
|
|
|
{ |
|
23
|
1 |
|
return $this->type; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function getParameterName(): string |
|
27
|
|
|
{ |
|
28
|
1 |
|
return $this->type->value; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getCurrentValue(): mixed |
|
32
|
|
|
{ |
|
33
|
1 |
|
return $this->type->getUserValue($this->user, $this->userSettingsProvider); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @return array<mixed> */ |
|
37
|
1 |
|
public function getPossibleValues(): array |
|
38
|
|
|
{ |
|
39
|
1 |
|
return match ($this->type) { |
|
40
|
1 |
|
UserSettingEnum::CSS_COLOR_SHEET => UserCssClassEnum::cases(), |
|
41
|
1 |
|
UserSettingEnum::RPG_BEHAVIOR => UserRpgBehaviorEnum::cases(), |
|
42
|
1 |
|
UserSettingEnum::DEFAULT_VIEW => $this->getFilteredViews($this->user, $this->isAdmin), |
|
43
|
1 |
|
default => throw new RuntimeException(sprintf('%s is not an enum', $this->type->name)) |
|
|
|
|
|
|
44
|
1 |
|
}; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function getEnctype(): ?string |
|
48
|
|
|
{ |
|
49
|
1 |
|
return match ($this->type) { |
|
50
|
1 |
|
UserSettingEnum::AVATAR => "multipart/form-data", |
|
51
|
1 |
|
default => null |
|
52
|
1 |
|
}; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @return array<ModuleEnum> */ |
|
56
|
1 |
|
private function getFilteredViews(UserInterface $user, bool $isAdmin): array |
|
57
|
|
|
{ |
|
58
|
1 |
|
return array_filter(ModuleEnum::cases(), function (ModuleEnum $case) use ($user, $isAdmin): bool { |
|
59
|
|
|
|
|
60
|
1 |
|
if (in_array($case, [ModuleEnum::GAME, ModuleEnum::INDEX, ModuleEnum::NOTES])) { |
|
61
|
1 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
if ($case === ModuleEnum::ADMIN && !$isAdmin) { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
1 |
|
return !($case === ModuleEnum::NPC && !$user->isNpc()); |
|
68
|
1 |
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|