Abstracts
1 year ago
Boolean.php
1 year ago
Currency.php
1 year ago
DateTime.php
1 year ago
Email.php
1 year ago
Exclude.php
1 year ago
ExcludeIf.php
1 year ago
ExcludeUnless.php
1 year ago
In.php
1 year ago
InStrict.php
1 year ago
Integer.php
1 year ago
Max.php
1 year ago
Min.php
1 year ago
Nullable.php
1 year ago
NullableIf.php
1 year ago
NullableUnless.php
1 year ago
Numeric.php
1 year ago
Optional.php
1 year ago
OptionalIf.php
1 year ago
OptionalUnless.php
1 year ago
Required.php
1 year ago
Size.php
1 year ago
Currency.php
249 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\Vendors\StellarWP\Validation\Rules; |
| 6 | |
| 7 | use Closure; |
| 8 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 9 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 10 | |
| 11 | class Currency implements ValidationRule, ValidatesOnFrontEnd |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @inheritDoc |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | public static function id(): string |
| 20 | { |
| 21 | return 'currency'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @inheritDoc |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | public static function fromString(?string $options = null): ValidationRule |
| 30 | { |
| 31 | return new self(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function serializeOption() |
| 40 | { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @inheritDoc |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 50 | { |
| 51 | if (!is_string($value) || !in_array(strtoupper($value), self::currencyCodes(), true)) { |
| 52 | $fail(sprintf(__('%s must be a valid currency', 'give'), '{field}')); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the list of valid ISO 4217 currency codes. |
| 58 | * |
| 59 | * @unreleased Updated to match current ISO 4217 standard as of 2024. |
| 60 | * |
| 61 | * Major changes include: |
| 62 | * - Removed obsolete codes: BYR (→BYN), EEK (→EUR), GHC (→GHS), LVL (→EUR), |
| 63 | * LTL (→EUR), TRL (→TRY), VEF (→VES), ZWD (→ZWL) |
| 64 | * - Added 74+ missing current ISO 4217 codes including AED, AMD, AOA, BHD, etc. |
| 65 | * - Kept commonly used non-ISO codes: GGP, IMP, JEP, TVD |
| 66 | * - Total codes increased from 95 to 169 for better global coverage |
| 67 | * |
| 68 | * @see https://www.iso.org/iso-4217-currency-codes.html |
| 69 | * |
| 70 | * @since 1.0.0 |
| 71 | * |
| 72 | * @return string[] |
| 73 | */ |
| 74 | public static function currencyCodes(): array |
| 75 | { |
| 76 | static $codes = null; |
| 77 | |
| 78 | if ($codes === null) { |
| 79 | $codes = [ |
| 80 | "AED", // UAE Dirham |
| 81 | "AFN", // Afghan Afghani |
| 82 | "ALL", // Albanian Lek |
| 83 | "AMD", // Armenian Dram |
| 84 | "ANG", // Netherlands Antillean Guilder |
| 85 | "AOA", // Angolan Kwanza |
| 86 | "ARS", // Argentine Peso |
| 87 | "AUD", // Australian Dollar |
| 88 | "AWG", // Aruban Florin |
| 89 | "AZN", // Azerbaijani Manat |
| 90 | "BAM", // Bosnia and Herzegovina Convertible Mark |
| 91 | "BBD", // Barbados Dollar |
| 92 | "BDT", // Bangladeshi Taka |
| 93 | "BGN", // Bulgarian Lev |
| 94 | "BHD", // Bahraini Dinar |
| 95 | "BIF", // Burundian Franc |
| 96 | "BMD", // Bermudian Dollar |
| 97 | "BND", // Brunei Dollar |
| 98 | "BOB", // Bolivian Boliviano |
| 99 | "BRL", // Brazilian Real |
| 100 | "BSD", // Bahamian Dollar |
| 101 | "BTN", // Bhutanese Ngultrum |
| 102 | "BWP", // Botswanan Pula |
| 103 | "BYN", // Belarusian Ruble |
| 104 | "BZD", // Belize Dollar |
| 105 | "CAD", // Canadian Dollar |
| 106 | "CDF", // Congolese Franc |
| 107 | "CHF", // Swiss Franc |
| 108 | "CLP", // Chilean Peso |
| 109 | "CNY", // Chinese Yuan |
| 110 | "COP", // Colombian Peso |
| 111 | "CRC", // Costa Rican Colón |
| 112 | "CUP", // Cuban Peso |
| 113 | "CVE", // Cape Verdean Escudo |
| 114 | "CZK", // Czech Koruna |
| 115 | "DJF", // Djiboutian Franc |
| 116 | "DKK", // Danish Krone |
| 117 | "DOP", // Dominican Peso |
| 118 | "DZD", // Algerian Dinar |
| 119 | "EGP", // Egyptian Pound |
| 120 | "ERN", // Eritrean Nakfa |
| 121 | "ETB", // Ethiopian Birr |
| 122 | "EUR", // Euro |
| 123 | "FJD", // Fijian Dollar |
| 124 | "FKP", // Falkland Islands Pound |
| 125 | "GBP", // British Pound Sterling |
| 126 | "GEL", // Georgian Lari |
| 127 | "GGP", // Guernsey Pound (non-ISO but commonly used) |
| 128 | "GHS", // Ghanaian Cedi |
| 129 | "GIP", // Gibraltar Pound |
| 130 | "GMD", // Gambian Dalasi |
| 131 | "GNF", // Guinean Franc |
| 132 | "GTQ", // Guatemalan Quetzal |
| 133 | "GYD", // Guyanese Dollar |
| 134 | "HKD", // Hong Kong Dollar |
| 135 | "HNL", // Honduran Lempira |
| 136 | "HRK", // Croatian Kuna (replaced by EUR in 2023) |
| 137 | "HTG", // Haitian Gourde |
| 138 | "HUF", // Hungarian Forint |
| 139 | "IDR", // Indonesian Rupiah |
| 140 | "ILS", // Israeli New Shekel |
| 141 | "IMP", // Isle of Man Pound (non-ISO but commonly used) |
| 142 | "INR", // Indian Rupee |
| 143 | "IQD", // Iraqi Dinar |
| 144 | "IRR", // Iranian Rial |
| 145 | "ISK", // Icelandic Króna |
| 146 | "JEP", // Jersey Pound (non-ISO but commonly used) |
| 147 | "JMD", // Jamaican Dollar |
| 148 | "JOD", // Jordanian Dinar |
| 149 | "JPY", // Japanese Yen |
| 150 | "KES", // Kenyan Shilling |
| 151 | "KGS", // Kyrgyzstani Som |
| 152 | "KHR", // Cambodian Riel |
| 153 | "KMF", // Comorian Franc |
| 154 | "KPW", // North Korean Won |
| 155 | "KRW", // South Korean Won |
| 156 | "KWD", // Kuwaiti Dinar |
| 157 | "KYD", // Cayman Islands Dollar |
| 158 | "KZT", // Kazakhstani Tenge |
| 159 | "LAK", // Laotian Kip |
| 160 | "LBP", // Lebanese Pound |
| 161 | "LKR", // Sri Lankan Rupee |
| 162 | "LRD", // Liberian Dollar |
| 163 | "LSL", // Lesotho Loti |
| 164 | "LYD", // Libyan Dinar |
| 165 | "MAD", // Moroccan Dirham |
| 166 | "MDL", // Moldovan Leu |
| 167 | "MGA", // Malagasy Ariary |
| 168 | "MKD", // Macedonian Denar |
| 169 | "MMK", // Myanmar Kyat |
| 170 | "MNT", // Mongolian Tugrik |
| 171 | "MOP", // Macanese Pataca |
| 172 | "MRU", // Mauritanian Ouguiya |
| 173 | "MUR", // Mauritian Rupee |
| 174 | "MVR", // Maldivian Rufiyaa |
| 175 | "MWK", // Malawian Kwacha |
| 176 | "MXN", // Mexican Peso |
| 177 | "MYR", // Malaysian Ringgit |
| 178 | "MZN", // Mozambican Metical |
| 179 | "NAD", // Namibian Dollar |
| 180 | "NGN", // Nigerian Naira |
| 181 | "NIO", // Nicaraguan Córdoba |
| 182 | "NOK", // Norwegian Krone |
| 183 | "NPR", // Nepalese Rupee |
| 184 | "NZD", // New Zealand Dollar |
| 185 | "OMR", // Omani Rial |
| 186 | "PAB", // Panamanian Balboa |
| 187 | "PEN", // Peruvian Sol |
| 188 | "PGK", // Papua New Guinean Kina |
| 189 | "PHP", // Philippine Peso |
| 190 | "PKR", // Pakistani Rupee |
| 191 | "PLN", // Polish Zloty |
| 192 | "PYG", // Paraguayan Guaraní |
| 193 | "QAR", // Qatari Riyal |
| 194 | "RON", // Romanian Leu |
| 195 | "RSD", // Serbian Dinar |
| 196 | "RUB", // Russian Ruble |
| 197 | "RWF", // Rwandan Franc |
| 198 | "SAR", // Saudi Riyal |
| 199 | "SBD", // Solomon Islands Dollar |
| 200 | "SCR", // Seychellois Rupee |
| 201 | "SDG", // Sudanese Pound |
| 202 | "SEK", // Swedish Krona |
| 203 | "SGD", // Singapore Dollar |
| 204 | "SHP", // Saint Helena Pound |
| 205 | "SLE", // Sierra Leonean Leone (new) |
| 206 | "SLL", // Sierra Leonean Leone (old) |
| 207 | "SOS", // Somali Shilling |
| 208 | "SRD", // Surinamese Dollar |
| 209 | "SSP", // South Sudanese Pound |
| 210 | "STN", // São Tomé and Príncipe Dobra |
| 211 | "SVC", // Salvadoran Colón |
| 212 | "SYP", // Syrian Pound |
| 213 | "SZL", // Swazi Lilangeni |
| 214 | "THB", // Thai Baht |
| 215 | "TJS", // Tajikistani Somoni |
| 216 | "TMT", // Turkmenistani Manat |
| 217 | "TND", // Tunisian Dinar |
| 218 | "TOP", // Tongan Paʻanga |
| 219 | "TRY", // Turkish Lira |
| 220 | "TTD", // Trinidad and Tobago Dollar |
| 221 | "TVD", // Tuvaluan Dollar (non-ISO but used) |
| 222 | "TWD", // New Taiwan Dollar |
| 223 | "TZS", // Tanzanian Shilling |
| 224 | "UAH", // Ukrainian Hryvnia |
| 225 | "UGX", // Ugandan Shilling |
| 226 | "USD", // United States Dollar |
| 227 | "UYU", // Uruguayan Peso |
| 228 | "UZS", // Uzbekistani Som |
| 229 | "VED", // Venezuelan Bolívar Digital |
| 230 | "VES", // Venezuelan Bolívar Soberano |
| 231 | "VND", // Vietnamese Dong |
| 232 | "VUV", // Vanuatuan Vatu |
| 233 | "WST", // Samoan Tala |
| 234 | "XAF", // Central African CFA Franc |
| 235 | "XCD", // East Caribbean Dollar |
| 236 | "XDR", // Special Drawing Rights |
| 237 | "XOF", // West African CFA Franc |
| 238 | "XPF", // CFP Franc |
| 239 | "YER", // Yemeni Rial |
| 240 | "ZAR", // South African Rand |
| 241 | "ZMW", // Zambian Kwacha |
| 242 | "ZWL", // Zimbabwean Dollar |
| 243 | ]; |
| 244 | } |
| 245 | |
| 246 | return $codes; |
| 247 | } |
| 248 | } |
| 249 |