From d22db5bd9586b98e231cd63c892dd910d1f6eaff Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Fri, 27 Sep 2024 17:08:08 +0200 Subject: [PATCH] Add enum to control levels of redaction in controlled vocabularies --- l18n | 2 +- src/enums/MDVocabularyRedactionLevel.php | 174 +++++++++++++++++++++++ 2 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 src/enums/MDVocabularyRedactionLevel.php diff --git a/l18n b/l18n index eb42b7f..7623a03 160000 --- a/l18n +++ b/l18n @@ -1 +1 @@ -Subproject commit eb42b7f0d826d7c8d84ff7003c3138edcd13f131 +Subproject commit 7623a0314d1e0f76f2c758b9facef43ea5ebfa0a diff --git a/src/enums/MDVocabularyRedactionLevel.php b/src/enums/MDVocabularyRedactionLevel.php new file mode 100644 index 0000000..ca92660 --- /dev/null +++ b/src/enums/MDVocabularyRedactionLevel.php @@ -0,0 +1,174 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a type of contribution to an object group. + */ +enum MDVocabularyRedactionLevel implements MDValueEnumInterface, JsonSerializable { + + case checked; + case unchecked; + case questionable; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDVocabularyRedactionLevel + */ + public static function fromString(string $input):MDVocabularyRedactionLevel { + + return match($input) { + "+" => self::checked, + "checked" => self::checked, + + "-" => self::unchecked, + "unchecked" => self::unchecked, + + "" => self::questionable, + "questionable" => self::questionable, + default => throw new MDpageParameterNotFromListException("Unknown vocabulary redaction level"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDVocabularyRedactionLevel + */ + public static function fromInt(int $input):MDVocabularyRedactionLevel { + + return match($input) { + 0 => self::checked, + 1 => self::unchecked, + 2 => self::questionable, + default => throw new MDpageParameterNotFromListException("Unknown vocabulary redaction level"), + }; + + } + + /** + * 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(), "vocabulary_redaction_level", "vocabulary_redaction_level"); + + } + + /** + * 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(), "vocabulary_redaction_level", "vocabulary_redaction_level"); + + } + + /** + * 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 name as noted in DB. + * + * @return string + */ + public function toDbString():string { + + return match($this) { + self::checked => '+', + self::unchecked => '-', + self::questionable => '', + # default => throw new MDpageParameterNotFromListException("Unknown object record status"), + }; + + } + + /** + * Returns integer representation of object record status. + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::checked => 0, + self::unchecked => 1, + self::questionable => 2, + # 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("vocabulary_redaction_level", "vocabulary_redaction_level", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->toDbString(); + + } +}