Add enum representation of currencies (MDCurrency)

This commit is contained in:
jrenslin committed 2026-09-24 12:44:46 +02:00
1 parent 14045869c7
commit a5d8f9978c
5 files changed
+648 -2

No files matched your search

+289
View File
@@ -0,0 +1,289 @@
<?PHP
declare(strict_types = 1);
/**
* Represents a currency.
*/
enum MDCurrency implements MDValueEnumInterface, JsonSerializable {
case none;
case byBYN;
case chCHF;
case ddrMark;
case ddrMDN;
case deDM;
case deRM;
case esReal;
case euEUR;
case frFF;
case huFt;
case huLari;
case huPengo;
case huKorona;
case idIDR;
case nlGulden;
case plZloty;
case ruRUB;
case suSUR;
case uaUAH;
case uaUAK;
case ukGBP;
case usUSD;
/**
* Returns a value of this type based on a string.
*
* @param string $input Input to get a value from.
*
* @return MDCurrency
*/
public static function fromString(string $input):MDCurrency {
return match($input) {
"0",
"",
"none" => self::none,
"1",
"by-BYN" => self::byBYN,
"2",
"ch-CHF" => self::chCHF,
"3",
"ddr-Mark" => self::ddrMark,
"4",
"ddr-MDN" => self::ddrMDN,
"5",
"de-DM" => self::deDM,
"6",
"de-RM" => self::deRM,
"7",
"es-Real" => self::esReal,
"8",
"eu-EUR" => self::euEUR,
"9",
"fr-FF" => self::frFF,
"10",
"hu-Ft" => self::huFt,
"11",
"hu-Lari" => self::huLari,
"12",
"hu-Pengő" => self::huPengo,
"13",
"hu-Korona" => self::huKorona,
"14",
"id-IDR" => self::idIDR,
"15",
"nl-Gulden" => self::nlGulden,
"16",
"pl-Złoty" => self::plZloty,
"17",
"ru-RUB" => self::ruRUB,
"18",
"su-SUR" => self::suSUR,
"19",
"ua-UAH" => self::uaUAH,
"20",
"ua-UAK" => self::uaUAK,
"21",
"uk-GBP" => self::ukGBP,
"22",
"ua-USD" => self::usUSD,
default => throw new MDpageParameterNotFromListException("Unknown currency"),
};
}
/**
* Returns a value of this type based on an integer.
*
* @param integer $input Input to get a value from.
*
* @return MDCurrency
*/
public static function fromInt(int $input):MDCurrency {
return match($input) {
0 => self::none,
1 => self::byBYN,
2 => self::chCHF,
3 => self::ddrMark,
4 => self::ddrMDN,
5 => self::deDM,
6 => self::deRM,
7 => self::esReal,
8 => self::euEUR,
9 => self::frFF,
10 => self::huFt,
11 => self::huLari,
12 => self::huPengo,
13 => self::huKorona,
14 => self::idIDR,
15 => self::nlGulden,
16 => self::plZloty,
17 => self::ruRUB,
18 => self::suSUR,
19 => self::uaUAH,
20 => self::uaUAK,
21 => self::ukGBP,
22 => self::usUSD,
default => throw new MDpageParameterNotFromListException("Unknown currency"),
};
}
/**
* Lists all available names.
*
* @return array<string>
*/
public static function caseNames():array {
$output = [];
$cases = self::cases();
foreach ($cases as $case) {
$output[] = $case->toString();
}
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(), "currencies_set", "currencies_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(), "currencies_set", "currencies_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::none => 0,
self::byBYN => 1,
self::chCHF => 2,
self::ddrMark => 3,
self::ddrMDN => 4,
self::deDM => 5,
self::deRM => 6,
self::esReal => 7,
self::euEUR => 8,
self::frFF => 9,
self::huFt => 10,
self::huLari => 11,
self::huPengo => 12,
self::huKorona => 13,
self::idIDR => 14,
self::nlGulden => 15,
self::plZloty => 16,
self::ruRUB => 17,
self::suSUR => 18,
self::uaUAH => 19,
self::uaUAK => 20,
self::ukGBP => 21,
self::usUSD => 22,
# default => throw new MDpageParameterNotFromListException("Unknown object record status"),
};
}
/**
* Returns string representation.
*
* @return string
*/
public function toString():string {
return match($this) {
self::none => '',
self::byBYN => 'by-BYN',
self::chCHF => 'ch-CHF',
self::ddrMark => 'ddr-Mark',
self::ddrMDN => "ddr-MDN",
self::deDM => "de-DM",
self::deRM => "de-RM",
self::esReal => "es-Real",
self::euEUR => "eu-EUR",
self::frFF => "fr-FF",
self::huFt => "hu-Ft",
self::huLari => "hu-Lari",
self::huPengo => "hu-Pengő",
self::huKorona => "hu-Korona",
self::idIDR => "id-IDR",
self::nlGulden => "nl-Gulden",
self::plZloty => "pl-Złoty",
self::ruRUB => "ru-RUB",
self::suSUR => "su-SUR",
self::uaUAH => "ua-UAH",
self::uaUAK => "ua-UAK",
self::ukGBP => "uk-GBP",
self::usUSD => "ua-USD",
# 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 {
if ($this === self::none) return '';
return $tlLoader->tl("currencies_set", "currencies_set", $this->toString());
}
/**
* Provides the option to serialize as a string during json_encode().
*
* @return string
*/
public function jsonSerialize():string {
return $this->toString();
}
}