Skip to content

Commit

Permalink
Use PHP ISO Codes in the "CurrencyCode" rule
Browse files Browse the repository at this point in the history
Since we now have PHP ISO Codes as a dependency[1], it doesn't make
sense to keep dealing with this data ourselves.

[1]: 04b2722

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent 44e769a commit 27d7db5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 248 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/update-regionals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ jobs:
with:
ref: ${{ secrets.LAST_MINOR_VERSION }}

- name: Update currency codes
run: bin/update-currency-codes

- name: Update language codes
- name: Update language codes
run: bin/update-language-codes

- name: Update top level domains
Expand Down
39 changes: 0 additions & 39 deletions bin/update-currency-codes

This file was deleted.

15 changes: 14 additions & 1 deletion docs/rules/CurrencyCode.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# CurrencyCode

- `CurrencyCode()`
- `CurrencyCode("alpha-3"|"numeric" $set)`

Validates an [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217) currency code like GBP or EUR.
Validates an [ISO 4217][] currency code.

**This rule requires [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][] to be installed.**

```php
v::currencyCode()->validate('GBP'); // true
```

This rule supports the two [ISO 4217][] sets:

- `alpha-3`
- `numeric`

## Categorization

- ISO codes
Expand All @@ -17,6 +25,7 @@ v::currencyCode()->validate('GBP'); // true

Version | Description
--------|-------------
3.0.0 | Require [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][]
2.0.0 | Became case-sensitive
1.0.0 | Created

Expand All @@ -25,3 +34,7 @@ See also:

- [CountryCode](CountryCode.md)
- [SubdivisionCode](SubdivisionCode.md)

[ISO 4217]: http://en.wikipedia.org/wiki/ISO_4217
[sokil/php-isocodes]: https://github.com/sokil/php-isocodes
[sokil/php-isocodes-db-only]: https://github.com/sokil/php-isocodes-db-only
3 changes: 2 additions & 1 deletion library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function countable(): ChainedValidator;
/** @param "alpha-2"|"alpha-3"|"numeric" $set */
public function countryCode(string $set = 'alpha-2'): ChainedValidator;

public function currencyCode(): ChainedValidator;
/** @param "alpha-3"|"numeric" $set */
public function currencyCode(string $set = 'alpha-3'): ChainedValidator;

public function cpf(): ChainedValidator;

Expand Down
231 changes: 42 additions & 189 deletions library/Rules/CurrencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,202 +9,55 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Sokil\IsoCodes\Database\Currencies;

use function class_exists;
use function implode;
use function in_array;

