Add MDMeasurementType for listing available measurement types

This commit is contained in:
Joshua Ramon Enslin 2023-10-19 03:27:07 +02:00
parent f14d7edc6e
commit 7c7fb58a0d
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 167 additions and 2 deletions

View File

@ -0,0 +1,141 @@
<?PHP
/**
* Represents a measurement type.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Represents a measurement type.
*/
enum MDMeasurementType implements JsonSerializable {
case length;
case height;
case width;
case number_of_pieces;
case weight;
case diameter;
case wall;
case number_of_pages;
case die_axis;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDCopyrightCollective
*/
public static function fromString(string $input):MDCopyrightCollective {
return match($input) {
'length' => self::length,
'height' => self::height,
'width' => self::width,
'number_of_pieces' => self::number_of_pieces,
'weight' => self::weight,
'diameter' => self::diameter,
'wall' => self::wall,
'number_of_pages' => self::number_of_pages,
'die_axis' => self::die_axis,
default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
};
}
/**
* Returns a value of this type based on an integer.
*
* @param integer $input Input to get a value from.
*
* @return MDCopyrightCollective
*/
public static function fromInt(int $input):MDCopyrightCollective {
return match($input) {
1 => self::length,
2 => self::height,
3 => self::width,
4 => self::number_of_pieces,
5 => self::weight,
6 => self::diameter,
7 => self::wall,
8 => self::number_of_pages,
9 => self::die_axis,
default => throw new MDpageParameterNotFromListException("Unknown measurement 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;
}
/**
* Returns the human-readable name of the measurement type.
*
* @return string
*/
public function getName():string {
return match($this) {
self::vg_bildkunst => "VG Bildkunst",
# default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
default => throw new MDpageParameterNotFromListException("To be implemented"),
};
}
/**
* Returns an integer representation of the collective (for storage in DB).
*
* @return integer
*/
public function toInt():int {
return match($this) {
self::length => 1,
self::height => 2,
self::width => 3,
self::number_of_pieces => 4,
self::weight => 5,
self::diameter => 6,
self::wall => 7,
self::number_of_pages => 8,
self::die_axis => 9,
default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
};
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->name;
}
}

View File

@ -87,6 +87,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
'ndp-ikmk' => self::ndp_ikmk, 'ndp-ikmk' => self::ndp_ikmk,
'ndp-ikmk-persons' => self::ndp_ikmk_persons, 'ndp-ikmk-persons' => self::ndp_ikmk_persons,
'nomisma' => self::nomisma, 'nomisma' => self::nomisma,
'nomisma.org' => self::nomisma,
'npg' => self::npg, 'npg' => self::npg,
'oberbegriffsdatei' => self::oberbegriffsdatei, 'oberbegriffsdatei' => self::oberbegriffsdatei,
'orcid' => self::orcid, 'orcid' => self::orcid,
@ -99,6 +100,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
'viaf' => self::viaf, 'viaf' => self::viaf,
'wikidata' => self::wikidata, 'wikidata' => self::wikidata,
'Wikidata' => self::wikidata, 'Wikidata' => self::wikidata,
'www.wikidata.org' => self::wikidata,
'WIKIDATA' => self::wikidata, 'WIKIDATA' => self::wikidata,
'WIKIPEDIA' => self::wikidata, 'WIKIPEDIA' => self::wikidata,
'wikipedia' => self::wikipedia, 'wikipedia' => self::wikipedia,
@ -111,6 +113,28 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
} }
/**
* Attempts to get a repository based on a provided link. This function is rather expensive
* and should be avoided as much as possible.
*
* @param string $input Input to get a value from.
*
* @return MDNodaRepository
*/
public static function fromUrl(string $input):MDNodaRepository {
$cases = self::cases();
foreach ($cases as $case) {
$output = $case->validateId($input);
if ($output !== false) return $case;
}
throw new MDInvalidNodaLink("Failed to get norm data repository based on the provided link");
}
/** /**
* Returns the name as stored in the DB. * Returns the name as stored in the DB.
* *