Skip to content

Commit 30ca894

Browse files
authored
feat: add isrc rule (#94)
1 parent 573c1c4 commit 30ca894

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Require the package via Composer:
2121

2222
The Validation library is built to work with the Laravel Framework (>=10). It
2323
comes with a service provider, which will be discovered automatically and
24-
registers the validation rules into your installation. The package provides 30
24+
registers the validation rules into your installation. The package provides 37
2525
additional validation rules including multi language error messages, which can
2626
be used like Laravel's own validation rules.
2727

@@ -190,6 +190,12 @@ Checks for a valid [International Securities Identification Number](https://en.w
190190

191191
public Intervention\Validation\Rules\Isin::__construct()
192192

193+
### International Standard Recording Code (ISRC)
194+
195+
The field under validation must be a valid [International Standard Recording Code](https://en.wikipedia.org/wiki/International_Standard_Recording_Code) (ISRC).
196+
197+
public Intervention\Validation\Rules\Isrc::__construct()
198+
193199
### International Standard Serial Number (ISSN)
194200

195201
Checks for a valid [International Standard Serial Number](https://en.wikipedia.org/wiki/International_Standard_Serial_Number) (ISSN).

src/Rules/Isrc.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Intervention\Validation\Rules;
6+
7+
use Intervention\Validation\AbstractRegexRule;
8+
9+
class Isrc extends AbstractRegexRule
10+
{
11+
protected function pattern(): string
12+
{
13+
return "/^[A-Z]{2}[A-Z0-9]{3}\d{7}$/i";
14+
}
15+
16+
public function isValid(mixed $value): bool
17+
{
18+
$normalized = $this->normalizeValue((string) $value);
19+
20+
if ($normalized === null || $this->hasInvalidLength($normalized)) {
21+
return false;
22+
}
23+
24+
return parent::isValid($normalized);
25+
}
26+
27+
private function normalizeValue(string $value): ?string
28+
{
29+
$normalized = preg_replace('/[^A-Z0-9]/i', '', $value);
30+
31+
return $normalized === null ? null : (string) $normalized;
32+
}
33+
34+
private function hasInvalidLength(string $value): bool
35+
{
36+
return strlen($value) !== 12;
37+
}
38+
}

src/lang/de/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'hexadecimalcolor' => 'Der Wert :attribute muss einen gültigen hexadezimalen Farbwert enthalten.',
1010
'creditcard' => 'Der Wert :attribute ist keine gültige Kreditkartennummer.',
1111
'isbn' => 'Der Wert :attribute muss eine gültige International Standard Book Number (ISBN) enthalten.',
12+
'isrc' => 'Der Wert :attribute muss eine gültige International Standard Recording Code (ISRC) enthalten.',
1213
'username' => 'Der Wert :attribute enthält keinen gültigen Benutzernamen.',
1314
'htmlclean' => 'Der Wert :attribute enthält nicht erlaubten HTML Code.',
1415
'domainname' => 'Der Wert :attribute muss einen Domainnamen enthalten.',

src/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'hexadecimalcolor' => 'The :attribute must be a valid hexadecimal color code.',
1010
'creditcard' => 'The :attribute must be a valid creditcard number.',
1111
'isbn' => ':attribute must be a valid International Standard Book Number (ISBN).',
12+
'isrc' => 'The :attribute must be a valid International Standard Recording Code (ISRC).',
1213
'username' => 'The value :attribute must be a valid username.',
1314
'htmlclean' => 'The value :attribute contains forbidden HTML code.',
1415
'domainname' => ':attribute must be a well formed domainname.',

src/lang/fr/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'hexadecimalcolor' => 'Le champ :attribute doit être un code couleur hexadécimal valide.',
1010
'creditcard' => 'Le champ :attribute doit être un numéro de carte de crédit valide.',
1111
'isbn' => 'Le champ :attribute doit être un numéro ISBN valide.',
12+
'isrc' => 'Le champ :attribute doit être un code ISRC (International Standard Recording Code) valide.',
1213
'username' => 'La valeur de :attribute doit être un pseudonyme valide : chaine alphanumérique entre 3' .
1314
' et 20 caractères, débutant obligatoirement par une lettre, acceptant les tirets et tirets bas.',
1415
'htmlclean' => 'La valeur de :attribute ne doit pas contenir de code HTML.',

tests/Rules/IsrcTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Intervention\Validation\Tests\Rules;
6+
7+
use Generator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Intervention\Validation\Rules\Isrc;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class IsrcTest extends TestCase
13+
{
14+
#[DataProvider('dataProvider')]
15+
public function testValidation(bool $result, string $value): void
16+
{
17+
$valid = (new Isrc())->isValid($value);
18+
$this->assertEquals($result, $valid);
19+
}
20+
21+
public static function dataProvider(): Generator
22+
{
23+
yield [true, 'US-UAN-14-00011'];
24+
yield [true, 'USUAN1400011'];
25+
yield [true, 'GB-XXX-20-12345'];
26+
yield [true, 'GBXXX2012345'];
27+
yield [true, 'DE-A1B-99-00001'];
28+
yield [true, 'DEA1B9900001'];
29+
yield [true, 'QM-123-24-00001'];
30+
yield [true, 'QM1232400001'];
31+
yield [true, 'QZ-ABC-25-99999'];
32+
yield [true, 'QZABC2599999'];
33+
yield [true, 'QT-456-21-00123'];
34+
yield [true, 'QT4562100123'];
35+
yield [true, 'FR-789-00-00001'];
36+
yield [true, 'FR7890000001'];
37+
yield [true, 'US UAN 14 00011'];
38+
yield [true, 'us-uan-14-00011'];
39+
yield [true, 'US-uan-14-00011'];
40+
yield [true, 'gb-xxx-20-12345'];
41+
yield [true, 'de-a1b-99-00001'];
42+
yield [true, 'US/UAN/14/00011'];
43+
yield [true, 'US_UAN_14_00011'];
44+
yield [true, 'US.UAN.14.00011'];
45+
46+
yield [false, 'US-UAN-14-0001'];
47+
yield [false, 'US-UAN-14-000111'];
48+
yield [false, 'USUAN140001'];
49+
yield [false, 'USUAN14000111'];
50+
yield [false, '1S-UAN-14-00011'];
51+
yield [false, 'U1-UAN-14-00011'];
52+
yield [false, 'U-UAN-14-00011'];
53+
yield [false, 'USA-UAN-14-00011'];
54+
yield [false, 'US--14-00011'];
55+
yield [false, 'US-UA-14-00011'];
56+
yield [false, 'US-UANN-14-00011'];
57+
yield [false, 'US-U@N-14-00011'];
58+
yield [false, 'US-UAN-1-00011'];
59+
yield [false, 'US-UAN-123-00011'];
60+
yield [false, 'US-UAN-AB-00011'];
61+
yield [false, 'US-UAN--00011'];
62+
yield [false, 'US-UAN-14-0001'];
63+
yield [false, 'US-UAN-14-000111'];
64+
yield [false, 'US-UAN-14-0001A'];
65+
yield [false, 'US-UAN-14-'];
66+
yield [false, ''];
67+
yield [false, 'foobar'];
68+
yield [false, '123456789012'];
69+
yield [false, 'ABCDEFGHIJKL'];
70+
71+
yield [true, 'US-UAN-14-00011-'];
72+
yield [true, '-US-UAN-14-00011'];
73+
yield [true, 'US--UAN-14-00011'];
74+
}
75+
}

0 commit comments

Comments
 (0)