#[Template(
'{{name}} must be a valid currency',
'{{name}} must not be a valid currency',
)]
final class CurrencyCode extends AbstractSearcher
final class CurrencyCode extends Standard
{
/**
* @see http://www.currency-iso.org/en/home/tables/table-a1.html
*
* @return string[]
*/
protected function getDataSource(mixed $input = null): array
private readonly Currencies $currencies;

/** @param "alpha-3"|"numeric" $set */
public function __construct(
private readonly string $set = 'alpha-3',
?Currencies $currencies = null
) {
if (!class_exists(Currencies::class)) {
throw new MissingComposerDependencyException(
'CurrencyCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only'
);
}

$availableSets = ['alpha-3', 'numeric'];
if (!in_array($set, $availableSets, true)) {
throw new InvalidRuleConstructorException(
'"%s" is not a valid set for ISO 4217 (Available: %s)',
$set,
implode(', ', $availableSets)
);
}
$this->currencies = $currencies ?? new Currencies();
}

public function evaluate(mixed $input): Result
{
return [
'AED', // UAE Dirham
'AFN', // Afghani
'ALL', // Lek
'AMD', // Armenian Dram
'ANG', // Netherlands Antillean Guilder
'AOA', // Kwanza
'ARS', // Argentine Peso
'AUD', // Australian Dollar
'AWG', // Aruban Florin
'AZN', // Azerbaijan Manat
'BAM', // Convertible Mark
'BBD', // Barbados Dollar
'BDT', // Taka
'BGN', // Bulgarian Lev
'BHD', // Bahraini Dinar
'BIF', // Burundi Franc
'BMD', // Bermudian Dollar
'BND', // Brunei Dollar
'BOB', // Boliviano
'BOV', // Mvdol
'BRL', // Brazilian Real
'BSD', // Bahamian Dollar
'BTN', // Ngultrum
'BWP', // Pula
'BYN', // Belarusian Ruble
'BZD', // Belize Dollar
'CAD', // Canadian Dollar
'CDF', // Congolese Franc
'CHE', // WIR Euro
'CHF', // Swiss Franc
'CHW', // WIR Franc
'CLF', // Unidad de Fomento
'CLP', // Chilean Peso
'CNY', // Yuan Renminbi
'COP', // Colombian Peso
'COU', // Unidad de Valor Real
'CRC', // Costa Rican Colon
'CUC', // Peso Convertible
'CUP', // Cuban Peso
'CVE', // Cabo Verde Escudo
'CZK', // Czech Koruna
'DJF', // Djibouti Franc
'DKK', // Danish Krone
'DOP', // Dominican Peso
'DZD', // Algerian Dinar
'EGP', // Egyptian Pound
'ERN', // Nakfa
'ETB', // Ethiopian Birr
'EUR', // Euro
'FJD', // Fiji Dollar
'FKP', // Falkland Islands Pound
'GBP', // Pound Sterling
'GEL', // Lari
'GHS', // Ghana Cedi
'GIP', // Gibraltar Pound
'GMD', // Dalasi
'GNF', // Guinean Franc
'GTQ', // Quetzal
'GYD', // Guyana Dollar
'HKD', // Hong Kong Dollar
'HNL', // Lempira
'HTG', // Gourde
'HUF', // Forint
'IDR', // Rupiah
'ILS', // New Israeli Sheqel
'INR', // Indian Rupee
'IQD', // Iraqi Dinar
'IRR', // Iranian Rial
'ISK', // Iceland Krona
'JMD', // Jamaican Dollar
'JOD', // Jordanian Dinar
'JPY', // Yen
'KES', // Kenyan Shilling
'KGS', // Som
'KHR', // Riel
'KMF', // Comorian Franc
'KPW', // North Korean Won
'KRW', // Won
'KWD', // Kuwaiti Dinar
'KYD', // Cayman Islands Dollar
'KZT', // Tenge
'LAK', // Lao Kip
'LBP', // Lebanese Pound
'LKR', // Sri Lanka Rupee
'LRD', // Liberian Dollar
'LSL', // Loti
'LYD', // Libyan Dinar
'MAD', // Moroccan Dirham
'MDL', // Moldovan Leu
'MGA', // Malagasy Ariary
'MKD', // Denar
'MMK', // Kyat
'MNT', // Tugrik
'MOP', // Pataca
'MRU', // Ouguiya
'MUR', // Mauritius Rupee
'MVR', // Rufiyaa
'MWK', // Malawi Kwacha
'MXN', // Mexican Peso
'MXV', // Mexican Unidad de Inversion (UDI)
'MYR', // Malaysian Ringgit
'MZN', // Mozambique Metical
'NAD', // Namibia Dollar
'NGN', // Naira
'NIO', // Cordoba Oro
'NOK', // Norwegian Krone
'NPR', // Nepalese Rupee
'NZD', // New Zealand Dollar
'OMR', // Rial Omani
'PAB', // Balboa
'PEN', // Sol
'PGK', // Kina
'PHP', // Philippine Peso
'PKR', // Pakistan Rupee
'PLN', // Zloty
'PYG', // Guarani
'QAR', // Qatari Rial
'RON', // Romanian Leu
'RSD', // Serbian Dinar
'RUB', // Russian Ruble
'RWF', // Rwanda Franc
'SAR', // Saudi Riyal
'SBD', // Solomon Islands Dollar
'SCR', // Seychelles Rupee
'SDG', // Sudanese Pound
'SEK', // Swedish Krona
'SGD', // Singapore Dollar
'SHP', // Saint Helena Pound
'SLE', // Leone
'SLL', // Leone
'SOS', // Somali Shilling
'SRD', // Surinam Dollar
'SSP', // South Sudanese Pound
'STN', // Dobra
'SVC', // El Salvador Colon
'SYP', // Syrian Pound
'SZL', // Lilangeni
'THB', // Baht
'TJS', // Somoni
'TMT', // Turkmenistan New Manat
'TND', // Tunisian Dinar
'TOP', // Pa’anga
'TRY', // Turkish Lira
'TTD', // Trinidad and Tobago Dollar
'TWD', // New Taiwan Dollar
'TZS', // Tanzanian Shilling
'UAH', // Hryvnia
'UGX', // Uganda Shilling
'USD', // US Dollar
'USN', // US Dollar (Next day)
'UYI', // Uruguay Peso en Unidades Indexadas (UI)
'UYU', // Peso Uruguayo
'UYW', // Unidad Previsional
'UZS', // Uzbekistan Sum
'VED', // Bolívar Soberano
'VES', // Bolívar Soberano
'VND', // Dong
'VUV', // Vatu
'WST', // Tala
'XAF', // CFA Franc BEAC
'XAG', // Silver
'XAU', // Gold
'XBA', // Bond Markets Unit European Composite Unit (EURCO)
'XBB', // Bond Markets Unit European Monetary Unit (E.M.U.-6)
'XBC', // Bond Markets Unit European Unit of Account 9 (E.U.A.-9)
'XBD', // Bond Markets Unit European Unit of Account 17 (E.U.A.-17)
'XCD', // East Caribbean Dollar
'XDR', // SDR (Special Drawing Right)
'XOF', // CFA Franc BCEAO
'XPD', // Palladium
'XPF', // CFP Franc
'XPT', // Platinum
'XSU', // Sucre
'XTS', // Codes specifically reserved for testing purposes
'XUA', // ADB Unit of Account
'XXX', // The codes assigned for transactions where no currency is involved
'YER', // Yemeni Rial
'ZAR', // Rand
'ZMW', // Zambian Kwacha
'ZWL', // Zimbabwe Dollar
];
$currency = match ($this->set) {
'alpha-3' => $this->currencies->getByLetterCode($input),
'numeric' => $this->currencies->getByNumericCode($input),
};

return new Result($currency !== null, $input, $this);
}
}
3 changes: 2 additions & 1 deletion library/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static function countable(): ChainedValidator;
/** @param "alpha-2"|"alpha-3"|"numeric" $set */
public static function countryCode(string $set = 'alpha-2'): ChainedValidator;

public static function currencyCode(): ChainedValidator;
/** @param "alpha-3"|"numeric" $set */
public static function currencyCode(string $set = 'alpha-3'): ChainedValidator;

public static function cpf(): ChainedValidator;

Expand Down
Loading

0 comments on commit 27d7db5

Please sign in to comment.