mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 00:32:16 +02:00
Use PHP ISO Codes in the "CurrencyCode" rule
Since we now have PHP ISO Codes as a dependency[1], it doesn't make
sense to keep dealing with this data ourselves.
[1]: 04b2722d02
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
44e769a271
commit
27d7db5cc6
5
.github/workflows/update-regionals.yaml
vendored
5
.github/workflows/update-regionals.yaml
vendored
|
@ -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
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
# Usage: {script}
|
||||
# Update the list of currency codes
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
declare -r IFS=$'\n'
|
||||
|
||||
declare -r LIST_URL="https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml"
|
||||
declare -r LIST_FILENAME=$(mktemp)
|
||||
|
||||
declare -r RULE_FILENAME=$(dirname "${BASH_SOURCE}")/../library/Rules/CurrencyCode.php
|
||||
declare -r RULE_FILENAME_TEMPORARY=$(mktemp)
|
||||
|
||||
echo "- Downloading list"
|
||||
curl --silent --location "${LIST_URL}" --output "${LIST_FILENAME}"
|
||||
|
||||
declare -r CURRENCY_CODES_COUNT=$(grep "<CcyNtry>" "${LIST_FILENAME}" | wc --lines)
|
||||
|
||||
echo "- Creating temporary file"
|
||||
{
|
||||
sed -n "/^</,/ return \[/p" "${RULE_FILENAME}"
|
||||
for index in $(seq 1 ${CURRENCY_CODES_COUNT}); do
|
||||
declare name=$(xmlstarlet sel --template --value-of "//CcyNtry[${index}]/CcyNm" < "${LIST_FILENAME}")
|
||||
declare code=$(xmlstarlet sel --template --value-of "//CcyNtry[${index}]/Ccy" < "${LIST_FILENAME}")
|
||||
|
||||
if [[ -z "${code}" ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo " '${code}', //" $(sed --regexp-extended 's, +$,,' <<< "${name}")
|
||||
done | sort --unique
|
||||
sed --silent '/^ \]/,/^}/p' "${RULE_FILENAME}"
|
||||
} > "${RULE_FILENAME_TEMPORARY}"
|
||||
|
||||
echo "- Updating content of '$(basename ${RULE_FILENAME})'"
|
||||
mv "${RULE_FILENAME_TEMPORARY}" "${RULE_FILENAME}"
|
||||
|
||||
echo "Finished!"
|
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -76,7 +76,8 @@ interface ChainedValidator extends Validatable
|
|||
/** @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;
|
||||
|
||||
|
|
|
@ -9,202 +9,55 @@ declare(strict_types=1);
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,8 @@ interface StaticValidator
|
|||
/** @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;
|
||||
|
||||
|
|
|
@ -11,36 +11,52 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
#[Group('rule')]
|
||||
#[CoversClass(CurrencyCode::class)]
|
||||
final class CurrencyCodeTest extends RuleTestCase
|
||||
{
|
||||
#[Test]
|
||||
public function itShouldThrowsExceptionWhenInvalidFormat(): void
|
||||
{
|
||||
$this->expectException(InvalidRuleConstructorException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'"whatever" is not a valid set for ISO 4217 (Available: alpha-3, numeric)'
|
||||
);
|
||||
|
||||
// @phpstan-ignore-next-line
|
||||
new CurrencyCode('whatever');
|
||||
}
|
||||
|
||||
/** @return iterable<array{CurrencyCode, mixed}> */
|
||||
public static function providerForValidInput(): iterable
|
||||
{
|
||||
$rule = new CurrencyCode();
|
||||
|
||||
return [
|
||||
[$rule, 'EUR'],
|
||||
[$rule, 'GBP'],
|
||||
[$rule, 'XAU'],
|
||||
[$rule, 'XBA'],
|
||||
[$rule, 'XXX'],
|
||||
[new CurrencyCode(), 'EUR'],
|
||||
[new CurrencyCode('numeric'), '978'],
|
||||
[new CurrencyCode(), 'GBP'],
|
||||
[new CurrencyCode('numeric'), '826'],
|
||||
[new CurrencyCode(), 'XAU'],
|
||||
[new CurrencyCode('numeric'), '959'],
|
||||
[new CurrencyCode(), 'XBA'],
|
||||
[new CurrencyCode('numeric'), '955'],
|
||||
[new CurrencyCode(), 'XXX'],
|
||||
[new CurrencyCode('numeric'), '999'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return iterable<array{CurrencyCode, mixed}> */
|
||||
public static function providerForInvalidInput(): iterable
|
||||
{
|
||||
$rule = new CurrencyCode();
|
||||
|
||||
return [
|
||||
[$rule, ''],
|
||||
[$rule, 'BTC'],
|
||||
[$rule, 'GGP'],
|
||||
[$rule, 'USA'],
|
||||
[new CurrencyCode(), ''],
|
||||
[new CurrencyCode('numeric'), '123'],
|
||||
[new CurrencyCode(), 'BTC'],
|
||||
[new CurrencyCode(), 'GGP'],
|
||||
[new CurrencyCode(), 'USA'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue