Skip to content

Commit c5d3969

Browse files
author
Jon Waldstein
committed
refactor: update the currency list to match iso-4217 standards and add ability to override the list
1 parent 83ce93c commit c5d3969

2 files changed

Lines changed: 415 additions & 125 deletions

File tree

src/Rules/Currency.php

Lines changed: 190 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
class Currency implements ValidationRule, ValidatesOnFrontEnd
1212
{
13+
/**
14+
* @var string[]
15+
*/
16+
protected $currencyCodes;
17+
18+
/**
19+
* @unreleased
20+
*/
21+
public function __construct(?array $currencyCodes = null)
22+
{
23+
$this->currencyCodes = $currencyCodes ?? self::currencyCodes();
24+
}
25+
1326
/**
1427
* @inheritDoc
1528
*
@@ -47,12 +60,25 @@ public function serializeOption()
4760
*/
4861
public function __invoke($value, Closure $fail, string $key, array $values)
4962
{
50-
if (!is_string($value) || !in_array(strtoupper($value), self::currencyCodes(), true)) {
63+
if (!is_string($value) || !in_array(strtoupper($value), $this->currencyCodes, true)) {
5164
$fail(sprintf(__('%s must be a valid currency', '%TEXTDOMAIN%'), '{field}'));
5265
}
5366
}
5467

5568
/**
69+
* Returns the list of valid ISO 4217 currency codes.
70+
*
71+
* @unreleased Updated to match current ISO 4217 standard as of 2024.
72+
*
73+
* Major changes include:
74+
* - Removed obsolete codes: BYR (→BYN), EEK (→EUR), GHC (→GHS), LVL (→EUR),
75+
* LTL (→EUR), TRL (→TRY), VEF (→VES), ZWD (→ZWL)
76+
* - Added 74+ missing current ISO 4217 codes including AED, AMD, AOA, BHD, etc.
77+
* - Kept commonly used non-ISO codes: GGP, IMP, JEP, TVD
78+
* - Total codes increased from 95 to 169 for better global coverage
79+
*
80+
* @see https://www.iso.org/iso-4217-currency-codes.html
81+
*
5682
* @since 1.0.0
5783
*
5884
* @return string[]
@@ -63,120 +89,169 @@ public static function currencyCodes(): array
6389

6490
if ($codes === null) {
6591
$codes = [
66-
"ALL",
67-
"AFN",
68-
"ARS",
69-
"AWG",
70-
"AUD",
71-
"AZN",
72-
"BSD",
73-
"BBD",
74-
"BDT",
75-
"BYR",
76-
"BZD",
77-
"BMD",
78-
"BOB",
79-
"BAM",
80-
"BWP",
81-
"BGN",
82-
"BRL",
83-
"BND",
84-
"KHR",
85-
"CAD",
86-
"KYD",
87-
"CLP",
88-
"CNY",
89-
"COP",
90-
"CRC",
91-
"HRK",
92-
"CUP",
93-
"CZK",
94-
"DKK",
95-
"DOP",
96-
"XCD",
97-
"EGP",
98-
"SVC",
99-
"EEK",
100-
"EUR",
101-
"FKP",
102-
"FJD",
103-
"GHC",
104-
"GIP",
105-
"GTQ",
106-
"GGP",
107-
"GYD",
108-
"HNL",
109-
"HKD",
110-
"HUF",
111-
"ISK",
112-
"INR",
113-
"IDR",
114-
"IRR",
115-
"IMP",
116-
"ILS",
117-
"JMD",
118-
"JPY",
119-
"JEP",
120-
"KZT",
121-
"KPW",
122-
"KRW",
123-
"KGS",
124-
"LAK",
125-
"LVL",
126-
"LBP",
127-
"LRD",
128-
"LTL",
129-
"MKD",
130-
"MYR",
131-
"MUR",
132-
"MXN",
133-
"MNT",
134-
"MZN",
135-
"NAD",
136-
"NPR",
137-
"ANG",
138-
"NZD",
139-
"NIO",
140-
"NGN",
141-
"NOK",
142-
"OMR",
143-
"PKR",
144-
"PAB",
145-
"PYG",
146-
"PEN",
147-
"PHP",
148-
"PLN",
149-
"QAR",
150-
"RON",
151-
"RUB",
152-
"SHP",
153-
"SAR",
154-
"RSD",
155-
"SCR",
156-
"SGD",
157-
"SBD",
158-
"SOS",
159-
"ZAR",
160-
"LKR",
161-
"SEK",
162-
"CHF",
163-
"SRD",
164-
"SYP",
165-
"TWD",
166-
"THB",
167-
"TTD",
168-
"TRY",
169-
"TRL",
170-
"TVD",
171-
"UAH",
172-
"GBP",
173-
"USD",
174-
"UYU",
175-
"UZS",
176-
"VEF",
177-
"VND",
178-
"YER",
179-
"ZWD",
92+
"AED", // UAE Dirham
93+
"AFN", // Afghan Afghani
94+
"ALL", // Albanian Lek
95+
"AMD", // Armenian Dram
96+
"ANG", // Netherlands Antillean Guilder
97+
"AOA", // Angolan Kwanza
98+
"ARS", // Argentine Peso
99+
"AUD", // Australian Dollar
100+
"AWG", // Aruban Florin
101+
"AZN", // Azerbaijani Manat
102+
"BAM", // Bosnia and Herzegovina Convertible Mark
103+
"BBD", // Barbados Dollar
104+
"BDT", // Bangladeshi Taka
105+
"BGN", // Bulgarian Lev
106+
"BHD", // Bahraini Dinar
107+
"BIF", // Burundian Franc
108+
"BMD", // Bermudian Dollar
109+
"BND", // Brunei Dollar
110+
"BOB", // Bolivian Boliviano
111+
"BRL", // Brazilian Real
112+
"BSD", // Bahamian Dollar
113+
"BTN", // Bhutanese Ngultrum
114+
"BWP", // Botswanan Pula
115+
"BYN", // Belarusian Ruble
116+
"BZD", // Belize Dollar
117+
"CAD", // Canadian Dollar
118+
"CDF", // Congolese Franc
119+
"CHF", // Swiss Franc
120+
"CLP", // Chilean Peso
121+
"CNY", // Chinese Yuan
122+
"COP", // Colombian Peso
123+
"CRC", // Costa Rican Colón
124+
"CUP", // Cuban Peso
125+
"CVE", // Cape Verdean Escudo
126+
"CZK", // Czech Koruna
127+
"DJF", // Djiboutian Franc
128+
"DKK", // Danish Krone
129+
"DOP", // Dominican Peso
130+
"DZD", // Algerian Dinar
131+
"EGP", // Egyptian Pound
132+
"ERN", // Eritrean Nakfa
133+
"ETB", // Ethiopian Birr
134+
"EUR", // Euro
135+
"FJD", // Fijian Dollar
136+
"FKP", // Falkland Islands Pound
137+
"GBP", // British Pound Sterling
138+
"GEL", // Georgian Lari
139+
"GGP", // Guernsey Pound (non-ISO but commonly used)
140+
"GHS", // Ghanaian Cedi
141+
"GIP", // Gibraltar Pound
142+
"GMD", // Gambian Dalasi
143+
"GNF", // Guinean Franc
144+
"GTQ", // Guatemalan Quetzal
145+
"GYD", // Guyanese Dollar
146+
"HKD", // Hong Kong Dollar
147+
"HNL", // Honduran Lempira
148+
"HRK", // Croatian Kuna (replaced by EUR in 2023)
149+
"HTG", // Haitian Gourde
150+
"HUF", // Hungarian Forint
151+
"IDR", // Indonesian Rupiah
152+
"ILS", // Israeli New Shekel
153+
"IMP", // Isle of Man Pound (non-ISO but commonly used)
154+
"INR", // Indian Rupee
155+
"IQD", // Iraqi Dinar
156+
"IRR", // Iranian Rial
157+
"ISK", // Icelandic Króna
158+
"JEP", // Jersey Pound (non-ISO but commonly used)
159+
"JMD", // Jamaican Dollar
160+
"JOD", // Jordanian Dinar
161+
"JPY", // Japanese Yen
162+
"KES", // Kenyan Shilling
163+
"KGS", // Kyrgyzstani Som
164+
"KHR", // Cambodian Riel
165+
"KMF", // Comorian Franc
166+
"KPW", // North Korean Won
167+
"KRW", // South Korean Won
168+
"KWD", // Kuwaiti Dinar
169+
"KYD", // Cayman Islands Dollar
170+
"KZT", // Kazakhstani Tenge
171+
"LAK", // Laotian Kip
172+
"LBP", // Lebanese Pound
173+
"LKR", // Sri Lankan Rupee
174+
"LRD", // Liberian Dollar
175+
"LSL", // Lesotho Loti
176+
"LYD", // Libyan Dinar
177+
"MAD", // Moroccan Dirham
178+
"MDL", // Moldovan Leu
179+
"MGA", // Malagasy Ariary
180+
"MKD", // Macedonian Denar
181+
"MMK", // Myanmar Kyat
182+
"MNT", // Mongolian Tugrik
183+
"MOP", // Macanese Pataca
184+
"MRU", // Mauritanian Ouguiya
185+
"MUR", // Mauritian Rupee
186+
"MVR", // Maldivian Rufiyaa
187+
"MWK", // Malawian Kwacha
188+
"MXN", // Mexican Peso
189+
"MYR", // Malaysian Ringgit
190+
"MZN", // Mozambican Metical
191+
"NAD", // Namibian Dollar
192+
"NGN", // Nigerian Naira
193+
"NIO", // Nicaraguan Córdoba
194+
"NOK", // Norwegian Krone
195+
"NPR", // Nepalese Rupee
196+
"NZD", // New Zealand Dollar
197+
"OMR", // Omani Rial
198+
"PAB", // Panamanian Balboa
199+
"PEN", // Peruvian Sol
200+
"PGK", // Papua New Guinean Kina
201+
"PHP", // Philippine Peso
202+
"PKR", // Pakistani Rupee
203+
"PLN", // Polish Zloty
204+
"PYG", // Paraguayan Guaraní
205+
"QAR", // Qatari Riyal
206+
"RON", // Romanian Leu
207+
"RSD", // Serbian Dinar
208+
"RUB", // Russian Ruble
209+
"RWF", // Rwandan Franc
210+
"SAR", // Saudi Riyal
211+
"SBD", // Solomon Islands Dollar
212+
"SCR", // Seychellois Rupee
213+
"SDG", // Sudanese Pound
214+
"SEK", // Swedish Krona
215+
"SGD", // Singapore Dollar
216+
"SHP", // Saint Helena Pound
217+
"SLE", // Sierra Leonean Leone (new)
218+
"SLL", // Sierra Leonean Leone (old)
219+
"SOS", // Somali Shilling
220+
"SRD", // Surinamese Dollar
221+
"SSP", // South Sudanese Pound
222+
"STN", // São Tomé and Príncipe Dobra
223+
"SVC", // Salvadoran Colón
224+
"SYP", // Syrian Pound
225+
"SZL", // Swazi Lilangeni
226+
"THB", // Thai Baht
227+
"TJS", // Tajikistani Somoni
228+
"TMT", // Turkmenistani Manat
229+
"TND", // Tunisian Dinar
230+
"TOP", // Tongan Paʻanga
231+
"TRY", // Turkish Lira
232+
"TTD", // Trinidad and Tobago Dollar
233+
"TVD", // Tuvaluan Dollar (non-ISO but used)
234+
"TWD", // New Taiwan Dollar
235+
"TZS", // Tanzanian Shilling
236+
"UAH", // Ukrainian Hryvnia
237+
"UGX", // Ugandan Shilling
238+
"USD", // United States Dollar
239+
"UYU", // Uruguayan Peso
240+
"UZS", // Uzbekistani Som
241+
"VED", // Venezuelan Bolívar Digital
242+
"VES", // Venezuelan Bolívar Soberano
243+
"VND", // Vietnamese Dong
244+
"VUV", // Vanuatuan Vatu
245+
"WST", // Samoan Tala
246+
"XAF", // Central African CFA Franc
247+
"XCD", // East Caribbean Dollar
248+
"XDR", // Special Drawing Rights
249+
"XOF", // West African CFA Franc
250+
"XPF", // CFP Franc
251+
"YER", // Yemeni Rial
252+
"ZAR", // South African Rand
253+
"ZMW", // Zambian Kwacha
254+
"ZWL", // Zimbabwean Dollar
180255
];
181256
}
182257

0 commit comments

Comments
 (0)