Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
85 changes: 85 additions & 0 deletions src/Rules/CurrencyCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRule;
use InvalidArgumentException;

class CurrencyCode extends AbstractRule
{
public const ALPHA = 'alpha';
public const NUMERIC = 'numeric';

private const VALUES = [
self::ALPHA => [
'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);
}
}
1 change: 1 addition & 0 deletions src/lang/ar/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
'vin' => 'يجب أن يكون :attribute رقم تعريف مركبة صالحًا (VIN).',
'countrycode' => 'يجب أن يكون :attribute رمز بلد صالحًا.',
'languagecode' => 'يجب أن يكون :attribute رمز لغة صالحًا.',
'currencycode' => 'يجب أن يكون :attribute رمز عملة صالحًا.',
];
1 change: 1 addition & 0 deletions src/lang/bn/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
'vin' => 'এই :attribute টি একটি বৈধ যানবাহন সনাক্তকরণ নম্বর (VIN) হতে হবে.',
'countrycode' => 'এই :attribute টি একটি বৈধ দেশ কোড হতে হবে.',
'languagecode' => 'এই :attribute টি একটি বৈধ ভাষা কোড হতে হবে.',
'currencycode' => 'এই :attribute টি একটি বৈধ মুদ্রা কোড হতে হবে.',
];
1 change: 1 addition & 0 deletions src/lang/de/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/es/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/fa/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
'vin' => 'مقدار :attribute باید یک شماره شناسایی خودرو (VIN) معتبر باشد.',
'countrycode' => 'مقدار :attribute باید یک کد کشور معتبر باشد.',
'languagecode' => 'مقدار :attribute باید یک کد زبان معتبر باشد.',
'currencycode' => 'مقدار :attribute باید یک کد واحد پول معتبر باشد.',
];
1 change: 1 addition & 0 deletions src/lang/fi/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/fr/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/id/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/nl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
1 change: 1 addition & 0 deletions src/lang/uk/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
'vin' => 'Поле :attribute повинно містити дійсний ідентифікаційний номер транспортного засобу (VIN).',
'countrycode' => 'Поле :attribute повинно містити дійсний код країни.',
'languagecode' => 'Поле :attribute повинно містити дійсний код мови.',
'currencycode' => 'Поле :attribute повинно містити дійсний код валюти.',
];
51 changes: 51 additions & 0 deletions tests/Rules/CurrencyCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use Generator;
use Intervention\Validation\Rules\CurrencyCode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class CurrencyCodeTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidationAlpha2(bool $result, string $value, string $format, bool $strict): void
{
$valid = (new CurrencyCode($format, $strict))->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];
}
}