Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to non-static API #3

Merged
merged 4 commits into from
Mar 2, 2014
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor/
67 changes: 34 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# ISBN PHP library [![Build Status](https://travis-ci.org/Fale/isbn.png?branch=master)](https://travis-ci.org/Fale/isbn) #
This library is develop to provide all tools needed to handle ISBN (both ISBN-10 and ISBN-13) codes to PHP developers.

## Initialization ##
$isbn = new Isbn\Isbn();

## Check ##
This function allows you to verify if an ISBN code is an ISBN-10 or ISBN-13. This does not verifies if the ISBN code is valid. To check if the ISBN code is valid, you can use the `Validation` class.
Examples:

Isbn\Check::is10('888183718'); // Will return false
Isbn\Check::is13('9788889527191'); // Will return true
Isbn\Check::is13('978888952719'); // Will return false
Isbn\Check::identify('8881837188'); // Will return 10
Isbn\Check::identify('888183718'); // Will return false
Isbn\Check::identify('9788889527191'); // Will return 13
Isbn\Check::identify('978888952719'); // Will return false
$isbn->check->is10('888183718'); // Will return false
$isbn->check->is13('9788889527191'); // Will return true
$isbn->check->is13('978888952719'); // Will return false
$isbn->check->identify('8881837188'); // Will return 10
$isbn->check->identify('888183718'); // Will return false
$isbn->check->identify('9788889527191'); // Will return 13
$isbn->check->identify('978888952719'); // Will return false

## Validation ##
This class allows you to validate ISBN-10 and ISBN-13.
Examples:

Isbn\Validation::isbn('8881837188'); // Will return true
Isbn\Validation::isbn('8881837187'); // Will return false
Isbn\Validation::isbn('9788889527191'); // Will return true
Isbn\Validation::isbn('9788889527190'); // Will return false
Isbn\Validation::isbn10('8881837188'); // Will return true
Isbn\Validation::isbn10('8881837187'); // Will return false
Isbn\Validation::isbn13('9788889527191'); // Will return true
Isbn\Validation::isbn13('9788889527190'); // Will return false
$isbn->validation->isbn('8881837188'); // Will return true
$isbn->validation->isbn('8881837187'); // Will return false
$isbn->validation->isbn('9788889527191'); // Will return true
$isbn->validation->isbn('9788889527190'); // Will return false
$isbn->validation->isbn10('8881837188'); // Will return true
$isbn->validation->isbn10('8881837187'); // Will return false
$isbn->validation->isbn13('9788889527191'); // Will return true
$isbn->validation->isbn13('9788889527190'); // Will return false

## Hyphens ##
This class provides simple functions to work with hyphens.
Expand All @@ -33,44 +36,42 @@ This class provides simple functions to work with hyphens.
This function allows you to put correct hyphens in ISBN-10 and ISBN-13.
Examples:

$hyphens = new Isbn\Hyphens('9791090636071');
echo $hyphens->addHyphens(); // Will return 979-10-90636-07-1
echo $isbn->hyphens->addHyphens('9791090636071'); // Will return 979-10-90636-07-1

$hyphens = new Isbn\Hyphens('9791090636071');
echo $hyphens->addHyphens(' '); // Will return 979 10 90636 07 1
echo $hyphens->addHyphens('9791090636071', ' '); // Will return 979 10 90636 07 1

### Remove Hyphens ###
This function allows you to remove hyphens from the ISBN-10 and ISBN-13.
Examples:

Isbn\Hyphens::removeHyphens('85 359 0277 5'); // Will return 8535902775
Isbn\Hyphens::removeHyphens('0-943396-04-2'); // Will return 0943396042
Isbn\Hyphens::removeHyphens('978 988 00 3827 3'); // Will return 9789880038273
Isbn\Hyphens::removeHyphens('979-10-90636-07-1'); // Will return 9791090636071
$isbn->hyphens->removeHyphens('85 359 0277 5'); // Will return 8535902775
$isbn->hyphens->removeHyphens('0-943396-04-2'); // Will return 0943396042
$isbn->hyphens->removeHyphens('978 988 00 3827 3'); // Will return 9789880038273
$isbn->hyphens->removeHyphens('979-10-90636-07-1'); // Will return 9791090636071

### Fix Hyphens ###
This function allows you to fix hyphens in ISBN-10 and ISBN-13

