Add enum for representing phone types

This commit is contained in:
2026-08-13 11:44:49 +02:00
parent 4c8f569854
commit ce3b4e436f
+188
View File
@@ -0,0 +1,188 @@
<?PHP
declare(strict_types = 1);
/**
* Represents a type of phone.
*/
enum MDPhoneType implements MDValueEnumInterface, JsonSerializable {
case work;
case home;
case mobile;
case work_mobile;
case fax;
case fax_work;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDPhoneType
*/
public static function fromString(string $input):MDPhoneType {
return match($input) {
"work" => self::work,
"home" => self::home,
"mobile" => self::mobile,
"work mobile",
"work_mobile" => self::work_mobile,
"fax" => self::fax,
"fax work",
"fax_work" => self::fax_work,
default => throw new MDpageParameterNotFromListException("Unknown phone type"),
};
}
/**
* Returns a value of this type based on an integer.
*
* @param integer $input Input to get a value from.
*
* @return MDPhoneType
*/
public static function fromInt(int $input):MDPhoneType {
return match($input) {
0 => self::work,
1 => self::home,
2 => self::mobile,
3 => self::work_mobile,
4 => self::fax,
5 => self::fax_work,
default => throw new MDpageParameterNotFromListException("Unknown phone type"),
};
}
/**
* 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;
}
/**
* 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 {
$output = [];
foreach (self::cases() as $case) {
$output[$case->toString()] = $tlLoader->tl("phone_types", "phone_types", $case->toString());
}
return $output;
}
/**
* 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 {
$output = self::getUnsortedList($tlLoader);
asort($output);
return $output;
}
/**
* Lists all available names.
*
* @return array<integer>
*/
public static function caseInts():array {
$output = [];
$cases = self::cases();
foreach ($cases as $case) {
$output[] = $case->toInt();
}
return $output;
}
/**
* Returns integer representation of object record status.
*
* @return integer
*/
public function toInt():int {
return match($this) {
self::work => 0,
self::home => 1,
self::mobile => 2,
self::work_mobile => 3,
self::fax => 4,
self::fax_work => 5,
};
}
/**
* Returns canonical string representation of object record status.
*
* @return string
*/
public function toString():string {
return match($this) {
self::work => "work",
self::home => "home",
self::mobile => "mobile",
self::work_mobile => "work mobile",
self::fax => "fax",
self::fax_work => "fax work",
};
}
/**
* 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("phone_types", "phone_types", $this->toString());
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->toString();
}
}