190 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			190 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?PHP
 | 
						|
/**
 | 
						|
 * Represents a type of source.
 | 
						|
 *
 | 
						|
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 | 
						|
 */
 | 
						|
declare(strict_types = 1);
 | 
						|
 | 
						|
/**
 | 
						|
 * Represents a type of source.
 | 
						|
 */
 | 
						|
enum MDSourceType implements MDValueEnumInterface, JsonSerializable {
 | 
						|
 | 
						|
    case article;
 | 
						|
    case inbook;
 | 
						|
    case book;
 | 
						|
    case phdthesis;
 | 
						|
    case electronical;
 | 
						|
    case patent;
 | 
						|
    case unpublished;
 | 
						|
    case misc;
 | 
						|
    case periodical;
 | 
						|
    case booklet;
 | 
						|
    case proceedings;
 | 
						|
    case inproceedings;
 | 
						|
 | 
						|
    /**
 | 
						|
     * Returns a value of this type based on a string.
 | 
						|
     *
 | 
						|
     * @param string $input Input to get a value from.
 | 
						|
     *
 | 
						|
     * @return MDSourceType
 | 
						|
     */
 | 
						|
    public static function fromString(string $input):MDSourceType {
 | 
						|
 | 
						|
        return match($input) {
 | 
						|
            "article" => self::article,
 | 
						|
            "inbook" => self::inbook,
 | 
						|
            "book" => self::book,
 | 
						|
            "phdthesis" => self::phdthesis,
 | 
						|
            "electronical" => self::electronical,
 | 
						|
            "patent" => self::patent,
 | 
						|
            "unpublished" => self::unpublished,
 | 
						|
            "misc" => self::misc,
 | 
						|
            "periodical" => self::periodical,
 | 
						|
            "booklet" => self::booklet,
 | 
						|
            "proceedings" => self::proceedings,
 | 
						|
            "inproceedings" => self::inproceedings,
 | 
						|
            default => throw new MDpageParameterNotFromListException("Unknown source type"),
 | 
						|
        };
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Returns a value of this type based on an integer.
 | 
						|
     *
 | 
						|
     * @param integer $input Input to get a value from.
 | 
						|
     *
 | 
						|
     * @return MDSourceType
 | 
						|
     */
 | 
						|
    public static function fromInt(int $input):MDSourceType {
 | 
						|
 | 
						|
        return match($input) {
 | 
						|
            0 => self::article,
 | 
						|
            1 => self::inbook,
 | 
						|
            2 => self::book,
 | 
						|
            3 => self::phdthesis,
 | 
						|
            4 => self::electronical,
 | 
						|
            5 => self::patent,
 | 
						|
            6 => self::unpublished,
 | 
						|
            7 => self::misc,
 | 
						|
            8 => self::periodical,
 | 
						|
            9 => self::booklet,
 | 
						|
            10 => self::proceedings,
 | 
						|
            11 => self::inproceedings,
 | 
						|
            default => throw new MDpageParameterNotFromListException("Unknown source type"),
 | 
						|
        };
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 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(), "source_type_set", "source_type_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(), "source_type_set", "source_type_set");
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 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 integer representation of object record status.
 | 
						|
     *
 | 
						|
     * @return integer
 | 
						|
     */
 | 
						|
    public function toInt():int {
 | 
						|
 | 
						|
        return match($this) {
 | 
						|
            self::article => 0,
 | 
						|
            self::inbook => 1,
 | 
						|
            self::book => 2,
 | 
						|
            self::phdthesis => 3,
 | 
						|
            self::electronical => 4,
 | 
						|
            self::patent => 5,
 | 
						|
            self::unpublished => 6,
 | 
						|
            self::misc => 7,
 | 
						|
            self::periodical => 8,
 | 
						|
            self::booklet => 9,
 | 
						|
            self::proceedings => 10,
 | 
						|
            self::inproceedings => 11,
 | 
						|
            # 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("source_type_set", "source_type_set", $this->name);
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Provides the option to serialize as a string during json_encode().
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function jsonSerialize():string {
 | 
						|
 | 
						|
        return $this->name;
 | 
						|
 | 
						|
    }
 | 
						|
}
 |