aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Config/System
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Config/System')
-rw-r--r--core/Command/Config/System/CastHelper.php76
-rw-r--r--core/Command/Config/System/SetConfig.php77
2 files changed, 78 insertions, 75 deletions
diff --git a/core/Command/Config/System/CastHelper.php b/core/Command/Config/System/CastHelper.php
new file mode 100644
index 00000000000..f2b838bdf9b
--- /dev/null
+++ b/core/Command/Config/System/CastHelper.php
@@ -0,0 +1,76 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\Command\Config\System;
+
+class CastHelper {
+ /**
+ * @return array{value: mixed, readable-value: string}
+ */
+ public function castValue(?string $value, string $type): array {
+ switch ($type) {
+ case 'integer':
+ case 'int':
+ if (!is_numeric($value)) {
+ throw new \InvalidArgumentException('Non-numeric value specified');
+ }
+ return [
+ 'value' => (int)$value,
+ 'readable-value' => 'integer ' . (int)$value,
+ ];
+
+ case 'double':
+ case 'float':
+ if (!is_numeric($value)) {
+ throw new \InvalidArgumentException('Non-numeric value specified');
+ }
+ return [
+ 'value' => (float)$value,
+ 'readable-value' => 'double ' . (float)$value,
+ ];
+
+ case 'boolean':
+ case 'bool':
+ $value = strtolower($value);
+ return match ($value) {
+ 'true' => [
+ 'value' => true,
+ 'readable-value' => 'boolean ' . $value,
+ ],
+ 'false' => [
+ 'value' => false,
+ 'readable-value' => 'boolean ' . $value,
+ ],
+ default => throw new \InvalidArgumentException('Unable to parse value as boolean'),
+ };
+
+ case 'null':
+ return [
+ 'value' => null,
+ 'readable-value' => 'null',
+ ];
+
+ case 'string':
+ $value = (string)$value;
+ return [
+ 'value' => $value,
+ 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
+ ];
+
+ case 'json':
+ $value = json_decode($value, true);
+ return [
+ 'value' => $value,
+ 'readable-value' => 'json ' . json_encode($value),
+ ];
+
+ default:
+ throw new \InvalidArgumentException('Invalid type');
+ }
+ }
+}
diff --git a/core/Command/Config/System/SetConfig.php b/core/Command/Config/System/SetConfig.php
index 62ab7f7120f..1b1bdc66a6e 100644
--- a/core/Command/Config/System/SetConfig.php
+++ b/core/Command/Config/System/SetConfig.php
@@ -17,6 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface;
class SetConfig extends Base {
public function __construct(
SystemConfig $systemConfig,
+ private CastHelper $castHelper,
) {
parent::__construct($systemConfig);
}
@@ -57,7 +58,7 @@ class SetConfig extends Base {
protected function execute(InputInterface $input, OutputInterface $output): int {
$configNames = $input->getArgument('name');
$configName = $configNames[0];
- $configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
+ $configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type'));
$updateOnly = $input->getOption('update-only');
if (count($configNames) > 1) {
@@ -81,80 +82,6 @@ class SetConfig extends Base {
}
/**
- * @param string $value
- * @param string $type
- * @return mixed
- * @throws \InvalidArgumentException
- */
- protected function castValue($value, $type) {
- switch ($type) {
- case 'integer':
- case 'int':
- if (!is_numeric($value)) {
- throw new \InvalidArgumentException('Non-numeric value specified');
- }
- return [
- 'value' => (int)$value,
- 'readable-value' => 'integer ' . (int)$value,
- ];
-
- case 'double':
- case 'float':
- if (!is_numeric($value)) {
- throw new \InvalidArgumentException('Non-numeric value specified');
- }
- return [
- 'value' => (float)$value,
- 'readable-value' => 'double ' . (float)$value,
- ];
-
- case 'boolean':
- case 'bool':
- $value = strtolower($value);
- switch ($value) {
- case 'true':
- return [
- 'value' => true,
- 'readable-value' => 'boolean ' . $value,
- ];
-
- case 'false':
- return [
- 'value' => false,
- 'readable-value' => 'boolean ' . $value,
- ];
-
- default:
- throw new \InvalidArgumentException('Unable to parse value as boolean');
- }
-
- // no break
- case 'null':
- return [
- 'value' => null,
- 'readable-value' => 'null',
- ];
-
- case 'string':
- $value = (string)$value;
- return [
- 'value' => $value,
- 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
- ];
-
- case 'json':
- $value = json_decode($value, true);
- return [
- 'value' => $value,
- 'readable-value' => 'json ' . json_encode($value),
- ];
-
- default:
- throw new \InvalidArgumentException('Invalid type');
- }
- }
-
- /**
* @param array $configNames
* @param mixed $existingValues
* @param mixed $value