Isbn\Hyphens::fixHyphens('85 35902 77 5', ' '); // Will return 85 359 0277 5
Isbn\Hyphens::fixHyphens('0 943 3960 42'); // Will return 0-943396-04-2
Isbn\Hyphens::fixHyphens('978 988 003827 3', ' '); // Will return 978 988 00 3827 3
Isbn\Hyphens::fixHyphens('979-10906-36-07-1'); // Will return 979-10-90636-07-1
$isbn->hyphens->fixHyphens('85 35902 77 5', ' '); // Will return 85 359 0277 5
$isbn->hyphens->fixHyphens('0 943 3960 42'); // Will return 0-943396-04-2
$isbn->hyphens->fixHyphens('978 988 003827 3', ' '); // Will return 978 988 00 3827 3
$isbn->hyphens->fixHyphens('979-10906-36-07-1'); // Will return 979-10-90636-07-1

## CheckDigit ##
This class allows you to calculate the check digit for ISBN-10 and ISBN-13.
Examples:

Isbn\CheckDigit::make('888183718'); // Will return 8
Isbn\CheckDigit::make('978888952719'); // Will return 1
Isbn\CheckDigit::make10('888183718'); // Will return 8
Isbn\CheckDigit::make13('978888952719'); // Will return 1
$isbn->checkDigit->make('888183718'); // Will return 8
$isbn->checkDigit->make('978888952719'); // Will return 1
$isbn->checkDigit->make10('888183718'); // Will return 8
$isbn->checkDigit->make13('978888952719'); // Will return 1

## Translate ##
This class allows you to convert ISBN-10 to ISBN-13 and back.
Examples:

Isbn\Translate::to13('8889527191'); // Will return 9788889527191
Isbn\Translate::to10('9786028328227'); // Will return 6028328227
$isbn->translate->to13('8889527191'); // Will return 9788889527191
$isbn->translate->to10('9786028328227'); // Will return 6028328227

# Develop this library #
If you are interested in some new features please open a bug on GitHub. If you already have a patch available, please, open a pull request. Before opening a pull request, be sure that all tests are passed.
Expand Down
21 changes: 13 additions & 8 deletions src/Isbn/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
namespace Isbn;

class Check {

private $hyphens;

public function __construct(Hyphens $hyphens) {
$this->hyphens = $hyphens;
}

public static function identify($isbn)
public function identify($isbn)
{
if (Check::is10($isbn))
if ($this->is10($isbn))
return 10;
if (Check::is13($isbn))
if ($this->is13($isbn))
return 13;
return false;
}

public static function is10($isbn)
public function is10($isbn)
{
$isbn = Hyphens::removeHyphens($isbn);
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) == 10)
return true;
return false;
}

public static function is13($isbn)
public function is13($isbn)
{
$isbn = Hyphens::removeHyphens($isbn);
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) == 13)
return true;
return false;
}

}
18 changes: 12 additions & 6 deletions src/Isbn/CheckDigit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

class CheckDigit {

public static function make($isbn)
private $hyphens;

public function __construct(Hyphens $hyphens) {
$this->hyphens = $hyphens;
}

public function make($isbn)
{
$isbn = Hyphens::removeHyphens($isbn);
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) == 12 OR strlen($isbn) == 13)
return CheckDigit::make13($isbn);
return $this->make13($isbn);
if (strlen($isbn) == 9 OR strlen($isbn) == 10)
return CheckDigit::make10($isbn);
return $this->make10($isbn);
return false;
}

public static function make10($isbn)
public function make10($isbn)
{
if (strlen($isbn) < 9 OR strlen($isbn) > 10)
return false;
Expand All @@ -33,7 +39,7 @@ public static function make10($isbn)
return $check;
}

