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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class Isrc extends AbstractRegexRule
{
protected function pattern(): string
{
return "/^[A-Z]{2}[A-Z0-9]{3}\d{7}$/i";
}

public function isValid(mixed $value): bool
{
$normalized = $this->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;
}
}
1 change: 1 addition & 0 deletions src/lang/de/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/fr/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
75 changes: 75 additions & 0 deletions tests/Rules/IsrcTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

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

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