MDQualityAssessment/src/Checks/PlausiForLegalStatus/MDPlausiLegalCheckReason.php

120 lines
3.1 KiB
PHP
Raw Normal View History

<?PHP
/**
* Describes a reason for a notice on why the provided copyright status of
* an object or its representations is invalid.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a possible status for to-dos.
*/
enum MDPlausiLegalCheckReason implements JsonSerializable {
case creator_dead_for_years;
case creator_recently_or_not_dead;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDPlausiLegalCheckReason
*/
public static function fromString(string $input):MDPlausiLegalCheckReason {
return match($input) {
'creator_dead_for_years' => self::creator_dead_for_years,
'creator_recently_or_not_dead' => self::creator_recently_or_not_dead,
default => throw new MDpageParameterNotFromListException("Unknown reason"),
};
}
/**
* Returns a value of this type based on a integer.
*
* @param integer $input Input to get a value from.
*
* @return MDPlausiLegalCheckReason
*/
public static function fromInt(int $input):MDPlausiLegalCheckReason {
return match($input) {
1 => self::creator_dead_for_years,
2 => self::creator_recently_or_not_dead,
default => throw new MDpageParameterNotFromListException("Unknown reason"),
};
}
/**
* Returns a numeric representation of the type, e.g. for efficient storage in a DB.
*
* @return integer
*/
public function toInt():int {
return match($this) {
self::creator_dead_for_years => 1,
self::creator_recently_or_not_dead => 2,
};
}
/**
* 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;
}
/**
* Returns a translation for the entry.
*
* @param MDTlLoader $tlLoader Translation loader.
*
* @return string
*/
public function getTl(MDTlLoader $tlLoader):string {
$tl = $tlLoader->tl("quality", "quality", "legal_check_reason_" . $this->name);
$toReplaceWith = (string)match($this) {
self::creator_dead_for_years => MDPlausiForLegalStatus::YEARS_AFTER_DEATH_TO_PUBLIC_DOMAIN,
self::creator_recently_or_not_dead => MDPlausiForLegalStatus::YEARS_AFTER_DEATH_TO_PUBLIC_DOMAIN_CERTAIN,
};
if (str_contains($tl, "[placeholder_copyright_expiry]") === false) {
throw new Exception("Error in translating. Language " . $tlLoader->getLang() . " is missing placeholder [placeholder_copyright_expiry].");
}
return str_replace("[placeholder_copyright_expiry]", $toReplaceWith, $tl);
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
}