diff --git a/README.md b/README.md index 7327168..402ae05 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Require the package via Composer: The Validation library is built to work with the Laravel Framework (>=10). It comes with a service provider, which will be discovered automatically and -registers the validation rules into your installation. The package provides 30 +registers the validation rules into your installation. The package provides 37 additional validation rules including multi language error messages, which can be used like Laravel's own validation rules. @@ -190,6 +190,12 @@ Checks for a valid [International Securities Identification Number](https://en.w public Intervention\Validation\Rules\Isin::__construct() +### International Standard Recording Code (ISRC) + +The field under validation must be a valid [International Standard Recording Code](https://en.wikipedia.org/wiki/International_Standard_Recording_Code) (ISRC). + + public Intervention\Validation\Rules\Isrc::__construct() + ### International Standard Serial Number (ISSN) Checks for a valid [International Standard Serial Number](https://en.wikipedia.org/wiki/International_Standard_Serial_Number) (ISSN). diff --git a/src/Rules/Isrc.php b/src/Rules/Isrc.php new file mode 100644 index 0000000..4031e4d --- /dev/null +++ b/src/Rules/Isrc.php @@ -0,0 +1,38 @@ +normalizeValue((string) $value); + + if ($normalized === null || $this->hasInvalidLength($normalized)) { + return false; + } + + return parent::isValid($normalized); + } + + private function normalizeValue(string $value): ?string + { + $normalized = preg_replace('/[^A-Z0-9]/i', '', $value); + + return $normalized === null ? null : (string) $normalized; + } + + private function hasInvalidLength(string $value): bool + { + return strlen($value) !== 12; + } +} diff --git a/src/lang/de/validation.php b/src/lang/de/validation.php index 03a8879..e5ae636 100644 --- a/src/lang/de/validation.php +++ b/src/lang/de/validation.php @@ -9,6 +9,7 @@ 'hexadecimalcolor' => 'Der Wert :attribute muss einen gültigen hexadezimalen Farbwert enthalten.', 'creditcard' => 'Der Wert :attribute ist keine gültige Kreditkartennummer.', 'isbn' => 'Der Wert :attribute muss eine gültige International Standard Book Number (ISBN) enthalten.', + 'isrc' => 'Der Wert :attribute muss eine gültige International Standard Recording Code (ISRC) enthalten.', 'username' => 'Der Wert :attribute enthält keinen gültigen Benutzernamen.', 'htmlclean' => 'Der Wert :attribute enthält nicht erlaubten HTML Code.', 'domainname' => 'Der Wert :attribute muss einen Domainnamen enthalten.', diff --git a/src/lang/en/validation.php b/src/lang/en/validation.php index 5215432..cab1661 100644 --- a/src/lang/en/validation.php +++ b/src/lang/en/validation.php @@ -9,6 +9,7 @@ 'hexadecimalcolor' => 'The :attribute must be a valid hexadecimal color code.', 'creditcard' => 'The :attribute must be a valid creditcard number.', 'isbn' => ':attribute must be a valid International Standard Book Number (ISBN).', + 'isrc' => 'The :attribute must be a valid International Standard Recording Code (ISRC).', 'username' => 'The value :attribute must be a valid username.', 'htmlclean' => 'The value :attribute contains forbidden HTML code.', 'domainname' => ':attribute must be a well formed domainname.', diff --git a/src/lang/fr/validation.php b/src/lang/fr/validation.php index d1a51f6..76da756 100644 --- a/src/lang/fr/validation.php +++ b/src/lang/fr/validation.php @@ -9,6 +9,7 @@ 'hexadecimalcolor' => 'Le champ :attribute doit être un code couleur hexadécimal valide.', 'creditcard' => 'Le champ :attribute doit être un numéro de carte de crédit valide.', 'isbn' => 'Le champ :attribute doit être un numéro ISBN valide.', + 'isrc' => 'Le champ :attribute doit être un code ISRC (International Standard Recording Code) valide.', 'username' => 'La valeur de :attribute doit être un pseudonyme valide : chaine alphanumérique entre 3' . ' et 20 caractères, débutant obligatoirement par une lettre, acceptant les tirets et tirets bas.', 'htmlclean' => 'La valeur de :attribute ne doit pas contenir de code HTML.', diff --git a/tests/Rules/IsrcTest.php b/tests/Rules/IsrcTest.php new file mode 100644 index 0000000..b9f8bf9 --- /dev/null +++ b/tests/Rules/IsrcTest.php @@ -0,0 +1,75 @@ +isValid($value); + $this->assertEquals($result, $valid); + } + + public static function dataProvider(): Generator + { + yield [true, 'US-UAN-14-00011']; + yield [true, 'USUAN1400011']; + yield [true, 'GB-XXX-20-12345']; + yield [true, 'GBXXX2012345']; + yield [true, 'DE-A1B-99-00001']; + yield [true, 'DEA1B9900001']; + yield [true, 'QM-123-24-00001']; + yield [true, 'QM1232400001']; + yield [true, 'QZ-ABC-25-99999']; + yield [true, 'QZABC2599999']; + yield [true, 'QT-456-21-00123']; + yield [true, 'QT4562100123']; + yield [true, 'FR-789-00-00001']; + yield [true, 'FR7890000001']; + yield [true, 'US UAN 14 00011']; + yield [true, 'us-uan-14-00011']; + yield [true, 'US-uan-14-00011']; + yield [true, 'gb-xxx-20-12345']; + yield [true, 'de-a1b-99-00001']; + yield [true, 'US/UAN/14/00011']; + yield [true, 'US_UAN_14_00011']; + yield [true, 'US.UAN.14.00011']; + + yield [false, 'US-UAN-14-0001']; + yield [false, 'US-UAN-14-000111']; + yield [false, 'USUAN140001']; + yield [false, 'USUAN14000111']; + yield [false, '1S-UAN-14-00011']; + yield [false, 'U1-UAN-14-00011']; + yield [false, 'U-UAN-14-00011']; + yield [false, 'USA-UAN-14-00011']; + yield [false, 'US--14-00011']; + yield [false, 'US-UA-14-00011']; + yield [false, 'US-UANN-14-00011']; + yield [false, 'US-U@N-14-00011']; + yield [false, 'US-UAN-1-00011']; + yield [false, 'US-UAN-123-00011']; + yield [false, 'US-UAN-AB-00011']; + yield [false, 'US-UAN--00011']; + yield [false, 'US-UAN-14-0001']; + yield [false, 'US-UAN-14-000111']; + yield [false, 'US-UAN-14-0001A']; + yield [false, 'US-UAN-14-']; + yield [false, '']; + yield [false, 'foobar']; + yield [false, '123456789012']; + yield [false, 'ABCDEFGHIJKL']; + + yield [true, 'US-UAN-14-00011-']; + yield [true, '-US-UAN-14-00011']; + yield [true, 'US--UAN-14-00011']; + } +}