Skip to content

Commit

Permalink
Merge pull request #8 from scento/exceptions
Browse files Browse the repository at this point in the history
Exceptions
  • Loading branch information
Fale committed Oct 3, 2015
2 parents 662cab6 + 3a7d3a1 commit f0777e8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/Isbn/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ public function identify($isbn)
*
* @param string $isbn
* @return boolean
* @throws Exception
*/
public function is10($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

$isbn = $this->hyphens->removeHyphens($isbn);
return (strlen($isbn) === 10);
}
Expand All @@ -65,9 +70,14 @@ public function is10($isbn)
*
* @param string $isbn
* @return boolean
* @throws Exception
*/
public function is13($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

$isbn = $this->hyphens->removeHyphens($isbn);
return (strlen($isbn) === 13);
}
Expand Down
24 changes: 19 additions & 5 deletions src/Isbn/CheckDigit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function __construct(Hyphens $hyphens)
*/
public function make($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) === 12 or strlen($isbn) === 13) {
return $this->make13($isbn);
Expand All @@ -53,20 +57,25 @@ public function make($isbn)
* Calculate the check digit of the ISBN-10 $isbn.
*
* @param string $isbn
* @return boolean|string|int
* @return string|int
* @throws Exception
*/
public function make10($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

//Verify length
$isbnLength = strlen($isbn);
if ($isbnLength < 9 or $isbnLength > 10) {
return false;
throw new Exception('Invalid ISBN-10 format.');
}

//Calculate check digit
$check = 0;
for ($i = 0; $i < 9; $i++) {
if ($isbn[$i] === "X") {
if ($isbn[$i] === 'X') {
$check += 10 * intval(10 - $i);
} else {
$check += intval($isbn[$i]) * intval(10 - $i);
Expand All @@ -87,14 +96,19 @@ public function make10($isbn)
* Calculate the check digit of the ISBN-13 $isbn
*
* @param string $isbn
* @return boolean|int
* @return int
* @throws Exception
*/
public function make13($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

//Verify length
$isbnLength = strlen($isbn);
if ($isbnLength < 12 or $isbnLength > 13) {
return false;
throw new Exception('Invalid ISBN-13 format.');
}

//Calculate check digit
Expand Down
17 changes: 17 additions & 0 deletions src/Isbn/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* ISBN Exception
*
* @author Wenzel Pünter <[email protected]>
* @version 2.0.0
* @package ISBN
*/
namespace Isbn;

/**
* ISBN Exception
*/
class Exception extends \Exception
{

}
21 changes: 16 additions & 5 deletions src/Isbn/Hyphens.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ class Hyphens
*
* @param string $isbn
* @return string
* @throws Exception
*/
public function removeHyphens($isbn)
{
$isbn = str_replace(" ", "", $isbn);
$isbn = str_replace("-", "", $isbn);
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

$isbn = str_replace(' ', '', $isbn);
$isbn = str_replace('-', '', $isbn);
return $isbn;
}

Expand All @@ -49,7 +54,7 @@ public function removeHyphens($isbn)
* @param string $char
* @return string
*/
public function fixHyphens($isbn, $char = "-")
public function fixHyphens($isbn, $char = '-')
{
$isbn = $this->removeHyphens($isbn);
return $this->addHyphens($isbn, $char);
Expand All @@ -60,9 +65,15 @@ public function fixHyphens($isbn, $char = "-")
*
* @param string $isbn
* @param string $char
* @throws Exception
*/
public function addHyphens($isbn, $char = "-")
public function addHyphens($isbn, $char = '-')
{
if(is_string($isbn) === false ||
is_string($char) === false) {
throw new Exception('Invalid parameter type.');
}

$this->isbn = $isbn;
$this->isbnSplit = array();

Expand Down Expand Up @@ -157,7 +168,7 @@ private function getRegistrantElement()
if (isset($this->isbnSplit[0]) === true) {
$soFar = implode('-', $this->isbnSplit);
} else {
$soFar = "978-".$this->isbnSplit[1];
$soFar = '978-'.$this->isbnSplit[1];
}

switch ($soFar) {
Expand Down
14 changes: 12 additions & 2 deletions src/Isbn/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public function __construct(CheckDigit $checkDigit)
*
* @param string $isbn
* @return string
* @throws Exception
*/
public function to10($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

if (strlen($isbn) > 13) {
$isbn = substr($isbn, 4, -1);
} else {
Expand All @@ -55,15 +60,20 @@ public function to10($isbn)
*
* @param string $isbn
* @return string
* @throws Exception
*/
public function to13($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

$isbn = substr($isbn, 0, -1);

if (strlen($isbn) > 9) {
$isbn = "978-".$isbn;
$isbn = '978-'.$isbn;
} else {
$isbn = "978".$isbn;
$isbn = '978'.$isbn;
}

return $isbn.$this->checkDigit->make($isbn);
Expand Down
16 changes: 13 additions & 3 deletions src/Isbn/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,27 @@ public function isbn($isbn)
*
* @param string $isbn
* @return boolean
* @throws Exception
*/
public function isbn10($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

//Verify ISBN-10 scheme
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) != 10) {
return false;
}
if (preg_match("/\d{9}[0-9xX]/i",$isbn) == false) {
if (preg_match('/\d{9}[0-9xX]/i',$isbn) == false) {
return false;
}

//Verify checksum
$check = 0;
for ($i = 0; $i < 10; $i++) {
if ($isbn[$i] === "X") {
if ($isbn[$i] === 'X') {
$check += 10 * intval(10 - $i);
} else {
$check += intval($isbn[$i]) * intval(10 - $i);
Expand All @@ -90,15 +95,20 @@ public function isbn10($isbn)
*
* @param string $isbn
* @return boolean
* @throws Exception
*/
public function isbn13($isbn)
{
if(is_string($isbn) === false) {
throw new Exception('Invalid parameter type.');
}

//Verify ISBN-13 scheme
$isbn = $this->hyphens->removeHyphens($isbn);
if (strlen($isbn) != 13) {
return false;
}
if (preg_match("/\d{13}/i",$isbn) == false) {
if (preg_match('/\d{13}/i',$isbn) == false) {
return false;
}

Expand Down

0 comments on commit f0777e8

Please sign in to comment.