From f9558cd3aa9d15c4592de05a72181c33bbde9fe8 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 5 Sep 2023 14:19:43 +0900 Subject: [PATCH] Fix for the SetVarType / SetVarTypeNull string: if it is stringable in anyway, set string (it converts) this is not check IF it is a string value as it was before int/float: same, if it is numerc it will be convert to int or float All other stay the same Note "set ..." imply to set, and not to convert to 0 if it is int string that can be covnerted to int --- .../CoreLibsConvertSetVarTypeNullTest.php | 13 +++++++++---- .../Convert/CoreLibsConvertSetVarTypeTest.php | 13 +++++++++---- .../Convert/Extends/SetVarTypeMain.php | 19 +++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/4dev/tests/Convert/CoreLibsConvertSetVarTypeNullTest.php b/4dev/tests/Convert/CoreLibsConvertSetVarTypeNullTest.php index 88c638d0..6afab615 100644 --- a/4dev/tests/Convert/CoreLibsConvertSetVarTypeNullTest.php +++ b/4dev/tests/Convert/CoreLibsConvertSetVarTypeNullTest.php @@ -43,12 +43,17 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase 'int, no override' => [ 1, null, - null + '1' ], 'int, override set' => [ 1, 'not int', - 'not int' + '1' + ], + 'array, override set' => [ + [1, 2], + null, + null ] ]; } @@ -201,7 +206,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase 'float' => [ 1.5, null, - null + 1 ] ]; } @@ -349,7 +354,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase 'int' => [ 1, null, - null + 1.0 ] ]; } diff --git a/4dev/tests/Convert/CoreLibsConvertSetVarTypeTest.php b/4dev/tests/Convert/CoreLibsConvertSetVarTypeTest.php index 8afe8611..1344b13b 100644 --- a/4dev/tests/Convert/CoreLibsConvertSetVarTypeTest.php +++ b/4dev/tests/Convert/CoreLibsConvertSetVarTypeTest.php @@ -43,11 +43,16 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase 'int, no override' => [ 1, null, - '' + '1' ], 'int, override set' => [ 1, 'not int', + '1' + ], + 'array, override set' => [ + [1, 2], + 'not int', 'not int' ] ]; @@ -189,7 +194,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase 'float' => [ 1.5, null, - 0 + 1 ] ]; } @@ -330,7 +335,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase 'int' => [ 1, null, - 0.0 + 1.0 ] ]; } @@ -341,7 +346,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase * @dataProvider varSetTypeFloatProvider * @testdox setFloat $input with override $default will be $expected [$_dataName] * - * @param mixed $input + * @param mixed $input * @param float|null $default * @param float $expected * @return void diff --git a/www/lib/CoreLibs/Convert/Extends/SetVarTypeMain.php b/www/lib/CoreLibs/Convert/Extends/SetVarTypeMain.php index 98ff0531..d32e416e 100644 --- a/www/lib/CoreLibs/Convert/Extends/SetVarTypeMain.php +++ b/www/lib/CoreLibs/Convert/Extends/SetVarTypeMain.php @@ -26,8 +26,12 @@ class SetVarTypeMain ?string $default = null, bool $to_null = false ): ?string { - if (is_string($val)) { - return $val; + if ( + $val === null || + is_scalar($val) || + $val instanceof \Stringable + ) { + return (string)$val; } if ($to_null === false) { return (string)$default; @@ -39,6 +43,7 @@ class SetVarTypeMain * Will convert input data to string if possible. * Runs for string/int/float/bool/null * Will skip array/object/resource/callable/etc and use default for that + * Note: this is pretty much the same as setStrMain because string is easy * * @param mixed $val Input variable * @param string|null $default Default value @@ -71,6 +76,7 @@ class SetVarTypeMain /** * If input variable is int, return it, else return default value. If to_null * is true then null as return is allowed, else only int is returned + * Note, if float is sent in, int is returned * * @param mixed $val Input variable * @param int|null $default Default value @@ -82,8 +88,8 @@ class SetVarTypeMain ?int $default = null, bool $to_null = false ): ?int { - if (is_int($val)) { - return $val; + if (is_numeric($val)) { + return (int)$val; } if ($to_null === false) { return (int)$default; @@ -129,6 +135,7 @@ class SetVarTypeMain /** * If input is float return it, else set to default value. If to_null is set * to true, allow null return + * Note if an int is sent in, float is returned * * @param mixed $val Input variable * @param float|null $default Default value @@ -140,8 +147,8 @@ class SetVarTypeMain ?float $default = null, bool $to_null = false ): ?float { - if (is_float($val)) { - return $val; + if (is_numeric($val)) { + return (float)$val; } if ($to_null === false) { return (float)$default;