7fc57353a8
objects
159 lines
3.6 KiB
PHP
159 lines
3.6 KiB
PHP
<?PHP
|
|
/**
|
|
* Represents a measurement type.
|
|
*
|
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
|
*/
|
|
declare(strict_types = 1);
|
|
|
|
/**
|
|
* Represents a measurement type.
|
|
*/
|
|
enum MDWeightUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface {
|
|
|
|
case t;
|
|
case kg;
|
|
case g;
|
|
|
|
/**
|
|
* Returns a value of this type based on a string.
|
|
*
|
|
* @param string $input Input to get a value from.
|
|
*
|
|
* @return MDWeightUnit
|
|
*/
|
|
public static function fromString(string $input):MDWeightUnit {
|
|
|
|
return match($input) {
|
|
't' => self::t,
|
|
'kg' => self::kg,
|
|
'g' => self::g,
|
|
default => throw new MDpageParameterNotFromListException("Unknown length unit"),
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns a value of this type based on an integer.
|
|
*
|
|
* @param integer $input Input to get a value from.
|
|
*
|
|
* @return MDWeightUnit
|
|
*/
|
|
public static function fromInt(int $input):MDWeightUnit {
|
|
|
|
return match($input) {
|
|
1 => self::t,
|
|
2 => self::kg,
|
|
3 => self::g,
|
|
default => throw new MDpageParameterNotFromListException("Unknown length unit"),
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Lists all available names.
|
|
*
|
|
* @return array<string>
|
|
*/
|
|
public static function caseNames():array {
|
|
|
|
$output = [];
|
|
|
|
$cases = self::cases();
|
|
foreach ($cases as $case) {
|
|
$output[] = $case->name;
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns an integer representation of the collective (for storage in DB).
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function toInt():int {
|
|
|
|
return match($this) {
|
|
self::t => 1,
|
|
self::kg => 2,
|
|
self::g => 3,
|
|
# default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns the measurement calculated down to the base unit (e.g. mm for lengths).
|
|
*
|
|
* @param float $value Measurement value.
|
|
*
|
|
* @return float
|
|
*/
|
|
public function convertToBaseUnit(float $value):float {
|
|
|
|
$multiplier = match($this) {
|
|
self::t => 1000000,
|
|
self::kg => 1000,
|
|
self::g => 1,
|
|
};
|
|
|
|
return floatval($value * $multiplier);
|
|
|
|
}
|
|
|
|
/**
|
|
* Gets an unsorted list of the entries in a translated version.
|
|
*
|
|
* @param MDTlLoader $tlLoader Translation loader.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public static function getUnsortedList(MDTlLoader $tlLoader):array {
|
|
return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_weight_set", "units_weight_set");
|
|
|
|
}
|
|
|
|
/**
|
|
* Gets a sorted list of the entries in a translated version.
|
|
*
|
|
* @param MDTlLoader $tlLoader Translation loader.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public static function getSortedList(MDTlLoader $tlLoader):array {
|
|
return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_weight_set", "units_weight_set");
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns the name of the current value in translation.
|
|
*
|
|
* @param MDTlLoader $tlLoader Translation loader.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getTledName(MDTlLoader $tlLoader):string {
|
|
|
|
return $tlLoader->tl("units_weight_set", "units_weight_set", $this->name);
|
|
|
|
}
|
|
|
|
/**
|
|
* Provides the option to serialize as a string during json_encode().
|
|
*
|
|
* @return string
|
|
*/
|
|
public function jsonSerialize():string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
}
|