From 7c7fb58a0daac294ba43b78af14a308875dd2631 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 19 Oct 2023 03:27:07 +0200 Subject: [PATCH] Add MDMeasurementType for listing available measurement types --- src/enums/MDMeasurementType.php | 141 ++++++++++++++++++++++++++++++++ src/enums/MDNodaRepository.php | 28 ++++++- 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 src/enums/MDMeasurementType.php diff --git a/src/enums/MDMeasurementType.php b/src/enums/MDMeasurementType.php new file mode 100644 index 0000000..c6fc819 --- /dev/null +++ b/src/enums/MDMeasurementType.php @@ -0,0 +1,141 @@ + + */ +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 + */ + 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; + + } +} diff --git a/src/enums/MDNodaRepository.php b/src/enums/MDNodaRepository.php index 44cd702..e22eb24 100644 --- a/src/enums/MDNodaRepository.php +++ b/src/enums/MDNodaRepository.php @@ -86,8 +86,9 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { 'ndl' => self::ndl, 'ndp-ikmk' => self::ndp_ikmk, 'ndp-ikmk-persons' => self::ndp_ikmk_persons, - 'nomisma' => self::nomisma, - 'npg' => self::npg, + 'nomisma' => self::nomisma, + 'nomisma.org' => self::nomisma, + 'npg' => self::npg, 'oberbegriffsdatei' => self::oberbegriffsdatei, 'orcid' => self::orcid, 'osm' => self::osm, @@ -99,6 +100,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { 'viaf' => self::viaf, 'wikidata' => self::wikidata, 'Wikidata' => self::wikidata, + 'www.wikidata.org' => self::wikidata, 'WIKIDATA' => self::wikidata, 'WIKIPEDIA' => self::wikidata, '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. *