MDAllowedValueSets/src/enums/MDCustomReportFormat.php

79 lines
1.7 KiB
PHP
Raw Normal View History

<?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 JsonSerializable {
case csv;
case html;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
2022-12-30 15:24:56 +01:00
* @return MDCustomReportFormat
*/
2022-12-30 05:11:41 +01:00
public static function fromString(string $input):MDCustomReportFormat {
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),
};
}
}