public static function make13($isbn)
public function make13($isbn)
{
if (strlen($isbn) < 12 OR strlen($isbn) > 13)
return false;
Expand Down
18 changes: 8 additions & 10 deletions src/Isbn/Hyphens.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@ class Hyphens
private $isbn;
private $isbnSplit = Array();

public static function removeHyphens($isbn)
public function removeHyphens($isbn)
{
$isbn = str_replace(" ","",$isbn);
$isbn = str_replace("-","",$isbn);
return $isbn;
}

public static function fixHyphens($isbn, $s = "-")
public function fixHyphens($isbn, $s = "-")
{
$isbn = Hyphens::removeHyphens($isbn);
$hyphens = new Hyphens($isbn);
return $hyphens->addHyphens($s);
$isbn = $this->removeHyphens($isbn);
return $this->addHyphens($isbn, $s);
}

public function __construct($isbn)
public function addHyphens($isbn, $s = "-")
{
$this->isbn = $isbn;
}

public function addHyphens($s = "-")
{
$this->isbnSplit = [];

if (strlen($this->isbn) == 13)
$this->isbnSplit[0] = substr($this->isbn, 0, 3);

$this->getRegistrationGroupElement();
$this->getRegistrantElement();
$this->getPublicationElement();
Expand Down
18 changes: 18 additions & 0 deletions src/Isbn/Isbn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Isbn;

class Isbn {
public $check;
public $checkDigit;
public $hyphens;
public $translate;
public $validation;

public function __construct() {
$this->hyphens = new Hyphens();
$this->check = new Check($this->hyphens);
$this->checkDigit = new CheckDigit($this->hyphens);
$this->translate = new Translate($this->checkDigit);
$this->validation = new Validation($this->check, $this->hyphens);
}
}
14 changes: 10 additions & 4 deletions src/Isbn/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
namespace Isbn;

class Translate {

private $checkDigit;

public function __construct(CheckDigit $checkDigit) {
$this->checkDigit = $checkDigit;
}

public static function to10($isbn)
public function to10($isbn)
{
if (strlen($isbn) > 13)
$isbn = substr($isbn, 4, -1);
else
$isbn = substr($isbn, 3, -1);
return $isbn . CheckDigit::make($isbn);
return $isbn . $this->checkDigit->make($isbn);
}

public static function to13($isbn)
public function to13($isbn)
{
$isbn = substr($isbn, 0, -1);
if (strlen($isbn) > 9)
$isbn = "978-" . $isbn;
else
$isbn = "978" . $isbn;
return $isbn . CheckDigit::make($isbn);
return $isbn . $this->checkDigit->make($isbn);
}

}
26 changes: 17 additions & 9 deletions src/Isbn/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
namespace Isbn;

class Validation {

private $check;
private $hyphens;

public static function isbn($isbn)
public function __construct(Check $check, Hyphens $hyphens) {
$this->check = $check;
$this->hyphens = $hyphens;
}

public function isbn($isbn)
{
if (Check::is13($isbn))
return Validation::isbn13($isbn);
if (Check::is10($isbn))
return Validation::isbn10($isbn);
if ($this->check->is13($isbn))
return $this->isbn13($isbn);
if ($this->check->is10($isbn))
return $this->isbn10($isbn);
return false;
}

public static function isbn10($isbn)
public function isbn10($isbn)
{
$isbn = Hyphens::removeHyphens($isbn);
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) != 10)
return false;
if (!preg_match("/\d{9}[0-9xX]/i",$isbn))
Expand All @@ -29,9 +37,9 @@ public static function isbn10($isbn)
return $check % 11 == 0;
}

public static function isbn13($isbn)
public function isbn13($isbn)
{
$isbn = Hyphens::removeHyphens($isbn);
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) != 13)
return false;
if (!preg_match("/\d{13}/i",$isbn))
Expand Down
22 changes: 12 additions & 10 deletions tests/CheckDigitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ class CheckDigitTest extends PHPUnit_Framework_TestCase

public function testIsbn10()
{
$this->assertEquals('8', Isbn\CheckDigit::make10('888183718'));
$this->assertEquals('8', Isbn\CheckDigit::make10('8881837188'));
$this->assertEquals('0', Isbn\CheckDigit::make10('711119626'));
$this->assertEquals('0', Isbn\CheckDigit::make10('7111196266'));
$isbn = new Isbn\Isbn();
$this->assertEquals('8', $isbn->checkDigit->make10('888183718'));
$this->assertEquals('8', $isbn->checkDigit->make10('8881837188'));
$this->assertEquals('0', $isbn->checkDigit->make10('711119626'));
$this->assertEquals('0', $isbn->checkDigit->make10('7111196266'));
}

public function testIsbn13()
{
$this->assertEquals('1', Isbn\CheckDigit::make13('978888952719'));
$this->assertEquals('1', Isbn\CheckDigit::make13('9788889527191'));
$isbn = new Isbn\Isbn();
$this->assertEquals('1', $isbn->checkDigit->make13('978888952719'));
$this->assertEquals('1', $isbn->checkDigit->make13('9788889527191'));
}

public function testIsbn()
{
$this->assertEquals('8', Isbn\CheckDigit::make('888183718'));
$this->assertEquals('1', Isbn\CheckDigit::make('978888952719'));
$this->assertFalse(Isbn\CheckDigit::make('97888895271921'));
$isbn = new Isbn\Isbn();
$this->assertEquals('8', $isbn->checkDigit->make('888183718'));
$this->assertEquals('1', $isbn->checkDigit->make('978888952719'));
$this->assertFalse($isbn->checkDigit->make('97888895271921'));
}

}
Loading