biblys / isbn
A PHP library to convert and validate ISBNs
Installs: 248 353
Dependents: 2
Suggesters: 0
Security: 0
Stars: 54
Watchers: 7
Forks: 16
Open Issues: 2
Requires
- php: ^7.2 || ^8.0
Requires (Dev)
- ext-json: *
- ext-simplexml: *
- guzzlehttp/guzzle: ^7.0
- phpunit/phpunit: ^6 || ^7 || ^8 || ^11
- dev-next
- dev-main
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 3.0.0-alpha.3
- 3.0.0-alpha.2
- 3.0.0-alpha.1
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- dev-add-custom-exceptions
- dev-fix-range-update-updating-dependency
- dev-legacy
- dev-update-ranges
This package is auto-updated.
Last update: 2025-01-18 08:29:47 UTC
README
biblys/isbn can be used to:
- validate a string against the ISBN-10, ISBN-13 and EAN-13 formats
- convert an ISBN to ISBN-10, ISBN-13, EAN-13, GTIN-14 and ISBN-A/DOI formats
- parse an ISBN to extract registration agency, publisher code, publication code, checksum, etc.
Installation
- Requirements: PHP 7.2 or above
Install with composer:
composer require biblys/isbn:~3.0
Usage
Convert
Use case: converting an EAN (9782843449499) to an ISBN-13 (978-2-84344-949-9).
<?php use Biblys\Isbn\Isbn; try { $input = "9782843449499"; $isbn13 = Isbn::convertToIsbn13($input); echo "ISBN-13: $isbn13"; // Prints ISBN-13: 978-2-84344-949-9 } catch(Exception $e) { echo "An error occurred while attempting to format ISBN $input: ".$e->getMessage(); }
All formatting methods:
Isbn::convertToIsbn10
Isbn::convertToIsbn13
Isbn::convertToEan13
Isbn::convertToGtin14
Isbn::convertToIsbnA
Validate
Use case: validating an incorrectly formed ISBN-13 (978-2-843-44949-9, should be 978-2-84344-949-9).
<?php use Biblys\Isbn\Isbn; try { $input = "978-2-843-44949-9"; Isbn::validateAsIsbn13($input); echo "ISBN $input is valid!"; } catch(Exception $e) { // Will throw because third hyphen is misplaced echo "ISBN $input is invalid: ".$e->getMessage(); }
All validating methods:
Isbn::validateAsIsbn10
Isbn::validateAsIsbn13
Isbn::validateAsEan13
Isbn::isParsable
Learn more about validating ISBNs
Parse
Use case: extracting the publisher code from an ISBN.
<?php use Biblys\Isbn\Isbn; $input = "9782956420132"; $isbn = Isbn::parse($input); echo $isbn->getRegistrantElement(); // Prints "9564201"
Isbn::parse
returns a ParsedIsbn
object implementing the following methods:
ParsedIsbn->getGs1Element
:: EAN product codeParsedIsbn->getRegistrationGroupElement
: Country, geographical region or language aera codeParsedIsbn->getRegistrantElement
: Publisher (or imprint within a group) codeParsedIsbn->getPublicationElement
: Publication codeParsedIsbn->getCheckDigit
: Checksum used for validation
Development
Using Gitpod
You can start a dev environment by clicking and start hacking in your browser right away!
Locally
If you'd rather set up a local dev environment, you'll need:
- PHP 7.x
- Composer
- (Optional) Docker to run tests and debug against different version of PHP
Clone this repository and run composer install
to get started!
Tests
Run tests with PHPUnit:
composer install composer test
Run tests in a docker container:
composer docker:test
Run tests in a docker container using a specific PHP version:
PHP_VERSION=7.1 composer docker:test
ISBN ranges update
New ISBN ranges may be added from time to time by the International ISBN Agency. Whenever it happens, this library must be updated. If a range update is necessary, please open an issue on GitHub. You can also open a pull request after updating the ranges your self with the following commands:
composer install composer run update-ranges
Or using a docker container:
composer docker:update-ranges
Create a minor release after ranges autoupdate
read "PR_NUMBER?Pull request number: " gh pr merge --auto -dr "$PR_NUMBER" \ && git fetch --tags \ && CURRENT_VERSION=$(git tag --sort=-v:refname | head -n 1) \ && IFS='.' read -r major minor patch <<< "$CURRENT_VERSION" \ && NEW_VERSION="$major.$minor.$((patch + 1))" \ && read "RELEASE_NOTES?Release notes: " \ && gh release create "$NEW_VERSION" --title "$NEW_VERSION" --notes "$RELEASE_NOTES"