refactor: split off value casting out of config:system:set command
Signed-off-by: Robin Appelman <robin@icewind.nl>pull/54641/head
parent
9ba1aae362
commit
642a81a8d4
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
namespace Core\Command\Config\System;
|
||||
|
||||
use OC\Core\Command\Config\System\CastHelper;
|
||||
use Test\TestCase;
|
||||
|
||||
class CastHelperTest extends TestCase {
|
||||
private CastHelper $castHelper;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->castHelper = new CastHelper();
|
||||
}
|
||||
|
||||
public static function castValueProvider(): array {
|
||||
return [
|
||||
[null, 'string', ['value' => '', 'readable-value' => 'empty string']],
|
||||
|
||||
['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],
|
||||
|
||||
['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
|
||||
['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],
|
||||
|
||||
['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
|
||||
['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],
|
||||
|
||||
['', 'null', ['value' => null, 'readable-value' => 'null']],
|
||||
|
||||
['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
|
||||
['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider castValueProvider
|
||||
*/
|
||||
public function testCastValue($value, $type, $expectedValue): void {
|
||||
$this->assertSame(
|
||||
$expectedValue,
|
||||
$this->castHelper->castValue($value, $type)
|
||||
);
|
||||
}
|
||||
|
||||
public static function castValueInvalidProvider(): array {
|
||||
return [
|
||||
['123', 'foobar'],
|
||||
|
||||
[null, 'integer'],
|
||||
['abc', 'integer'],
|
||||
['76ggg', 'double'],
|
||||
['true', 'float'],
|
||||
['foobar', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider castValueInvalidProvider
|
||||
*/
|
||||
public function testCastValueInvalid($value, $type): void {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->castHelper->castValue($value, $type);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue