Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/Faker/Core/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ public function isbn10(): string

public function isbn13(): string
{
$code = '97' . $this->numberExtension->numberBetween(8, 9) . Extension\Helper::numerify(str_repeat('#', 9));
$prefix = '97' . $this->numberExtension->numberBetween(8, 9);
$group = $this->numberExtension->numberBetween($prefix === '978' ? 0 : 1, 9) . $this->numberExtension->numberBetween(0, 9);

$code = $prefix . $group . Extension\Helper::numerify(str_repeat('#', 7));

return sprintf('%s%s', $code, Calculator\Ean::checksum($code));
}

public function ismn(): string
{
$code = '9790' . Extension\Helper::numerify(str_repeat('#', 8));

return sprintf('%s%s', $code, Calculator\Ean::checksum($code));
}
Expand Down
11 changes: 10 additions & 1 deletion src/Faker/Extension/BarcodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ public function isbn10(): string;
*
* @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
*
* @example '9790404436093'
* @example '9791404436090'
*/
public function isbn13(): string;

/**
* Get a random ISMN code
*
* @see https://en.wikipedia.org/wiki/International_Standard_Music_Number
*
* @example '9790404436093'
*/
public function ismn(): string;
}
14 changes: 13 additions & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,25 @@ public function isbn10(): string
*
* @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
*
* @example '9790404436093'
* @example '9791404436090'
*/
public function isbn13(): string
{
return $this->ext(Extension\BarcodeExtension::class)->isbn13();
}

/**
* Get a random ISMN code
*
* @see https://en.wikipedia.org/wiki/International_Standard_Music_Number
*
* @example '9790404436093'
*/
public function ismn(): string
{
return $this->ext(Extension\BarcodeExtension::class)->ismn();
}

/**
* Returns a random number between $int1 and $int2 (any order)
*
Expand Down
9 changes: 8 additions & 1 deletion test/Faker/Core/BarcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ public function testIsbn10(): void
public function testIsbn13(): void
{
$code = $this->faker->isbn13();
self::assertMatchesRegularExpression('/^\d{13}$/i', $code);
self::assertMatchesRegularExpression('/^97(8\d{9}|9[1-9]\d{8})\d$/i', $code);
self::assertTrue(Ean::isValid($code));
}

public function testIsmn(): void
{
$code = $this->faker->ismn();
self::assertMatchesRegularExpression('/^9790\d{9}$/i', $code);
self::assertTrue(Ean::isValid($code));
}
}