Skip to content

Commit 83281c4

Browse files
committed
Add logic to cast mixed AppValue to boolean.
1 parent 7b9ea41 commit 83281c4

1 file changed

Lines changed: 58 additions & 7 deletions

File tree

solid/lib/BaseServerConfig.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
<?php
22
namespace OCA\Solid;
33

4+
use InvalidArgumentException;
45
use OCP\IConfig;
56

67
class BaseServerConfig {
8+
9+
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
10+
11+
public const ERROR_INVALID_ARGUMENT = 'Invalid %s for %s: %s. Must be one of %s.';
12+
713
protected IConfig $config;
814

9-
/**
10-
* @param IConfig $config
11-
*/
15+
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
16+
1217
public function __construct(IConfig $config) {
1318
$this->config = $config;
1419
}
@@ -152,6 +157,7 @@ public function removeClientConfig($clientId) {
152157
unset($scopes[$clientId]);
153158
$this->config->setAppValue('solid', 'clientScopes', $scopes);
154159
}
160+
155161
public function saveClientRegistration($origin, $clientData) {
156162
$originHash = md5($origin);
157163
$existingRegistration = $this->getClientRegistration($originHash);
@@ -183,11 +189,56 @@ public function getClientRegistration($clientId) {
183189
return json_decode($data, true);
184190
}
185191

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);
188196
}
189197

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;
192243
}
193244
}

0 commit comments

Comments
 (0)