Add enum to control levels of redaction in controlled vocabularies

This commit is contained in:
Joshua Ramon Enslin 2024-09-27 17:08:08 +02:00
parent f19973d20e
commit d22db5bd95
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 175 additions and 1 deletions

2
l18n

@ -1 +1 @@
Subproject commit eb42b7f0d826d7c8d84ff7003c3138edcd13f131
Subproject commit 7623a0314d1e0f76f2c758b9facef43ea5ebfa0a

View File

@ -0,0 +1,174 @@
<?PHP
/**
* Represents a redaction level for vocabulary entries (checked, unchecked, questionable).
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
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<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(), "vocabulary_redaction_level", "vocabulary_redaction_level");
}
/**
* 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(), "vocabulary_redaction_level", "vocabulary_redaction_level");
}
/**
* 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 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();
}
}