1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Model; |
5
|
|
|
|
6
|
|
|
use Nette\Utils\Arrays; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
use Nexendrie\Utils\Intervals; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Settings Repository |
12
|
|
|
* |
13
|
|
|
* @author Jakub Konečný |
14
|
|
|
* @property-read array $settings |
15
|
|
|
*/ |
16
|
1 |
|
final class SettingsRepository { |
17
|
|
|
protected ThemesManager $themesManager; |
18
|
|
|
|
19
|
|
|
protected array $defaults = [ |
20
|
|
|
"roles" => [ |
21
|
|
|
"guestRole" => 13, |
22
|
|
|
"loggedInRole" => 12, |
23
|
|
|
"bannedRole" => 14, |
24
|
|
|
], |
25
|
|
|
"locale" => [ |
26
|
|
|
"dateFormat" => "j.n.Y", |
27
|
|
|
"dateTimeFormat" => "j.n.Y G:i", |
28
|
|
|
], |
29
|
|
|
"pagination" => [ |
30
|
|
|
"articles" => 10, |
31
|
|
|
], |
32
|
|
|
"newUser" => [ |
33
|
|
|
"style" => "nexendrie", |
34
|
|
|
"money" => 30, |
35
|
|
|
"town" => 3, |
36
|
|
|
], |
37
|
|
|
"fees" => [ |
38
|
|
|
"incomeTax" => 10, |
39
|
|
|
"loanInterest" => 15, |
40
|
|
|
"buildMonastery" => 1000, |
41
|
|
|
"buildCastle" => 1500, |
42
|
|
|
"foundGuild" => 1000, |
43
|
|
|
"foundOrder" => 1200, |
44
|
|
|
"foundTown" => 1000, |
45
|
|
|
"autoFeedMount" => 8, |
46
|
|
|
"depositInterest" => 3, |
47
|
|
|
"buyHouse" => 500, |
48
|
|
|
], |
49
|
|
|
"specialItems" => [ |
50
|
|
|
"foundTown" => 15, |
51
|
|
|
], |
52
|
|
|
"registration" => [ |
53
|
|
|
"token" => "", |
54
|
|
|
], |
55
|
|
|
"site" => [ |
56
|
|
|
"versionSuffix" => "", |
57
|
|
|
], |
58
|
|
|
"features" => [ |
59
|
|
|
"httpCaching" => false, |
60
|
|
|
], |
61
|
|
|
"buildings" => [ |
62
|
|
|
"weeklyWearingOut" => 3, |
63
|
|
|
"criticalCondition" => 30, |
64
|
|
|
], |
65
|
|
|
"socialAccounts" => [ |
66
|
|
|
"facebook" => "nexendrie", |
67
|
|
|
"twitter" => "nexendrieCZ", |
68
|
|
|
"friendica" => "@[email protected]", |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
protected array $rules = [ |
73
|
|
|
"newUser" => [ |
74
|
|
|
"style" => "validateStyle", |
75
|
|
|
"money" => "validateMoney", |
76
|
|
|
], |
77
|
|
|
"fees" => [ |
78
|
|
|
"incomeTax" => "validatePercent", |
79
|
|
|
"loanInterest" => "validatePercent", |
80
|
|
|
"buildMonastery" => "validateFee", |
81
|
|
|
"buildCastle" => "validateFee", |
82
|
|
|
"foundGuild" => "validateFee", |
83
|
|
|
"foundOrder" => "validateFee", |
84
|
|
|
"depositInterest" => "validatePercent", |
85
|
|
|
], |
86
|
|
|
"socialAccounts" => [ |
87
|
|
|
"friendica" => "validateFriendicaAccount", |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
protected array $settings = []; |
92
|
|
|
|
93
|
|
|
use \Nette\SmartObject; |
94
|
|
|
|
95
|
|
|
public function __construct(array $settings, ThemesManager $themesManager) { |
96
|
1 |
|
$this->themesManager = $themesManager; |
97
|
1 |
|
$this->settings = $this->validateSettings($settings); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
protected function validateStyle(string $value): bool { |
101
|
1 |
|
return array_key_exists($value, $this->themesManager->getList()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function validatePercent(int $value): bool { |
105
|
1 |
|
return Intervals::isInInterval($value, "[0,100]"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function validateMoney(int $value): bool { |
109
|
1 |
|
return Intervals::isInInterval($value, "[1,100]"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function validateFee(int $value): bool { |
113
|
1 |
|
return Intervals::isInInterval($value, "[0,5000]"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function validateFriendicaAccount(string $value): bool { |
117
|
1 |
|
return (preg_match('/^@.+@.+/', $value) === 1); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Validate section $name of config |
122
|
|
|
*/ |
123
|
|
|
protected function validateSection(string $name, array $config): array { |
124
|
1 |
|
$values = Arrays::get($config, $name, []); |
125
|
1 |
|
$resolver = new OptionsResolver(); |
126
|
1 |
|
$defaults = $this->defaults[$name]; |
127
|
1 |
|
$resolver->setDefaults($defaults); |
128
|
1 |
|
foreach($defaults as $key => $value) { |
129
|
1 |
|
$resolver->setAllowedTypes($key, gettype($value)); |
130
|
1 |
|
if(isset($this->rules[$name][$key])) { |
131
|
1 |
|
$resolver->setAllowedValues($key, function($value) use($name, $key) { |
132
|
1 |
|
return $this->{$this->rules[$name][$key]}($value); |
133
|
1 |
|
}); |
134
|
|
|
} |
135
|
|
|
} |
136
|
1 |
|
return $resolver->resolve($values); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function validateSettings(array $settings): array { |
140
|
1 |
|
$return = []; |
141
|
1 |
|
$sections = array_keys($this->defaults); |
142
|
1 |
|
foreach($sections as $section) { |
143
|
1 |
|
$return[$section] = $this->validateSection($section, $settings); |
144
|
|
|
} |
145
|
1 |
|
return $return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function getSettings(): array { |
149
|
1 |
|
return $this->settings; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
?> |