Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,13 @@ function (string $key) use ($prefix): bool {
* @param bool $lazy search within lazy loaded config
* @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others)
*
* @return array<string, string|int|float|bool|array> [appId => configValue]
* @return (
* $typedAs is IAppConfig::VALUE_INT ? array<string, int> : (
* $typedAs is IAppConfig::VALUE_FLOAT ? array<string, float> : (
* $typedAs is IAppConfig::VALUE_BOOL ? array<string, bool> : (
* $typedAs is IAppConfig::VALUE_ARRAY ? array<string, array> : (
* $typedAs is int ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [appId => configValue]
* @since 29.0.0
*/
#[\Override]
Expand Down Expand Up @@ -1590,11 +1596,11 @@ private function convertTypedValue(string $value, int $type): string|int|float|b
return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
case self::VALUE_ARRAY:
try {
return json_decode($value, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// ignoreable
$decoded = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException) {
$decoded = null;
}
break;
return is_array($decoded) ? $decoded : [];
}
return $value;
}
Expand Down
32 changes: 19 additions & 13 deletions lib/private/Config/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,13 @@ public function getAllValues(string $userId, bool $filtered = false): array {
* @param bool $lazy search within lazy loaded config
* @param ValueType|null $typedAs enforce type for the returned values
*
* @return array<string, string|int|float|bool|array> [appId => value]
* @return (
* $typedAs is ValueType::INT ? array<string, int> : (
* $typedAs is ValueType::FLOAT ? array<string, float> : (
* $typedAs is ValueType::BOOL ? array<string, bool> : (
* $typedAs is ValueType::ARRAY ? array<string, array> : (
* $typedAs is ValueType ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [appId => value]
* @since 31.0.0
*/
#[\Override]
Expand All @@ -354,11 +360,10 @@ public function getValuesByApps(string $userId, string $key, bool $lazy = false,
$entry = $cache[$app][$key];
try {
$value = $this->getDecryptedSensitiveValue($userId, $app, $key, $entry);
$value = $this->convertTypedValue($value, $typedAs ?? $entry->getType());
} catch (IncorrectTypeException|UnknownKeyException) {
$value = $entry->getRawValue();
}
$values[$app] = $value;
$values[$app] = $this->convertTypedValue($value, $typedAs ?? $entry->getType());
}
}

Expand All @@ -373,7 +378,13 @@ public function getValuesByApps(string $userId, string $key, bool $lazy = false,
* @param ValueType|null $typedAs enforce type for the returned values
* @param array|null $userIds limit to a list of user ids
*
* @return array<string, string|int|float|bool|array> [userId => value]
* @return (
* $typedAs is ValueType::INT ? array<string, int> : (
* $typedAs is ValueType::FLOAT ? array<string, float> : (
* $typedAs is ValueType::BOOL ? array<string, bool> : (
* $typedAs is ValueType::ARRAY ? array<string, array> : (
* $typedAs is ValueType ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [userId => value]
* @since 31.0.0
*/
#[\Override]
Expand All @@ -397,12 +408,7 @@ public function getValuesByUsers(
$executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult {
$result = $qb->executeQuery();
while ($row = $result->fetchAssociative()) {
$value = $row['configvalue'];
try {
$value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type']));
} catch (IncorrectTypeException) {
}
$values[$row['userid']] = $value;
$values[$row['userid']] = $this->convertTypedValue($row['configvalue'], $typedAs ?? ValueType::from((int)$row['type']));
}
return $result;
};
Expand Down Expand Up @@ -1934,11 +1940,11 @@ private function convertTypedValue(string $value, ValueType $type): string|int|f
return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
case ValueType::ARRAY:
try {
return json_decode($value, true, flags: JSON_THROW_ON_ERROR);
$decoded = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException) {
// ignoreable
$decoded = null;
}
break;
return is_array($decoded) ? $decoded : [];
}
return $value;
}
Expand Down
16 changes: 14 additions & 2 deletions lib/public/Config/IUserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ public function getAllValues(string $userId, bool $filtered = false): array;
* @param bool $lazy search within lazy loaded config
* @param ValueType|null $typedAs enforce type for the returned values
*
* @return array<string, string|int|float|bool|array> [appId => value]
* @return (
* $typedAs is ValueType::INT ? array<string, int> : (
* $typedAs is ValueType::FLOAT ? array<string, float> : (
* $typedAs is ValueType::BOOL ? array<string, bool> : (
* $typedAs is ValueType::ARRAY ? array<string, array> : (
* $typedAs is ValueType ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [appId => value]
* @throws \InvalidArgumentException if $userId or $key is invalid (too long, or empty string)
*
* @since 32.0.0
Expand All @@ -220,7 +226,13 @@ public function getValuesByApps(string $userId, string $key, bool $lazy = false,
* @param ValueType|null $typedAs enforce type for the returned values
* @param array|null $userIds limit the search to a list of user ids
*
* @return array<string, string|int|float|bool|array> [userId => value]
* @return (
* $typedAs is ValueType::INT ? array<string, int> : (
* $typedAs is ValueType::FLOAT ? array<string, float> : (
* $typedAs is ValueType::BOOL ? array<string, bool> : (
* $typedAs is ValueType::ARRAY ? array<string, array> : (
* $typedAs is ValueType ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [userId => value]
* @throws \InvalidArgumentException if $app or $key is invalid (too long, or empty string)
*
* @since 32.0.0
Expand Down
8 changes: 7 additions & 1 deletion lib/public/IAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ public function getAllValues(string $app, string $prefix = '', bool $filtered =
* @param bool $lazy search within lazy loaded config
* @param int|null $typedAs enforce type for the returned values {@see self::VALUE_STRING} and others
*
* @return array<string, string|int|float|bool|array> [appId => configValue]
* @return (
* $typedAs is self::VALUE_INT ? array<string, int> : (
* $typedAs is self::VALUE_FLOAT ? array<string, float> : (
* $typedAs is self::VALUE_BOOL ? array<string, bool> : (
* $typedAs is self::VALUE_ARRAY ? array<string, array> : (
* $typedAs is int ? array<string, string> :
* array<string, string|int|float|bool|array>))))) [appId => configValue]
* @since 29.0.0
*/
public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array;
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/AppConfigIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,49 @@ public function testSearchValues(): void {
$this->assertEqualsCanonicalizing(['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no'], $config->searchValues('enabled'));
}

public static function providerSearchValuesTypedAs(): array {
return [
[
'enabled', IAppConfig::VALUE_STRING,
['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no']
],
[
'enabled', IAppConfig::VALUE_MIXED,
['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no']
],
[
'enabled', IAppConfig::VALUE_BOOL,
['testapp' => true, '123456' => true, 'anotherapp' => false]
],
[
'enabled', IAppConfig::VALUE_INT,
['testapp' => 0, '123456' => 0, 'anotherapp' => 0]
],
[
'enabled', IAppConfig::VALUE_ARRAY,
['testapp' => [], '123456' => [], 'anotherapp' => []]
],
[
'array', IAppConfig::VALUE_ARRAY,
['typed' => ['test' => 1]]
],
[
'int', IAppConfig::VALUE_INT,
['typed' => 42]
],
[
'float', IAppConfig::VALUE_FLOAT,
['typed' => 3.14]
],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerSearchValuesTypedAs')]
public function testSearchValuesTypedAs(string $key, int $typedAs, array $result): void {
$config = $this->generateAppConfig();
$this->assertEqualsCanonicalizing($result, $config->searchValues($key, false, $typedAs));
}

public function testGetValueString(): void {
$config = $this->generateAppConfig();
$this->assertSame('value', $config->getValueString('typed', 'string', ''));
Expand Down
54 changes: 54 additions & 0 deletions tests/lib/Config/UserConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,32 @@ public static function providerSearchValuesByApps(): array {
'app2' => 0,
'app3' => 0,
]
],
[
'user1', 'key1', false, ValueType::BOOL,
[
'app1' => false,
'app3' => false,
]
],
[
'user1', 'key1', false, ValueType::ARRAY,
[
'app1' => [],
'app3' => [],
]
],
[
'user1', 'fast_array', false, ValueType::ARRAY,
[
'app1' => ['year' => 2024],
]
],
[
'user1', 'fast_array_sensitive', false, ValueType::ARRAY,
[
'app1' => ['password' => 'pwd'],
]
]
];
}
Expand Down Expand Up @@ -739,6 +765,34 @@ public static function providerSearchValuesByUsers(): array {
'user5' => 12,
]
],
[
'app3', 'key10', null, null,
[
'user1' => true,
'user2' => false,
'user4' => true,
]
],
[
'app2', 'key2', ValueType::BOOL, ['user1', 'user3'],
[
'user1' => false,
'user3' => false,
]
],
[
'app2', 'key2', ValueType::ARRAY, ['user1', 'user3'],
[
'user1' => [],
'user3' => [],
]
],
[
'app1', 'fast_array', ValueType::ARRAY, null,
[
'user1' => ['year' => 2024],
]
],
];
}

Expand Down
Loading