From 5142a57cfe674976fdb6e20bcbedc6b34207133a Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 6 Nov 2024 16:19:58 +0100 Subject: [PATCH] Add enum for controlling source types --- src/MDSourceType.php | 173 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/MDSourceType.php diff --git a/src/MDSourceType.php b/src/MDSourceType.php new file mode 100644 index 0000000..da3fc8e --- /dev/null +++ b/src/MDSourceType.php @@ -0,0 +1,173 @@ + + */ +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 + */ + 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 + */ + 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 + */ + 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 + */ + 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; + + } +}