From 262972eb541f0330e90e1940fdfcfd3b762a9c7a Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 20 Dec 2021 01:23:26 +0100 Subject: [PATCH] Add conversion funcs for length (to micrometer), weights (to milligramm) --- src/MDUnitsSet.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/MDUnitsSet.php b/src/MDUnitsSet.php index bd162c6..903d033 100644 --- a/src/MDUnitsSet.php +++ b/src/MDUnitsSet.php @@ -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. *