From a437f1b57b41dba9955f2d75594bbf6538389122 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sun, 7 May 2023 23:24:00 +0200 Subject: [PATCH] Add MDTagRelationType --- l18n | 2 +- src/enums/MDTagRelationType.php | 170 ++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 src/enums/MDTagRelationType.php diff --git a/l18n b/l18n index 524eadb..64e2ae6 160000 --- a/l18n +++ b/l18n @@ -1 +1 @@ -Subproject commit 524eadbbd27906ae85911b758acd5af0d5534ac7 +Subproject commit 64e2ae642a90aa1ecee9cd17abde3a8cf55b54d9 diff --git a/src/enums/MDTagRelationType.php b/src/enums/MDTagRelationType.php new file mode 100644 index 0000000..a323b93 --- /dev/null +++ b/src/enums/MDTagRelationType.php @@ -0,0 +1,170 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a type of link between tag and object (e.g. the tag being the object + * type, a material used for creating the object, etc.). + */ +enum MDTagRelationType implements MDValueEnumInterface, JsonSerializable { + + case tag; + case object_type; + case material; + case technique; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDTagRelationType + */ + public static function fromString(string $input):MDTagRelationType { + + return match($input) { + 'tag' => self::tag, + 'object_type' => self::object_type, + 'material' => self::material, + 'technique' => self::technique, + default => throw new MDpageParameterNotFromListException("Unknown tag relation type"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param string $input Input to get a value from. + * + * @return MDTagRelationType + */ + public static function fromInt(int $input):MDTagRelationType { + + return match($input) { + 0 => self::tag, + 1 => self::object_type, + 2 => self::material, + 3 => self::technique, + default => throw new MDpageParameterNotFromListException("Unknown tag relation type"), + }; + + } + + /** + * Returns the default status. + * + * @return MDTagRelationType + */ + public static function getDefault():MDTagRelationType { + + return self::tag; + + } + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Lists all available names. + * + * @return array + */ + public static function caseInts():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->toInt(); + } + + return $output; + + } + + /** + * Returns integer representation of tag relations. + * + * @return integer + */ + public function toInt():int { + + return match(self) { + self::tag => 0, + self::object_type => 1, + self::material => 2, + self::technique => 3, + default => throw new MDpageParameterNotFromListException("Unknown tag relation type"), + }; + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "tag_relation_type", "tag_relation_type"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "tag_relation_type", "tag_relation_type"); + + } + + /** + * 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("tag_relation_type", "tag_relation_type", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +}