diff --git a/src/Faker/Core/Barcode.php b/src/Faker/Core/Barcode.php index 4ad17e17a5..4e9a6c1e16 100644 --- a/src/Faker/Core/Barcode.php +++ b/src/Faker/Core/Barcode.php @@ -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)); } diff --git a/src/Faker/Extension/BarcodeExtension.php b/src/Faker/Extension/BarcodeExtension.php index 48d322f498..bc3704f66c 100644 --- a/src/Faker/Extension/BarcodeExtension.php +++ b/src/Faker/Extension/BarcodeExtension.php @@ -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; } diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index a2357d6723..2728cda49c 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -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) * diff --git a/test/Faker/Core/BarcodeTest.php b/test/Faker/Core/BarcodeTest.php index 631350027c..aa16ba0280 100644 --- a/test/Faker/Core/BarcodeTest.php +++ b/test/Faker/Core/BarcodeTest.php @@ -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)); } }