diff --git a/core/Command/Config/System/SetConfig.php b/core/Command/Config/System/SetConfig.php index 1b1bdc66a6e..df7343b86ea 100644 --- a/core/Command/Config/System/SetConfig.php +++ b/core/Command/Config/System/SetConfig.php @@ -81,6 +81,80 @@ class SetConfig extends Base { return 0; } + /** + * @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 diff --git a/tests/Core/Command/Config/System/SetConfigTest.php b/tests/Core/Command/Config/System/SetConfigTest.php index cf44058f670..d7ced242eb7 100644 --- a/tests/Core/Command/Config/System/SetConfigTest.php +++ b/tests/Core/Command/Config/System/SetConfigTest.php @@ -1,4 +1,5 @@ systemConfig->expects($this->once()) ->method('setValue') ->with($configNames[0], $expectedValue); @@ -89,7 +90,7 @@ class SetConfigTest extends TestCase { /** * @dataProvider setUpdateOnlyProvider */ - public function testSetUpdateOnly($configNames, $existingData): void { + public function testSetUpdateOnly($configNames, $existingData) { $this->expectException(\UnexpectedValueException::class); $this->systemConfig->expects($this->never()) @@ -136,7 +137,7 @@ class SetConfigTest extends TestCase { /** * @dataProvider castValueProvider */ - public function testCastValue($value, $type, $expectedValue): void { + public function testCastValue($value, $type, $expectedValue) { $this->assertSame($expectedValue, $this->invokePrivate($this->command, 'castValue', [$value, $type]) ); @@ -157,7 +158,7 @@ class SetConfigTest extends TestCase { /** * @dataProvider castValueInvalidProvider */ - public function testCastValueInvalid($value, $type): void { + public function testCastValueInvalid($value, $type) { $this->expectException(\InvalidArgumentException::class); $this->invokePrivate($this->command, 'castValue', [$value, $type]);