Add enum for controlling source types

This commit is contained in:
Joshua Ramon Enslin 2024-11-06 16:19:58 +01:00
parent 71e05e7eab
commit 5142a57cfe
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

173
src/MDSourceType.php Normal file
View File

@ -0,0 +1,173 @@
<?PHP
/**
* Represents a type of source.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a type of source.
*/
enum MDSourceType implements MDValueEnumInterface, JsonSerializable {
case article;
case inbook;
case book;
case phdthesis;
case electronical;
case patent;
case unpublished;
case misc;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDSourceType
*/
public static function fromString(string $input):MDSourceType {
return match($input) {
"article" => self::article,
"inbook" => self::inbook,
"book" => self::book,
"phdthesis" => self::phdthesis,
"electronical" => self::electronical,
"patent" => self::patent,
"unpublished" => self::unpublished,
"misc" => self::misc,
default => throw new MDpageParameterNotFromListException("Unknown source type"),
};
}
/**
* Returns a value of this type based on an integer.
*
* @param integer $input Input to get a value from.
*
* @return MDSourceType
*/
public static function fromInt(int $input):MDSourceType {
return match($input) {
0 => self::article,
1 => self::inbook,
2 => self::book,
3 => self::phdthesis,
4 => self::electronical,
5 => self::patent,
6 => self::unpublished,
7 => self::misc,
default => throw new MDpageParameterNotFromListException("Unknown source 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 {
return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "source_type_set", "source_type_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(), "source_type_set", "source_type_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::article => 0,
self::inbook => 1,
self::book => 2,
self::phdthesis => 3,
self::electronical => 4,
self::patent => 5,
self::unpublished => 6,
self::misc => 7,
# 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 {
return $tlLoader->tl("source_type_set", "source_type_set", $this->name);
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
}