diff --git a/README.md b/README.md index e97f475..a08df00 100644 --- a/README.md +++ b/README.md @@ -343,17 +343,17 @@ Optional verification according to the North American check system. Default `fal The field under validation must be a valid country code according to ISO 3166-1. - public Intervention\Validation\Rules\CountryCode::__construct(string $format = Iso3166::ALPHA2, bool $strict = true) + public Intervention\Validation\Rules\CountryCode::__construct(string $format = CountryCode::ALPHA2, bool $strict = true) #### Parameters **format** -The country code has three different format `ALPHA2`, `ALPHA3` or `NUMERIC`. Select the format you want to check. Default `CountryCode::ALPHA2`. +The country code has three different formats `ALPHA2`, `ALPHA3` or `NUMERIC`. Select the format you want to check. Default `CountryCode::ALPHA2`. **strict** -If `strict` is `true`, the language code must be passed in lowercase. Otherwise, the case doesn't matter. +If `strict` is `true`, the language code must be passed in uppercase. Otherwise, the case doesn't matter. ### Language Code (ISO 639-1) @@ -367,6 +367,22 @@ The field under validation must be a valid language code according to ISO ISO 63 If `strict` is `true`, the language code must be passed in lowercase. Otherwise, the case doesn't matter. +### Currency Code (ISO 4217) + +The field under validation must be a valid currency code according to ISO 4217. + + public Intervention\Validation\Rules\CurrencyCode::__construct(string $format = CurrencyCode::ALPHA, bool $strict = true) + +#### Parameters + +**format** + +The currency code has two different formats `ALPHA` or `NUMERIC`. Select the format you want to check. Default `CurrencyCode::ALPHA`. + +**strict** + +If `strict` is `true`, the code must be passed in uppercase. Otherwise, the case doesn't matter. + ## Development & Testing With this package comes a Docker image to build a test suite container. To diff --git a/src/Rules/CurrencyCode.php b/src/Rules/CurrencyCode.php new file mode 100644 index 0000000..b368c90 --- /dev/null +++ b/src/Rules/CurrencyCode.php @@ -0,0 +1,85 @@ + [ + 'AFN', 'EUR', 'ALL', 'DZD', 'USD', 'EUR', 'AOA', 'XCD', 'XCD', 'XAD', 'ARS', 'AMD', 'AWG', 'AUD', 'EUR', + 'AZN', 'BSD', 'BHD', 'BDT', 'BBD', 'BYN', 'EUR', 'BZD', 'XOF', 'BMD', 'INR', 'BTN', 'BOB', 'BOV', 'USD', + 'BAM', 'BWP', 'NOK', 'BRL', 'USD', 'BND', 'EUR', 'XOF', 'BIF', 'CVE', 'KHR', 'XAF', 'CAD', 'KYD', 'XAF', + 'XAF', 'CLP', 'CLF', 'CNY', 'AUD', 'AUD', 'COP', 'COU', 'KMF', 'CDF', 'XAF', 'NZD', 'CRC', 'XOF', 'EUR', + 'CUP', 'XCG', 'EUR', 'CZK', 'DKK', 'DJF', 'XCD', 'DOP', 'USD', 'EGP', 'SVC', 'USD', 'XAF', 'ERN', 'EUR', + 'SZL', 'ETB', 'EUR', 'FKP', 'DKK', 'FJD', 'EUR', 'EUR', 'EUR', 'XPF', 'EUR', 'XAF', 'GMD', 'GEL', 'EUR', + 'GHS', 'GIP', 'EUR', 'DKK', 'XCD', 'EUR', 'USD', 'GTQ', 'GBP', 'GNF', 'XOF', 'GYD', 'HTG', 'USD', 'AUD', + 'EUR', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR', 'XDR', 'IRR', 'IQD', 'EUR', 'GBP', 'ILS', 'EUR', 'JMD', + 'JPY', 'GBP', 'JOD', 'KZT', 'KES', 'AUD', 'KPW', 'KRW', 'KWD', 'KGS', 'LAK', 'EUR', 'LBP', 'LSL', 'ZAR', + 'LRD', 'LYD', 'CHF', 'EUR', 'EUR', 'MOP', 'MGA', 'MWK', 'MYR', 'MVR', 'XOF', 'EUR', 'USD', 'EUR', 'MRU', + 'MUR', 'EUR', 'XUA', 'MXN', 'MXV', 'USD', 'MDL', 'EUR', 'MNT', 'EUR', 'XCD', 'MAD', 'MZN', 'MMK', 'NAD', + 'ZAR', 'AUD', 'NPR', 'EUR', 'XPF', 'NZD', 'NIO', 'XOF', 'NGN', 'NZD', 'AUD', 'MKD', 'USD', 'NOK', 'OMR', + 'PKR', 'USD', 'PAB', 'USD', 'PGK', 'PYG', 'PEN', 'PHP', 'NZD', 'PLN', 'EUR', 'USD', 'QAR', 'EUR', 'RON', + 'RUB', 'RWF', 'EUR', 'SHP', 'XCD', 'XCD', 'EUR', 'EUR', 'XCD', 'WST', 'EUR', 'STN', 'SAR', 'XOF', 'RSD', + 'SCR', 'SLE', 'SGD', 'XCG', 'XSU', 'EUR', 'EUR', 'SBD', 'SOS', 'ZAR', 'SSP', 'EUR', 'LKR', 'SDG', 'SRD', + 'NOK', 'SEK', 'CHF', 'CHE', 'CHW', 'SYP', 'TWD', 'TJS', 'TZS', 'THB', 'USD', 'XOF', 'NZD', 'TOP', 'TTD', + 'TND', 'TRY', 'TMT', 'USD', 'AUD', 'UGX', 'UAH', 'AED', 'GBP', 'USD', 'USD', 'USN', 'UYU', 'UYI', 'UYW', + 'UZS', 'VUV', 'VES', 'VED', 'VND', 'USD', 'USD', 'XPF', 'MAD', 'YER', 'ZMW', 'ZWG', 'XBA', 'XBB', 'XBC', + 'XBD', 'XTS', 'XXX', 'XAU', 'XPD', 'XPT', 'XAG', + ], + self::NUMERIC => [ + '971', '978', '008', '012', '840', '978', '973', '951', '951', '396', '032', '051', '533', '036', '978', + '944', '044', '048', '050', '052', '933', '978', '084', '952', '060', '356', '064', '068', '984', '840', + '977', '072', '578', '986', '840', '096', '978', '952', '108', '132', '116', '950', '124', '136', '950', + '950', '152', '990', '156', '036', '036', '170', '970', '174', '976', '950', '554', '188', '952', '978', + '192', '532', '978', '203', '208', '262', '951', '214', '840', '818', '222', '840', '950', '232', '978', + '748', '230', '978', '238', '208', '242', '978', '978', '978', '953', '978', '950', '270', '981', '978', + '936', '292', '978', '208', '951', '978', '840', '320', '826', '324', '952', '328', '332', '840', '036', + '978', '340', '344', '348', '352', '356', '360', '960', '364', '368', '978', '826', '376', '978', '388', + '392', '826', '400', '398', '404', '036', '408', '410', '414', '417', '418', '978', '422', '426', '710', + '430', '434', '756', '978', '978', '446', '969', '454', '458', '462', '952', '978', '840', '978', '929', + '480', '978', '965', '484', '979', '840', '498', '978', '496', '978', '951', '504', '943', '104', '516', + '710', '036', '524', '978', '953', '554', '558', '952', '566', '554', '036', '807', '840', '578', '512', + '586', '840', '590', '840', '598', '600', '604', '608', '554', '985', '978', '840', '634', '978', '946', + '643', '646', '978', '654', '951', '951', '978', '978', '951', '882', '978', '930', '682', '952', '941', + '690', '925', '702', '532', '994', '978', '978', '090', '706', '710', '728', '978', '144', '938', '968', + '578', '752', '756', '947', '948', '760', '901', '972', '834', '764', '840', '952', '554', '776', '780', + '788', '949', '934', '840', '036', '800', '980', '784', '826', '840', '840', '997', '858', '940', '927', + '860', '548', '928', '926', '704', '840', '840', '953', '504', '886', '967', '924', '955', '956', '957', + '958', '963', '999', '959', '964', '962', '961', + ], + ]; + + /** + * @throws InvalidArgumentException + */ + public function __construct(protected string $format = self::ALPHA, protected bool $strict = true) + { + if (!in_array($this->format, [self::ALPHA, self::NUMERIC])) { + throw new InvalidArgumentException('Invalid format.'); + } + } + + /** + * {@inheritdoc} + * + * @see Rule::isValid() + */ + public function isValid(mixed $value): bool + { + $value = strval($value); + + if ($this->strict === false) { + $value = strtoupper($value); + } + + return in_array($value, self::VALUES[$this->format], strict: true); + } +} diff --git a/src/lang/ar/validation.php b/src/lang/ar/validation.php index 233529c..1aee926 100644 --- a/src/lang/ar/validation.php +++ b/src/lang/ar/validation.php @@ -36,4 +36,5 @@ 'vin' => 'يجب أن يكون :attribute رقم تعريف مركبة صالحًا (VIN).', 'countrycode' => 'يجب أن يكون :attribute رمز بلد صالحًا.', 'languagecode' => 'يجب أن يكون :attribute رمز لغة صالحًا.', + 'currencycode' => 'يجب أن يكون :attribute رمز عملة صالحًا.', ]; diff --git a/src/lang/bn/validation.php b/src/lang/bn/validation.php index 0b961e7..ed4eceb 100644 --- a/src/lang/bn/validation.php +++ b/src/lang/bn/validation.php @@ -36,4 +36,5 @@ 'vin' => 'এই :attribute টি একটি বৈধ যানবাহন সনাক্তকরণ নম্বর (VIN) হতে হবে.', 'countrycode' => 'এই :attribute টি একটি বৈধ দেশ কোড হতে হবে.', 'languagecode' => 'এই :attribute টি একটি বৈধ ভাষা কোড হতে হবে.', + 'currencycode' => 'এই :attribute টি একটি বৈধ মুদ্রা কোড হতে হবে.', ]; diff --git a/src/lang/de/validation.php b/src/lang/de/validation.php index 0a15832..b77e631 100644 --- a/src/lang/de/validation.php +++ b/src/lang/de/validation.php @@ -45,4 +45,5 @@ 'vin' => 'Der Wert :attribute muss eine gültige Fahrzeug-Identifizierungsnummer (VIN) enthalten.', 'countrycode' => 'Der Wert :attribute muss einen gültigen Ländercode enthalten.', 'languagecode' => 'Der Wert :attribute muss einen gültigen Sprachcode enthalten.', + 'currencycode' => 'Der Wert :attribute muss einen gültigen Währungscode enthalten.', ]; diff --git a/src/lang/en/validation.php b/src/lang/en/validation.php index e032ea7..94fe54c 100644 --- a/src/lang/en/validation.php +++ b/src/lang/en/validation.php @@ -44,4 +44,5 @@ 'vin' => 'The :attribute must be a valid Vehicle Identification Number (VIN).', 'countrycode' => 'The :attribute must be a valid country code.', 'languagecode' => 'The :attribute must be a valid language code.', + 'currencycode' => 'The :attribute must be a valid currency code.', ]; diff --git a/src/lang/es/validation.php b/src/lang/es/validation.php index a6551be..09afe14 100644 --- a/src/lang/es/validation.php +++ b/src/lang/es/validation.php @@ -43,4 +43,5 @@ 'vin' => ':attribute debe ser un Número de Identificación de Vehículo (VIN) válido.', 'countrycode' => ':attribute debe ser un código de país válido.', 'languagecode' => ':attribute debe ser un código de idioma válido.', + 'currencycode' => ':attribute debe ser un código de moneda válido.', ]; diff --git a/src/lang/fa/validation.php b/src/lang/fa/validation.php index b334030..af13f98 100644 --- a/src/lang/fa/validation.php +++ b/src/lang/fa/validation.php @@ -36,4 +36,5 @@ 'vin' => 'مقدار :attribute باید یک شماره شناسایی خودرو (VIN) معتبر باشد.', 'countrycode' => 'مقدار :attribute باید یک کد کشور معتبر باشد.', 'languagecode' => 'مقدار :attribute باید یک کد زبان معتبر باشد.', + 'currencycode' => 'مقدار :attribute باید یک کد واحد پول معتبر باشد.', ]; diff --git a/src/lang/fi/validation.php b/src/lang/fi/validation.php index f6868b1..3af9eec 100644 --- a/src/lang/fi/validation.php +++ b/src/lang/fi/validation.php @@ -42,4 +42,5 @@ 'vin' => ':attribute on oltava kelvollinen ajoneuvon tunnistenumero (VIN).', 'countrycode' => ':attribute on oltava kelvollinen maakoodi.', 'languagecode' => ':attribute on oltava kelvollinen kielikoodi.', + 'currencycode' => ':attribute on oltava kelvollinen valuuttakoodi.', ]; diff --git a/src/lang/fr/validation.php b/src/lang/fr/validation.php index a8f62ff..9789f95 100644 --- a/src/lang/fr/validation.php +++ b/src/lang/fr/validation.php @@ -51,4 +51,5 @@ 'vin' => 'Le champ :attribute doit être un numéro d\'identification de véhicule (VIN) valide.', 'countrycode' => 'Le champ :attribute doit être un code de pays valide.', 'languagecode' => 'Le champ :attribute doit être un code de langue valide.', + 'currencycode' => 'Le champ :attribute doit être un code de devise valide.', ]; diff --git a/src/lang/id/validation.php b/src/lang/id/validation.php index 74e2bb7..8070731 100644 --- a/src/lang/id/validation.php +++ b/src/lang/id/validation.php @@ -37,4 +37,5 @@ 'vin' => ':attribute harus berupa Nomor Identifikasi Kendaraan (VIN) yang valid.', 'countrycode' => ':attribute harus berupa kode negara yang valid.', 'languagecode' => ':attribute harus berupa kode bahasa yang valid.', + 'currencycode' => ':attribute harus berupa kode mata uang yang valid.', ]; diff --git a/src/lang/nl/validation.php b/src/lang/nl/validation.php index 046c202..4da2e70 100644 --- a/src/lang/nl/validation.php +++ b/src/lang/nl/validation.php @@ -43,4 +43,5 @@ 'vin' => ':attribute moet een geldig voertuigidentificatienummer (VIN) zijn.', 'countrycode' => ':attribute moet een geldige landcode zijn.', 'languagecode' => ':attribute moet een geldige taalcode zijn.', + 'currencycode' => ':attribute moet een geldige valutacode zijn.', ]; diff --git a/src/lang/uk/validation.php b/src/lang/uk/validation.php index 2dc7656..957b7be 100644 --- a/src/lang/uk/validation.php +++ b/src/lang/uk/validation.php @@ -43,4 +43,5 @@ 'vin' => 'Поле :attribute повинно містити дійсний ідентифікаційний номер транспортного засобу (VIN).', 'countrycode' => 'Поле :attribute повинно містити дійсний код країни.', 'languagecode' => 'Поле :attribute повинно містити дійсний код мови.', + 'currencycode' => 'Поле :attribute повинно містити дійсний код валюти.', ]; diff --git a/tests/Rules/CurrencyCodeTest.php b/tests/Rules/CurrencyCodeTest.php new file mode 100644 index 0000000..dc4759d --- /dev/null +++ b/tests/Rules/CurrencyCodeTest.php @@ -0,0 +1,51 @@ +isValid($value); + $this->assertEquals($result, $valid); + } + + public static function dataProvider(): Generator + { + yield [true, 'EUR', CurrencyCode::ALPHA, true]; + yield [true, 'USD', CurrencyCode::ALPHA, true]; + yield [true, 'CHF', CurrencyCode::ALPHA, true]; + yield [true, 'HNL', CurrencyCode::ALPHA, true]; + yield [true, 'EUR', CurrencyCode::ALPHA, false]; + yield [true, 'USD', CurrencyCode::ALPHA, false]; + yield [true, 'CHF', CurrencyCode::ALPHA, false]; + yield [true, 'HNL', CurrencyCode::ALPHA, false]; + yield [true, 'eur', CurrencyCode::ALPHA, false]; + yield [true, 'usd', CurrencyCode::ALPHA, false]; + yield [true, 'chf', CurrencyCode::ALPHA, false]; + yield [true, 'hnl', CurrencyCode::ALPHA, false]; + yield [false, 'EURO', CurrencyCode::ALPHA, true]; + yield [false, 'US', CurrencyCode::ALPHA, true]; + yield [false, 'CHFR', CurrencyCode::ALPHA, true]; + yield [false, 'AOL', CurrencyCode::ALPHA, true]; + yield [false, 'euro', CurrencyCode::ALPHA, false]; + + yield [true, '400', CurrencyCode::NUMERIC, true]; + yield [true, '344', CurrencyCode::NUMERIC, true]; + yield [true, '600', CurrencyCode::NUMERIC, true]; + yield [false, '1', CurrencyCode::NUMERIC, true]; + yield [false, '100', CurrencyCode::NUMERIC, true]; + yield [false, '666', CurrencyCode::NUMERIC, true]; + yield [false, '1', CurrencyCode::NUMERIC, false]; + yield [false, '100', CurrencyCode::NUMERIC, false]; + yield [false, '666', CurrencyCode::NUMERIC, false]; + } +}