Files
MDAllowedValueSets/src/enums/MDGender.php
Joshua Ramon Enslin be2da00759 Use ->toString() over ->name in json serialization of MDGender,
MDLicense

This is important for backwards compatibility
2024-07-30 00:51:04 +02:00

177 lines
4.0 KiB
PHP

<?PHP
/**
* Represents a gender.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a gender.
*/
enum MDGender implements MDValueEnumInterface, JsonSerializable {
case none;
case female;
case male;
case other;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDGender
*/
public static function fromString(string $input):MDGender {
return match($input) {
"" => self::none,
"none" => self::none,
"female" => self::female,
"male" => self::male,
"other" => self::other,
default => throw new MDpageParameterNotFromListException("Unknown gender"),
};
}
/**
* Returns a value of this type based on an integer.
*
* @param integer $input Input to get a value from.
*
* @return MDGender
*/
public static function fromInt(int $input):MDGender {
return match($input) {
0 => self::none,
1 => self::female,
2 => self::male,
3 => self::other,
default => throw new MDpageParameterNotFromListException("Unknown gender"),
};
}
/**
* Lists all available names.
*
* @return array<string>
*/
public static function caseNames():array {
$output = [];
$cases = self::cases();
foreach ($cases as $case) {
$output[] = $case->toString();
}
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 {
return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "gender_set", "gender_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(), "gender_set", "gender_set");
}
/**
* 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::none => 0,
self::female => 1,
self::male => 2,
self::other => 3,
# default => throw new MDpageParameterNotFromListException("Unknown object record status"),
};
}
/**
* Returns string representation.
*
* @return string
*/
public function toString():string {
return match($this) {
self::none => '',
self::female => 'female',
self::male => 'male',
self::other => 'other',
# default => throw new MDpageParameterNotFromListException("Unknown object record status"),
};
}
/**
* Returns the name of the current value in translation.
*
* @param MDTlLoader $tlLoader Translation loader.
*
* @return string
*/
public function getTledName(MDTlLoader $tlLoader):string {
if ($this === self::none) return '';
return $tlLoader->tl("gender_set", "gender_set", $this->toString());
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->toString();
}
}