Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9558cd3aa |
@@ -43,12 +43,17 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
|||||||
'int, no override' => [
|
'int, no override' => [
|
||||||
1,
|
1,
|
||||||
null,
|
null,
|
||||||
null
|
'1'
|
||||||
],
|
],
|
||||||
'int, override set' => [
|
'int, override set' => [
|
||||||
1,
|
1,
|
||||||
'not int',
|
'not int',
|
||||||
'not int'
|
'1'
|
||||||
|
],
|
||||||
|
'array, override set' => [
|
||||||
|
[1, 2],
|
||||||
|
null,
|
||||||
|
null
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -201,7 +206,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
|||||||
'float' => [
|
'float' => [
|
||||||
1.5,
|
1.5,
|
||||||
null,
|
null,
|
||||||
null
|
1
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -349,7 +354,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
|||||||
'int' => [
|
'int' => [
|
||||||
1,
|
1,
|
||||||
null,
|
null,
|
||||||
null
|
1.0
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,16 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
|||||||
'int, no override' => [
|
'int, no override' => [
|
||||||
1,
|
1,
|
||||||
null,
|
null,
|
||||||
''
|
'1'
|
||||||
],
|
],
|
||||||
'int, override set' => [
|
'int, override set' => [
|
||||||
1,
|
1,
|
||||||
'not int',
|
'not int',
|
||||||
|
'1'
|
||||||
|
],
|
||||||
|
'array, override set' => [
|
||||||
|
[1, 2],
|
||||||
|
'not int',
|
||||||
'not int'
|
'not int'
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
@@ -189,7 +194,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
|||||||
'float' => [
|
'float' => [
|
||||||
1.5,
|
1.5,
|
||||||
null,
|
null,
|
||||||
0
|
1
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -330,7 +335,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
|||||||
'int' => [
|
'int' => [
|
||||||
1,
|
1,
|
||||||
null,
|
null,
|
||||||
0.0
|
1.0
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -341,7 +346,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
|||||||
* @dataProvider varSetTypeFloatProvider
|
* @dataProvider varSetTypeFloatProvider
|
||||||
* @testdox setFloat $input with override $default will be $expected [$_dataName]
|
* @testdox setFloat $input with override $default will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param mixed $input
|
* @param mixed $input
|
||||||
* @param float|null $default
|
* @param float|null $default
|
||||||
* @param float $expected
|
* @param float $expected
|
||||||
* @return void
|
* @return void
|
||||||
|
|||||||
@@ -26,8 +26,12 @@ class SetVarTypeMain
|
|||||||
?string $default = null,
|
?string $default = null,
|
||||||
bool $to_null = false
|
bool $to_null = false
|
||||||
): ?string {
|
): ?string {
|
||||||
if (is_string($val)) {
|
if (
|
||||||
return $val;
|
$val === null ||
|
||||||
|
is_scalar($val) ||
|
||||||
|
$val instanceof \Stringable
|
||||||
|
) {
|
||||||
|
return (string)$val;
|
||||||
}
|
}
|
||||||
if ($to_null === false) {
|
if ($to_null === false) {
|
||||||
return (string)$default;
|
return (string)$default;
|
||||||
@@ -39,6 +43,7 @@ class SetVarTypeMain
|
|||||||
* Will convert input data to string if possible.
|
* Will convert input data to string if possible.
|
||||||
* Runs for string/int/float/bool/null
|
* Runs for string/int/float/bool/null
|
||||||
* Will skip array/object/resource/callable/etc and use default for that
|
* 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 mixed $val Input variable
|
||||||
* @param string|null $default Default value
|
* @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
|
* 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
|
* 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 mixed $val Input variable
|
||||||
* @param int|null $default Default value
|
* @param int|null $default Default value
|
||||||
@@ -82,8 +88,8 @@ class SetVarTypeMain
|
|||||||
?int $default = null,
|
?int $default = null,
|
||||||
bool $to_null = false
|
bool $to_null = false
|
||||||
): ?int {
|
): ?int {
|
||||||
if (is_int($val)) {
|
if (is_numeric($val)) {
|
||||||
return $val;
|
return (int)$val;
|
||||||
}
|
}
|
||||||
if ($to_null === false) {
|
if ($to_null === false) {
|
||||||
return (int)$default;
|
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
|
* If input is float return it, else set to default value. If to_null is set
|
||||||
* to true, allow null return
|
* to true, allow null return
|
||||||
|
* Note if an int is sent in, float is returned
|
||||||
*
|
*
|
||||||
* @param mixed $val Input variable
|
* @param mixed $val Input variable
|
||||||
* @param float|null $default Default value
|
* @param float|null $default Default value
|
||||||
@@ -140,8 +147,8 @@ class SetVarTypeMain
|
|||||||
?float $default = null,
|
?float $default = null,
|
||||||
bool $to_null = false
|
bool $to_null = false
|
||||||
): ?float {
|
): ?float {
|
||||||
if (is_float($val)) {
|
if (is_numeric($val)) {
|
||||||
return $val;
|
return (float)$val;
|
||||||
}
|
}
|
||||||
if ($to_null === false) {
|
if ($to_null === false) {
|
||||||
return (float)$default;
|
return (float)$default;
|
||||||
|
|||||||
Reference in New Issue
Block a user