Skip to content

Commit

Permalink
format app values
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Feb 1, 2024
1 parent 937a6a8 commit 56af4d1
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function getAllValues(string $app, string $key = '', bool $filtered = fal
// if we want to filter values, we need to get sensitivity
$this->loadConfigAll();
// array_merge() will remove numeric keys (here config keys), so addition arrays instead
$values = ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? []);
$values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? []));

if (!$filtered) {
return $values;
Expand Down Expand Up @@ -262,7 +262,8 @@ public function searchValues(string $key, bool $lazy = false): array {

foreach (array_keys($cache) as $app) {
if (isset($cache[$app][$key])) {
$values[$app] = $cache[$app][$key];
$appCache = $this->formatAppValues((string)$app, $cache[$app]);
$values[$app] = $appCache[$key];
}
}

Expand Down Expand Up @@ -1296,7 +1297,7 @@ public function setValue($app, $key, $value) {
* @param string|false $key
*
* @return array|false
* @deprecated 29.0.0 use getAllValues()
* @deprecated 29.0.0 use {@see getAllValues()}
*/
public function getValues($app, $key) {
if (($app !== false) === ($key !== false)) {
Expand All @@ -1317,12 +1318,45 @@ public function getValues($app, $key) {
* @param string $app
*
* @return array
* @deprecated 29.0.0 use getAllValues()
* @deprecated 29.0.0 use {@see getAllValues()}
*/
public function getFilteredValues($app) {
return $this->getAllValues($app, filtered: true);
}


/**
* @param string $app
* @param array $values
*
* @return array
* @throws AppConfigUnknownKeyException
*/
private function formatAppValues(string $app, array $values): array {
foreach($values as $key => $value) {
switch ($this->getValueType($app, $key)) {
case self::VALUE_INT:
$values[$key] = (int)$value;
break;
case self::VALUE_FLOAT:
$values[$key] = (float)$value;
break;
case self::VALUE_BOOL:
$values[$key] = in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
break;
case self::VALUE_ARRAY:
try {
$values[$key] = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// ignoreable
}
break;
}
}

return $values;
}

/**
* @param string $app
*
Expand Down

0 comments on commit 56af4d1

Please sign in to comment.