From 16f0621a9a69ec371eabe66a9276d7001f4ad5c0 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 20 Dec 2021 02:25:17 +0100 Subject: [PATCH] Use gramms and millimeters as base weight and length units --- src/MDUnitsSet.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/MDUnitsSet.php b/src/MDUnitsSet.php index 903d033..bf6ab5a 100644 --- a/src/MDUnitsSet.php +++ b/src/MDUnitsSet.php @@ -14,35 +14,34 @@ 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_LENGTH_TO_MILLIMETER = [ + 'm' => 1000, + 'dm' => 100, + 'cm' => 10, + 'mm' => 1, ]; - const UNITS_WEIGHT_TO_MILLIGRAMM = [ - 'm' => 1000000, - 'dm' => 100000, - 'cm' => 10000, - 'mm' => 1000, + const UNITS_WEIGHT_TO_GRAMM = [ + 't' => 1000000, + 'kg' => 1000, + 'g' => 1, ]; /** - * Converts a given length to micrometers. + * Converts a given length to millimeters. * * @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 { + public static function convertLengthToMillimeter(mixed $value, string $unit):float|null { - if (!isset(self::UNITS_LENGTH_TO_MICROMETER[$unit])) return null; + if (!isset(self::UNITS_LENGTH_TO_MILLIMETER[$unit])) return null; try { $number = MD_STD_IN::sanitize_float($value); - return floatval($number * self::UNITS_LENGTH_TO_MICROMETER[$unit]); + return floatval($number * self::UNITS_LENGTH_TO_MILLIMETER[$unit]); } catch (MDgenericInvalidInputsException $e) { return null; @@ -51,20 +50,20 @@ final class MDUnitsSet extends MDValueSet { } /** - * Converts a given length to micrometers. + * Converts a given weight to gramm. * * @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 { + public static function convertWeightToGramm(mixed $value, string $unit):float|null { - if (!isset(self::UNITS_WEIGHT_TO_MILLIGRAMM[$unit])) return null; + if (!isset(self::UNITS_WEIGHT_TO_GRAMM[$unit])) return null; try { $number = MD_STD_IN::sanitize_float($value); - return floatval($number * self::UNITS_WEIGHT_TO_MILLIGRAMM[$unit]); + return floatval($number * self::UNITS_WEIGHT_TO_GRAMM[$unit]); } catch (MDgenericInvalidInputsException $e) { return null;