2020-08-06 11:08:10 +02:00
|
|
|
<?PHP
|
|
|
|
/**
|
|
|
|
* Contains a class for controlling the list of available units.
|
|
|
|
*
|
|
|
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
|
|
|
*/
|
2020-08-07 00:12:49 +02:00
|
|
|
declare(strict_types = 1);
|
2020-08-06 11:08:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class containing available units for weights, lengths etc.
|
|
|
|
*/
|
2020-08-29 17:21:18 +02:00
|
|
|
final class MDUnitsSet extends MDValueSet {
|
2020-08-06 11:08:10 +02:00
|
|
|
|
2022-09-02 01:03:41 +02:00
|
|
|
const UNITS_LENGTH = ['', 'm', 'dm', 'cm', 'mm', 'ft', 'in'];
|
2020-08-06 11:08:10 +02:00
|
|
|
const UNITS_WEIGHT = ['', 't', 'kg', 'g'];
|
|
|
|
|
2021-12-20 02:25:17 +01:00
|
|
|
const UNITS_LENGTH_TO_MILLIMETER = [
|
|
|
|
'm' => 1000,
|
2022-09-02 01:01:16 +02:00
|
|
|
'ft' => 304.8,
|
2021-12-20 02:25:17 +01:00
|
|
|
'dm' => 100,
|
2022-09-02 01:01:16 +02:00
|
|
|
'in' => 25.4,
|
2021-12-20 02:25:17 +01:00
|
|
|
'cm' => 10,
|
|
|
|
'mm' => 1,
|
2021-12-20 01:23:26 +01:00
|
|
|
];
|
|
|
|
|
2021-12-20 02:25:17 +01:00
|
|
|
const UNITS_WEIGHT_TO_GRAMM = [
|
|
|
|
't' => 1000000,
|
|
|
|
'kg' => 1000,
|
|
|
|
'g' => 1,
|
2021-12-20 01:23:26 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2021-12-20 02:25:17 +01:00
|
|
|
* Converts a given length to millimeters.
|
2021-12-20 01:23:26 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value Input value to convert.
|
|
|
|
* @param string $unit Length unit of the value to convert.
|
|
|
|
*
|
|
|
|
* @return float|null
|
|
|
|
*/
|
2021-12-20 02:25:17 +01:00
|
|
|
public static function convertLengthToMillimeter(mixed $value, string $unit):float|null {
|
2021-12-20 01:23:26 +01:00
|
|
|
|
2021-12-20 02:25:17 +01:00
|
|
|
if (!isset(self::UNITS_LENGTH_TO_MILLIMETER[$unit])) return null;
|
2021-12-20 01:23:26 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$number = MD_STD_IN::sanitize_float($value);
|
2021-12-20 02:25:17 +01:00
|
|
|
return floatval($number * self::UNITS_LENGTH_TO_MILLIMETER[$unit]);
|
2021-12-20 01:23:26 +01:00
|
|
|
}
|
|
|
|
catch (MDgenericInvalidInputsException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-20 02:25:17 +01:00
|
|
|
* Converts a given weight to gramm.
|
2021-12-20 01:23:26 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value Input value to convert.
|
|
|
|
* @param string $unit Weight unit of the value to convert.
|
|
|
|
*
|
|
|
|
* @return float|null
|
|
|
|
*/
|
2021-12-20 02:25:17 +01:00
|
|
|
public static function convertWeightToGramm(mixed $value, string $unit):float|null {
|
2021-12-20 01:23:26 +01:00
|
|
|
|
2021-12-20 02:25:17 +01:00
|
|
|
if (!isset(self::UNITS_WEIGHT_TO_GRAMM[$unit])) return null;
|
2021-12-20 01:23:26 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$number = MD_STD_IN::sanitize_float($value);
|
2021-12-20 02:25:17 +01:00
|
|
|
return floatval($number * self::UNITS_WEIGHT_TO_GRAMM[$unit]);
|
2021-12-20 01:23:26 +01:00
|
|
|
}
|
|
|
|
catch (MDgenericInvalidInputsException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:18:10 +02:00
|
|
|
/**
|
|
|
|
* Returns a translated list of length units.
|
|
|
|
*
|
|
|
|
* @param MDTlLoader $tlLoader Translation loader.
|
|
|
|
*
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2020-08-07 10:22:12 +02:00
|
|
|
public static function getLengthUnitsTLed(MDTlLoader $tlLoader):array {
|
2020-08-07 10:18:10 +02:00
|
|
|
return parent::getTlUnsortedList($tlLoader, self::UNITS_LENGTH, "units_length_set", "units_length_set");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a translated list of weight units.
|
|
|
|
*
|
|
|
|
* @param MDTlLoader $tlLoader Translation loader.
|
|
|
|
*
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2020-08-07 10:22:12 +02:00
|
|
|
public static function getWeightUnitsTLed(MDTlLoader $tlLoader):array {
|
2020-08-07 10:18:10 +02:00
|
|
|
return parent::getTlUnsortedList($tlLoader, self::UNITS_WEIGHT, "units_weight_set", "units_weight_set");
|
|
|
|
|
|
|
|
}
|
2020-08-06 11:08:10 +02:00
|
|
|
}
|