Add conversion funcs for length (to micrometer), weights (to milligramm)

This commit is contained in:
Joshua Ramon Enslin 2021-12-20 01:23:26 +01:00
parent 9801f1a35f
commit 262972eb54
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -14,6 +14,64 @@ final class MDUnitsSet extends MDValueSet {
const UNITS_LENGTH = ['', 'm', 'dm', 'cm', 'mm'];
const UNITS_WEIGHT = ['', 't', 'kg', 'g'];
const UNITS_LENGTH_TO_MICROMETER = [
'm' => 1000000,
'dm' => 100000,
'cm' => 10000,
'mm' => 1000,
];
const UNITS_WEIGHT_TO_MILLIGRAMM = [
'm' => 1000000,
'dm' => 100000,
'cm' => 10000,
'mm' => 1000,
];
/**
* Converts a given length to micrometers.
*
* @param mixed $value Input value to convert.
* @param string $unit Length unit of the value to convert.
*
* @return float|null
*/
public static function convertLengthToMicrometer(mixed $value, string $unit):float|null {
if (!isset(self::UNITS_LENGTH_TO_MICROMETER[$unit])) return null;
try {
$number = MD_STD_IN::sanitize_float($value);
return floatval($number * self::UNITS_LENGTH_TO_MICROMETER[$unit]);
}
catch (MDgenericInvalidInputsException $e) {
return null;
}
}
/**
* Converts a given length to micrometers.
*
* @param mixed $value Input value to convert.
* @param string $unit Weight unit of the value to convert.
*
* @return float|null
*/
public static function convertWeightToMilligramm(mixed $value, string $unit):float|null {
if (!isset(self::UNITS_WEIGHT_TO_MILLIGRAMM[$unit])) return null;
try {
$number = MD_STD_IN::sanitize_float($value);
return floatval($number * self::UNITS_WEIGHT_TO_MILLIGRAMM[$unit]);
}
catch (MDgenericInvalidInputsException $e) {
return null;
}
}
/**
* Returns a translated list of length units.
*