Apply contribution guidelines to "CurrencyCode" rule

Also makes the rule case-sensitive since that is how ISO 4217
shows the currency codes.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
William Espindola 2018-06-16 11:55:15 -03:00 committed by Henrique Moody
parent 6124176c89
commit a463f8ab2d
No known key found for this signature in database
GPG key ID: 221E9281655813A6
10 changed files with 252 additions and 261 deletions

View file

@ -12,6 +12,7 @@ v::currencyCode()->validate('GBP'); // true
Version | Description
--------|-------------
2.0.0 | Became case-sensitive
1.0.0 | Created
***

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class CurrencyCodeException extends ValidationException
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Justin Hook <justinhook88@yahoo.co.uk>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class CurrencyCodeException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid currency',

View file

@ -15,197 +15,200 @@ namespace Respect\Validation\Rules;
/**
* Validates currency codes in ISO 4217.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Justin Hook <justinhook88@yahoo.co.uk>
* @author Tim Strijdhorst <tstrijdhorst@users.noreply.github.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
class CurrencyCode extends AbstractRule
final class CurrencyCode extends AbstractSearcher
{
/**
* @see http://www.currency-iso.org/en/home/tables/table-a1.html
* @see @see http://www.currency-iso.org/en/home/tables/table-a1.html
*
* @var array
* {@inheritdoc}
*/
private $currencyCodes = [
'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', // Azerbaijanian 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', // Belarussian 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', // Guinea Franc
'GTQ', // Quetzal
'GYD', // Guyana Dollar
'HKD', // Hong Kong Dollar
'HNL', // Lempira
'HRK', // Kuna
'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', // Comoro Franc
'KPW', // North Korean Won
'KRW', // Won
'KWD', // Kuwaiti Dinar
'KYD', // Cayman Islands Dollar
'KZT', // Tenge
'LAK', // 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
'MRO', // Ouguiya
'MUR', // Mauritius Rupee
'MVR', // Rufiyaa
'MWK', // 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', // Nuevo 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
'SLL', // Leone
'SOS', // Somali Shilling
'SRD', // Surinam Dollar
'SSP', // South Sudanese Pound
'STD', // Dobra
'SVC', // El Salvador Colon
'SYP', // Syrian Pound
'SZL', // Lilangeni
'THB', // Baht
'TJS', // Somoni
'TMT', // Turkmenistan New Manat
'TND', // Tunisian Dinar
'TOP', // Paanga
'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 (URUIURUI)
'UYU', // Peso Uruguayo
'UZS', // Uzbekistan Sum
'VEF', // Bolivar
'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
];
public function validate($input): bool
protected function getDataSource(): array
{
return in_array(mb_strtoupper($input), $this->currencyCodes, true);
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', // Azerbaijanian 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', // Belarussian 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', // Guinea Franc
'GTQ', // Quetzal
'GYD', // Guyana Dollar
'HKD', // Hong Kong Dollar
'HNL', // Lempira
'HRK', // Kuna
'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', // Comoro Franc
'KPW', // North Korean Won
'KRW', // Won
'KWD', // Kuwaiti Dinar
'KYD', // Cayman Islands Dollar
'KZT', // Tenge
'LAK', // 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
'MRO', // Ouguiya
'MUR', // Mauritius Rupee
'MVR', // Rufiyaa
'MWK', // 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', // Nuevo 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
'SLL', // Leone
'SOS', // Somali Shilling
'SRD', // Surinam Dollar
'SSP', // South Sudanese Pound
'STD', // Dobra
'SVC', // El Salvador Colon
'SYP', // Syrian Pound
'SZL', // Lilangeni
'THB', // Baht
'TJS', // Somoni
'TMT', // Turkmenistan New Manat
'TND', // Tunisian Dinar
'TOP', // Paanga
'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 (URUIURUI)
'UYU', // Peso Uruguayo
'UZS', // Uzbekistan Sum
'VEF', // Bolivar
'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
];
}
}

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CurrencyCodeException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::currencyCode()->check('batman');
} catch (CurrencyCodeException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::currencyCode())->check('BRL');
} catch (CurrencyCodeException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::currencyCode()->assert('ppz');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::currencyCode())->assert('GBP');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"batman" must be a valid currency
"BRL" must not be a valid currency
- "ppz" must be a valid currency
- "GBP" must not be a valid currency

View file

@ -1,10 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::currencyCode()->assert('usd');
v::currencyCode()->check('BRL');
?>
--EXPECTF--

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CurrencyCodeException;
use Respect\Validation\Validator as v;
try {
v::currencyCode()->check('batman');
} catch (CurrencyCodeException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
"batman" must be a valid currency

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::currencyCode()->assert('ppz');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "ppz" must be a valid currency

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CurrencyCodeException;
use Respect\Validation\Validator as v;
try {
v::not(v::currencyCode())->check('BRL');
} catch (CurrencyCodeException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
"BRL" must not be a valid currency

View file

@ -1,15 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::not(v::currencyCode())->assert('GBP');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "GBP" must not be a valid currency

View file

@ -16,11 +16,19 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\CurrencyCode
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Justin Hook <justinhook88@yahoo.co.uk>
* @author William Espindola <oi@williamespindola.com.br>
*/
class CurrencyCodeTest extends RuleTestCase
final class CurrencyCodeTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new CurrencyCode();
@ -29,11 +37,14 @@ class CurrencyCodeTest extends RuleTestCase
[$rule, 'EUR'],
[$rule, 'GBP'],
[$rule, 'XAU'],
[$rule, 'xba'],
[$rule, 'xxx'],
[$rule, 'XBA'],
[$rule, 'XXX'],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new CurrencyCode();
@ -42,6 +53,7 @@ class CurrencyCodeTest extends RuleTestCase
[$rule, 'BTC'],
[$rule, 'GGP'],
[$rule, 'USA'],
[$rule, 'xxx'],
];
}
}