|
1 | 1 | <?php |
2 | 2 | namespace OCA\Solid; |
3 | 3 |
|
| 4 | + use InvalidArgumentException; |
4 | 5 | use OCP\IConfig; |
5 | 6 |
|
6 | 7 | class BaseServerConfig { |
| 8 | + |
| 9 | + ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 10 | + |
| 11 | + public const ERROR_INVALID_ARGUMENT = 'Invalid %s for %s: %s. Must be one of %s.'; |
| 12 | + |
7 | 13 | protected IConfig $config; |
8 | 14 |
|
9 | | - /** |
10 | | - * @param IConfig $config |
11 | | - */ |
| 15 | + //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 16 | + |
12 | 17 | public function __construct(IConfig $config) { |
13 | 18 | $this->config = $config; |
14 | 19 | } |
@@ -152,6 +157,7 @@ public function removeClientConfig($clientId) { |
152 | 157 | unset($scopes[$clientId]); |
153 | 158 | $this->config->setAppValue('solid', 'clientScopes', $scopes); |
154 | 159 | } |
| 160 | + |
155 | 161 | public function saveClientRegistration($origin, $clientData) { |
156 | 162 | $originHash = md5($origin); |
157 | 163 | $existingRegistration = $this->getClientRegistration($originHash); |
@@ -183,11 +189,56 @@ public function getClientRegistration($clientId) { |
183 | 189 | return json_decode($data, true); |
184 | 190 | } |
185 | 191 |
|
186 | | - public function getUserSubDomainsEnabled(): bool { |
187 | | - return $this->config->getAppValue('solid', 'userSubDomainsEnabled', false); |
| 192 | + public function getUserSubDomainsEnabled() { |
| 193 | + $value = $this->config->getAppValue('solid', 'userSubDomainsEnabled', false); |
| 194 | + |
| 195 | + return $this->castToBool($value); |
188 | 196 | } |
189 | 197 |
|
190 | | - public function setUserSubDomainsEnabled(bool $enabled) { |
191 | | - $this->config->setAppValue('solid', 'userSubDomainsEnabled', $enabled); |
| 198 | + public function setUserSubDomainsEnabled($enabled) { |
| 199 | + $value = $this->castToBool($enabled); |
| 200 | + |
| 201 | + $this->config->setAppValue('solid', 'userSubDomainsEnabled', $value); |
| 202 | + } |
| 203 | + |
| 204 | + ////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 205 | + |
| 206 | + private function castToBool(string $mixedValue): bool |
| 207 | + { |
| 208 | + $type = gettype($mixedValue); |
| 209 | + |
| 210 | + if ($type === 'boolean' || $type === 'NULL' || $type === 'integer') { |
| 211 | + $value = (bool) $mixedValue; |
| 212 | + } else { |
| 213 | + if ($type === 'string') { |
| 214 | + $mixedValue = strtolower($mixedValue); |
| 215 | + if ($mixedValue === 'true' || $mixedValue === '1') { |
| 216 | + $value = true; |
| 217 | + } elseif ($mixedValue === 'false' || $mixedValue === '0') { |
| 218 | + $value = false; |
| 219 | + } else { |
| 220 | + $error = [ |
| 221 | + 'invalid' => 'value', |
| 222 | + 'for' => 'userSubDomainsEnabled', |
| 223 | + 'received' => $mixedValue, |
| 224 | + 'expected' => implode(',', ['true', 'false', '1', '0']) |
| 225 | + ]; |
| 226 | + } |
| 227 | + } else { |
| 228 | + $error = [ |
| 229 | + 'invalid' => 'type', |
| 230 | + 'for' => 'userSubDomainsEnabled', |
| 231 | + 'received' => $type, |
| 232 | + 'expected' => implode(',', ['boolean', 'NULL', 'integer', 'string']) |
| 233 | + ]; |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + if (isset($error)) { |
| 238 | + $errorMessage = vsprintf(self::ERROR_INVALID_ARGUMENT, $error); |
| 239 | + throw new InvalidArgumentException($errorMessage); |
| 240 | + } |
| 241 | + |
| 242 | + return $value; |
192 | 243 | } |
193 | 244 | } |
0 commit comments