Add enums for handling custom reports in musdb

This commit is contained in:
Joshua Ramon Enslin 2022-12-29 23:16:27 +01:00
parent 0f1458e7d4
commit 830bbd1103
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
3 changed files with 178 additions and 1 deletions

2
l18n

@ -1 +1 @@
Subproject commit 04b1e8d678fbb37cac7d96b167be5822bab2491a
Subproject commit dcd03ce585f08b1793de30c7733dde6655c69595

View File

@ -0,0 +1,78 @@
<?PHP
/**
* Represents a format in which custom reports can be generated.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a format in which custom reports can be generated.
*/
enum MDCustomReportFormat implements MDValueEnumInterface, JsonSerializable {
case csv;
case html;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDCustomReportTarget
*/
public static function fromString(string $input):MDObjectDamageType {
return match($input) {
'csv' => self::csv,
'html' => self::html,
default => throw new MDpageParameterNotFromListException("Unknown custom report format"),
};
}
/**
* 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;
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
/**
* Sanitizes a string for input into a file of the current format.
*
* @param string $input Input string to sanitize.
*
* @return string
*/
public function sanitize(string $input):string {
return match($this) {
self::csv => '"' . strtr($input, ['"' => '\'']) . '"',
self::html => htmlspecialchars($input),
};
}
}

View File

@ -0,0 +1,99 @@
<?PHP
/**
* Represents a type of page for which custom reports can be generated in musdb.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a type of page for which custom reports can be generated in musdb.
*/
enum MDCustomReportTarget implements MDValueEnumInterface, JsonSerializable {
case object_single;
case object_list;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDCustomReportTarget
*/
public static function fromString(string $input):MDObjectDamageType {
return match($input) {
'object_single' => self::object_single,
'object_list' => self::object_list,
default => throw new MDpageParameterNotFromListException("Unknown custom report target"),
};
}
/**
* 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(), "custom_report_targets_set", "custom_report_targets_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(), "custom_report_targets_set", "custom_report_targets_set");
}
/**
* 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("custom_report_targets_set", "custom_report_targets_set", $this->name);
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
}