From 3c23184e08c494d3010421338381f6e0ac5dcbc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wenzel=20P=C3=BCnter?= Date: Thu, 23 Oct 2014 16:55:02 +0200 Subject: [PATCH 1/2] Use Exceptions This commit introduces Exceptions. --- src/Isbn/Check.php | 10 ++++++++++ src/Isbn/CheckDigit.php | 22 ++++++++++++++++++---- src/Isbn/Exception.php | 17 +++++++++++++++++ src/Isbn/Hyphens.php | 11 +++++++++++ src/Isbn/Translate.php | 10 ++++++++++ src/Isbn/Validation.php | 10 ++++++++++ 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/Isbn/Exception.php diff --git a/src/Isbn/Check.php b/src/Isbn/Check.php index df03e58..47200fd 100644 --- a/src/Isbn/Check.php +++ b/src/Isbn/Check.php @@ -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); } @@ -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); } diff --git a/src/Isbn/CheckDigit.php b/src/Isbn/CheckDigit.php index cfb915f..96bd338 100644 --- a/src/Isbn/CheckDigit.php +++ b/src/Isbn/CheckDigit.php @@ -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); @@ -53,14 +57,19 @@ 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 @@ -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 diff --git a/src/Isbn/Exception.php b/src/Isbn/Exception.php new file mode 100644 index 0000000..a8e0adb --- /dev/null +++ b/src/Isbn/Exception.php @@ -0,0 +1,17 @@ + + * @version 2.0.0 + * @package ISBN +*/ +namespace Isbn; + +/** + * ISBN Exception +*/ +class Exception extends \Exception +{ + +} \ No newline at end of file diff --git a/src/Isbn/Hyphens.php b/src/Isbn/Hyphens.php index 5884818..3692176 100644 --- a/src/Isbn/Hyphens.php +++ b/src/Isbn/Hyphens.php @@ -34,9 +34,14 @@ class Hyphens * * @param string $isbn * @return string + * @throws Exception */ public function removeHyphens($isbn) { + if(is_string($isbn) === false) { + throw new Exception('Invalid parameter type.'); + } + $isbn = str_replace(" ", "", $isbn); $isbn = str_replace("-", "", $isbn); return $isbn; @@ -60,9 +65,15 @@ public function fixHyphens($isbn, $char = "-") * * @param string $isbn * @param string $char + * @throws Exception */ 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(); diff --git a/src/Isbn/Translate.php b/src/Isbn/Translate.php index 4e828e0..05ae58c 100644 --- a/src/Isbn/Translate.php +++ b/src/Isbn/Translate.php @@ -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 { @@ -55,9 +60,14 @@ 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) { diff --git a/src/Isbn/Validation.php b/src/Isbn/Validation.php index b346910..f2b5070 100644 --- a/src/Isbn/Validation.php +++ b/src/Isbn/Validation.php @@ -61,9 +61,14 @@ 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) { @@ -90,9 +95,14 @@ 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) { From 3a7d3a198d6f6e7f46bcf48dfe071a8a6999b341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wenzel=20P=C3=BCnter?= Date: Thu, 23 Oct 2014 16:57:08 +0200 Subject: [PATCH 2/2] Prefer single quotes --- src/Isbn/CheckDigit.php | 2 +- src/Isbn/Hyphens.php | 10 +++++----- src/Isbn/Translate.php | 4 ++-- src/Isbn/Validation.php | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Isbn/CheckDigit.php b/src/Isbn/CheckDigit.php index 96bd338..1f34b8d 100644 --- a/src/Isbn/CheckDigit.php +++ b/src/Isbn/CheckDigit.php @@ -75,7 +75,7 @@ public function make10($isbn) //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); diff --git a/src/Isbn/Hyphens.php b/src/Isbn/Hyphens.php index 3692176..3022cd0 100644 --- a/src/Isbn/Hyphens.php +++ b/src/Isbn/Hyphens.php @@ -42,8 +42,8 @@ public function removeHyphens($isbn) throw new Exception('Invalid parameter type.'); } - $isbn = str_replace(" ", "", $isbn); - $isbn = str_replace("-", "", $isbn); + $isbn = str_replace(' ', '', $isbn); + $isbn = str_replace('-', '', $isbn); return $isbn; } @@ -54,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); @@ -67,7 +67,7 @@ public function fixHyphens($isbn, $char = "-") * @param string $char * @throws Exception */ - public function addHyphens($isbn, $char = "-") + public function addHyphens($isbn, $char = '-') { if(is_string($isbn) === false || is_string($char) === false) { @@ -168,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) { diff --git a/src/Isbn/Translate.php b/src/Isbn/Translate.php index 05ae58c..782c515 100644 --- a/src/Isbn/Translate.php +++ b/src/Isbn/Translate.php @@ -71,9 +71,9 @@ public function to13($isbn) $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); diff --git a/src/Isbn/Validation.php b/src/Isbn/Validation.php index f2b5070..d367ad6 100644 --- a/src/Isbn/Validation.php +++ b/src/Isbn/Validation.php @@ -74,14 +74,14 @@ public function isbn10($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); @@ -108,7 +108,7 @@ public function isbn13($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; }