MDAllowedValueSets/src/enums/MDLengthUnit.php

174 lines
4.0 KiB
PHP

<?PHP
/**
* Represents a measurement type.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a measurement type.
*/
enum MDLengthUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface {
case m;
case dm;
case cm;
case mm;
case ft;
case in;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDLengthUnit
*/
public static function fromString(string $input):MDLengthUnit {
return match($input) {
'm' => self::m,
'dm' => self::dm,
'cm' => self::cm,
'mm' => self::mm,
'ft' => self::ft,
'in' => self::in,
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 MDLengthUnit
*/
public static function fromInt(int $input):MDLengthUnit {
return match($input) {
1 => self::m,
2 => self::dm,
3 => self::cm,
4 => self::mm,
5 => self::ft,
6 => self::in,
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::m => 1,
self::dm => 2,
self::cm => 3,
self::mm => 4,
self::ft => 5,
self::in => 6,
# 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::m => 1000,
self::ft => 304.8,
self::dm => 100,
self::in => 25.4,
self::cm => 10,
self::mm => 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_length_set", "units_length_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_length_set", "units_length_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_length_set", "units_length_set", $this->name);
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
}