nicebooks / isbn
ISBN tools
Installs: 303 170
Dependents: 3
Suggesters: 1
Security: 0
Stars: 27
Watchers: 3
Forks: 3
Open Issues: 1
pkg:composer/nicebooks/isbn
Requires
- php: ^8.1
Requires (Dev)
- ext-dom: *
- brick/varexporter: ~0.5.0
- php-coveralls/php-coveralls: ^2.5.2
- phpunit/phpunit: ^10.3
- vimeo/psalm: 6.2.0
- dev-master
- 0.4.65
- 0.4.64
- 0.4.63
- 0.4.62
- 0.4.61
- 0.4.60
- 0.4.59
- 0.4.58
- 0.4.57
- 0.4.56
- 0.4.55
- 0.4.54
- 0.4.53
- 0.4.52
- 0.4.51
- 0.4.50
- 0.4.49
- 0.4.48
- 0.4.47
- 0.4.46
- 0.4.45
- 0.4.44
- 0.4.43
- 0.4.42
- 0.4.41
- 0.4.40
- 0.4.39
- 0.4.38
- 0.4.37
- 0.4.36
- 0.4.35
- 0.4.34
- 0.4.33
- 0.4.32
- 0.4.31
- 0.4.30
- 0.4.29
- 0.4.28
- 0.4.27
- 0.4.26
- 0.4.25
- 0.4.24
- 0.4.23
- 0.4.22
- 0.4.21
- 0.4.20
- 0.4.19
- 0.4.18
- 0.4.17
- 0.4.16
- 0.4.15
- 0.4.14
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.48
- 0.3.47
- 0.3.46
- 0.3.45
- 0.3.44
- 0.3.43
- 0.3.42
- 0.3.41
- 0.3.40
- 0.3.39
- 0.3.38
- 0.3.37
- 0.3.36
- 0.3.35
- 0.3.34
- 0.3.33
- 0.3.32
- 0.3.31
- 0.3.30
- 0.3.29
- 0.3.28
- 0.3.27
- 0.3.26
- 0.3.25
- 0.3.24
- 0.3.23
- 0.3.22
- 0.3.21
- 0.3.20
- 0.3.19
- 0.3.18
- 0.3.17
- 0.3.16
- 0.3.15
- 0.3.14
- 0.3.13
- 0.3.12
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.93
- 0.2.92
- 0.2.91
- 0.2.90
- 0.2.89
- 0.2.88
- 0.2.87
- 0.2.86
- 0.2.85
- 0.2.84
- 0.2.83
- 0.2.82
- 0.2.81
- 0.2.80
- 0.2.79
- 0.2.78
- 0.2.77
- 0.2.76
- 0.2.75
- 0.2.74
- 0.2.73
- 0.2.72
- 0.2.71
- 0.2.70
- 0.2.69
- 0.2.68
- 0.2.67
- 0.2.66
- 0.2.65
- 0.2.64
- 0.2.63
- 0.2.62
- 0.2.61
- 0.2.60
- 0.2.59
- 0.2.58
- 0.2.57
- 0.2.56
- 0.2.55
- 0.2.54
- 0.2.53
- 0.2.52
- 0.2.51
- 0.2.50
- 0.2.49
- 0.2.48
- 0.2.47
- 0.2.46
- 0.2.45
- 0.2.44
- 0.2.43
- 0.2.42
- 0.2.41
- 0.2.40
- 0.2.39
- 0.2.38
- 0.2.37
- 0.2.36
- 0.2.35
- 0.2.34
- 0.2.33
- 0.2.32
- 0.2.31
- 0.2.30
- 0.2.29
- 0.2.28
- 0.2.27
- 0.2.26
- 0.2.25
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-isbn_subclass
- dev-php81
This package is auto-updated.
Last update: 2025-10-21 11:39:29 UTC
README
This library provides the functionality to validate, format and convert ISBN numbers, that powers the nicebooks.com website. It is released under a permissive MIT open-source license for anyone to use.
ISBN formatting follows the rules defined by the ISBN range file published by ISBN International.
Installation
This library is installable via Composer:
composer require nicebooks/isbn
Requirements
The current version of this library requires PHP 8.1 or higher.
You may use earlier versions of nicebooks/isbn with earlier versions of PHP, but you will only receive ISBN range updates if you use the latest version.
Project status & release process
While this library is still under development, it is well tested and should be stable enough to use in production environments. It is currently in use in production on nicebooks.com.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (updating ISBN ranges,
adding new methods, optimizing existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.4.*.
If you need to upgrade to a newer release cycle, check the release history
for a list of changes introduced by each further 0.x.0 version.
Overview
The IsbnTools class
This class contains all the tools to work with ISBN numbers as plain strings.
use Nicebooks\Isbn\IsbnTools; $tools = new IsbnTools();
Its constructor offers two configurable parameters:
public function __construct(bool $cleanupBeforeValidate = true, bool $validateCheckDigit = true)
$cleanupBeforeValidateremoves any non-alphanumeric ASCII character from the string before validating it.$validateCheckDigitcomputes the checksum of the ISBN number before any operation.
Method summary:
-
isValidIsbn(string $isbn) : boolchecks that the given ISBN is valid.var_export($tools->isValidIsbn('123456789X')); // true var_export($tools->isValidIsbn('9781234567897')); // true
-
isValidIsbn10(string $isbn) : boolchecks that the given ISBN is a valid ISBN-10.var_export($tools->isValidIsbn10('123456789X')); // true
-
isValidIsbn13(string $isbn) : boolchecks that the given ISBN is a valid ISBN-13.var_export($tools->isValidIsbn13('9781234567897')); // true
-
convertIsbn10to13(string $isbn) : stringconverts the given ISBN-10 to an ISBN-13.var_export($tools->convertIsbn10to13('123456789X')); // '9781234567897'
-
convertIsbn13to10(string $isbn) : stringconverts the given ISBN-13 to an ISBN-10.var_export($tools->convertIsbn13to10('9781234567897')); // '123456789X'
-
format(string $isbn) : stringformats the given ISBN by adding hyphens at the proper places.var_export($formatter->format('123456789X')); // '1-234-56789-X' var_export($formatter->format('9781234567897')); // '978-1-234-56789-7'
The Isbn classes
The Isbn class is an abstract, immutable class representing a valid ISBN-10 or ISBN-13.
It is an alternate way to access the functionality provided by IsbnTools, and offers a convenient way to pass an ISBN number around,
guaranteeing both its validity and its integrity.
Isbn has 2 subclasses: Isbn10 and Isbn13, allowing for narrower typing if your application expects only ISBN-10 or only ISBN-13 at some point.
An Isbn instance is obtained with the of() factory method:
use Nicebooks\Isbn\Isbn; $isbn = Isbn::of('123456789X'); // will return an instance of Isbn10 $isbn = Isbn::of('9781234567897'); // will return an instance of Isbn13
You can also use the Isbn10::of() and Isbn13::of() factory methods:
Isbn10::of('123456789X'); // equivalent to Isbn::of('123456789X')->to10(); Isbn13::of('9781234567897'); // equivalent to Isbn::of('9781234567897')->to13();
Method summary:
is10(): boolReturnstruefor an ISBN-10, orfalsefor an ISBN-13.is13(): boolReturnstruefor an ISBN-13, orfalsefor an ISBN-10.isConvertibleTo10(): boolReturnstrueif the ISBN can be converted to an ISBN-10,falseotherwise.to10(): Isbn10Converts the ISBN to an ISBN-10, or throws anIsbnNotConvertibleExceptionto13(): Isbn13Converts the ISBN to an ISBN-13format(): stringReturns the formatted representation of the ISBN.
Exceptions
Exceptions live in the Nicebooks\Isbn\Exception namespace.
InvalidIsbnExceptionis thrown when an invalid ISBN is detectedIsbnNotConvertibleExceptionis thrown when trying to convert an ISBN-13 that does not start with978to an ISBN-10.