Skip to content

Commit

Permalink
Merge branch 'release/2.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Feb 1, 2019
2 parents 0bad3d9 + b88b4c1 commit 8f4a2d2
Show file tree
Hide file tree
Showing 6 changed files with 1,258 additions and 545 deletions.
122 changes: 122 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# biblys/isbn

[![Build Status](https://travis-ci.org/biblys/isbn.svg?branch=master)](https://travis-ci.org/biblys/isbn)

This package can be used to :

- validate an ISBN code
- convert codes between ISBN-10, ISBN-13 and EAN (without hyphens) formats
- calculate the checksum digit
- show the registration agency (country or language)

## Installation

Install with composer:

$ composer require biblys/isbn:~2.0

## Usage

```php
<?php

use Biblys\Isbn\Isbn as Isbn;

// Create an ISBN object from an EAN code
$isbn = new Isbn('9791091146098');

// Check if input is a valid ISBN code
if ($isbn->isValid()) {

// Print the code in ISBN-13 format
echo $isbn->format('ISBN-13');

// Print the code in ISBN-10 format
echo $isbn->format('ISBN-10');

// Print the checksum digit
echo $isbn->getChecksum();

// Print the registration agency
echo $isbn->getAgency();

} else {

// Show validation errors
echo $isbn->getErrors();
}
```

## Test

Run tests with PHPUnit:

$ composer install
$ composer test

## ISBN ranges update

New ISBN ranges may be added from time to time by the
[International ISBN Agency](https://www.isbn-international.org/). 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

## Changelog

### 2.0.7 (2019-02-01)

- Attempts to format an invalid ISBN will now throw an Exception
- Fixed considering ISBNs with an invalid product code as valid
- Updated ISBN ranges

### 2.0.6 (2017-11-22)

- Fixed [#6](https://github.com/biblys/isbn/issues/6): Error when validating ISBN-10 with
X as a checksum digit

### 2.0.5 (2017-11-07)

- Update ISBN ranges

### 2.0.4 (2017-06-29)

- Fixed error when no rule is found (eg. for ISBN 9790706801940)

### 2.0.3 (2016-07-06)

- Added 978-99978 range for Mongolia

### 2.0.2 (2016-04-12)

- Fixed [#3](https://github.com/biblys/isbn/issues/3): Bug in the 978-613 range
- Added a composer script to update ISBN ranges from isbn-international.org

### 2.0.1 (2016-03-01)

- Fixed [#2](https://github.com/biblys/isbn/issues/2):
added LICENSE file and copyright information
- Added Travis configuration file

### 2.0.0 (2016-03-01)

- Revamped library as a Composer package

### 1.1.0 (2015-08-21)

- Fixed [#1](https://github.com/biblys/isbn/issues/1):
ISBN-10 checksum character calculation
- Added phpunit tests
- Updated ISBN XML ranges file

### 1.0.1 (2014-04-21)

- EAN-13 checksum character calculation bug fix

### 1.0.0 (2014-04-19)

- First release
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "biblys/isbn",
"description": "A PHP library to convert and validate ISBN & EAN",
"time": "2017-11-22",
"description": "A PHP library to convert and validate ISBNs & EANs",
"time": "2019-02-01",
"keywords": ["ISBN", "EAN", "Book"],
"license": "MIT",
"homepage": "https://github.com/biblys/isbn",
Expand Down
104 changes: 0 additions & 104 deletions readme.md

This file was deleted.

36 changes: 30 additions & 6 deletions src/Biblys/Isbn/Isbn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Isbn
const ERROR_EMPTY = 'No code provided',
ERROR_INVALID_CHARACTERS = 'Invalid characters in the code',
ERROR_INVALID_LENGTH = 'Code is too short or too long',
ERROR_INVALID_PRODUCT_CODE = 'Product code should be 978 or 979',
ERROR_INVALID_COUNTRY = 'Country code unknown';

private $_product, // GS1 Product Code (978 or 979 for books)
Expand Down Expand Up @@ -101,6 +102,10 @@ public function isValid()
*/
public function format($format = 'EAN')
{
if (!$this->isValid()) {
throw new \Exception('Cannot format invalid ISBN: '.$this->getErrors());
}

$this->calculateChecksum($format);

$A = $this->getProduct();
Expand Down Expand Up @@ -151,8 +156,9 @@ private function removeHyphens($code)
$code = str_replace($replacements,'',$code);

// Check for unwanted characters
if (!is_numeric($code) && !(is_numeric(substr($code, 0, -1)) && strtoupper(substr($code, -1)) == 'X'))
{
if (!is_numeric($code)
&& !(is_numeric(substr($code, 0, -1))
&& strtoupper(substr($code, -1)) == 'X')) {
$this->setValid(false);
$this->addError(self::ERROR_INVALID_CHARACTERS);
}
Expand Down Expand Up @@ -188,13 +194,26 @@ private function removeChecksum($code)
*/
private function removeProductCode($code)
{

$first3 = substr($code,0,3);
if ($first3 == 978 || $first3 == 979) {
$this->setProduct($first3);
$code = substr($code,3);
} else {

// For ISBN-10, product code is always 978
if (strlen($code) == 9) {
$this->setProduct(978);
}

// ISBN-13: check that product code is 978 or 979
elseif ($first3 == 978 || $first3 == 979) {
$this->setProduct($first3);
$code = substr($code, 3);
}

// Product code is Invalid
else {
$this->setValid(false);
$this->addError(self::ERROR_INVALID_PRODUCT_CODE);
}

return $code;
}

Expand All @@ -217,6 +236,11 @@ private function removeCountryCode($code)
}
}

// If product code was not found, cannot proceed
if (empty($rules)) {
return null;
}

// Select the right rule
foreach ($rules as $r)
{
Expand Down
Loading

0 comments on commit 8f4a2d2

Please sign in to comment.