Use gramms and millimeters as base weight and length units

This commit is contained in:
Joshua Ramon Enslin 2021-12-20 02:25:17 +01:00
parent 262972eb54
commit 16f0621a9a
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -14,35 +14,34 @@ final class MDUnitsSet extends MDValueSet {
const UNITS_LENGTH = ['', 'm', 'dm', 'cm', 'mm']; const UNITS_LENGTH = ['', 'm', 'dm', 'cm', 'mm'];
const UNITS_WEIGHT = ['', 't', 'kg', 'g']; const UNITS_WEIGHT = ['', 't', 'kg', 'g'];
const UNITS_LENGTH_TO_MICROMETER = [ const UNITS_LENGTH_TO_MILLIMETER = [
'm' => 1000000, 'm' => 1000,
'dm' => 100000, 'dm' => 100,
'cm' => 10000, 'cm' => 10,
'mm' => 1000, 'mm' => 1,
]; ];
const UNITS_WEIGHT_TO_MILLIGRAMM = [ const UNITS_WEIGHT_TO_GRAMM = [
'm' => 1000000, 't' => 1000000,
'dm' => 100000, 'kg' => 1000,
'cm' => 10000, 'g' => 1,
'mm' => 1000,
]; ];
/** /**
* Converts a given length to micrometers. * Converts a given length to millimeters.
* *
* @param mixed $value Input value to convert. * @param mixed $value Input value to convert.
* @param string $unit Length unit of the value to convert. * @param string $unit Length unit of the value to convert.
* *
* @return float|null * @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 { try {
$number = MD_STD_IN::sanitize_float($value); $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) { catch (MDgenericInvalidInputsException $e) {
return null; 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 mixed $value Input value to convert.
* @param string $unit Weight unit of the value to convert. * @param string $unit Weight unit of the value to convert.
* *
* @return float|null * @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 { try {
$number = MD_STD_IN::sanitize_float($value); $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) { catch (MDgenericInvalidInputsException $e) {
return null; return null;