Skip to content

Commit f0777e8

Browse files
committed
Merge pull request #8 from scento/exceptions
Exceptions
2 parents 662cab6 + 3a7d3a1 commit f0777e8

File tree

6 files changed

+87
-15
lines changed

6 files changed

+87
-15
lines changed

src/Isbn/Check.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ public function identify($isbn)
5353
*
5454
* @param string $isbn
5555
* @return boolean
56+
* @throws Exception
5657
*/
5758
public function is10($isbn)
5859
{
60+
if(is_string($isbn) === false) {
61+
throw new Exception('Invalid parameter type.');
62+
}
63+
5964
$isbn = $this->hyphens->removeHyphens($isbn);
6065
return (strlen($isbn) === 10);
6166
}
@@ -65,9 +70,14 @@ public function is10($isbn)
6570
*
6671
* @param string $isbn
6772
* @return boolean
73+
* @throws Exception
6874
*/
6975
public function is13($isbn)
7076
{
77+
if(is_string($isbn) === false) {
78+
throw new Exception('Invalid parameter type.');
79+
}
80+
7181
$isbn = $this->hyphens->removeHyphens($isbn);
7282
return (strlen($isbn) === 13);
7383
}

src/Isbn/CheckDigit.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __construct(Hyphens $hyphens)
3939
*/
4040
public function make($isbn)
4141
{
42+
if(is_string($isbn) === false) {
43+
throw new Exception('Invalid parameter type.');
44+
}
45+
4246
$isbn = $this->hyphens->removeHyphens($isbn);
4347
if (strlen($isbn) === 12 or strlen($isbn) === 13) {
4448
return $this->make13($isbn);
@@ -53,20 +57,25 @@ public function make($isbn)
5357
* Calculate the check digit of the ISBN-10 $isbn.
5458
*
5559
* @param string $isbn
56-
* @return boolean|string|int
60+
* @return string|int
61+
* @throws Exception
5762
*/
5863
public function make10($isbn)
5964
{
65+
if(is_string($isbn) === false) {
66+
throw new Exception('Invalid parameter type.');
67+
}
68+
6069
//Verify length
6170
$isbnLength = strlen($isbn);
6271
if ($isbnLength < 9 or $isbnLength > 10) {
63-
return false;
72+
throw new Exception('Invalid ISBN-10 format.');
6473
}
6574

6675
//Calculate check digit
6776
$check = 0;
6877
for ($i = 0; $i < 9; $i++) {
69-
if ($isbn[$i] === "X") {
78+
if ($isbn[$i] === 'X') {
7079
$check += 10 * intval(10 - $i);
7180
} else {
7281
$check += intval($isbn[$i]) * intval(10 - $i);
@@ -87,14 +96,19 @@ public function make10($isbn)
8796
* Calculate the check digit of the ISBN-13 $isbn
8897
*
8998
* @param string $isbn
90-
* @return boolean|int
99+
* @return int
100+
* @throws Exception
91101
*/
92102
public function make13($isbn)
93103
{
104+
if(is_string($isbn) === false) {
105+
throw new Exception('Invalid parameter type.');
106+
}
107+
94108
//Verify length
95109
$isbnLength = strlen($isbn);
96110
if ($isbnLength < 12 or $isbnLength > 13) {
97-
return false;
111+
throw new Exception('Invalid ISBN-13 format.');
98112
}
99113

100114
//Calculate check digit

src/Isbn/Exception.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* ISBN Exception
4+
*
5+
* @author Wenzel Pünter <[email protected]>
6+
* @version 2.0.0
7+
* @package ISBN
8+
*/
9+
namespace Isbn;
10+
11+
/**
12+
* ISBN Exception
13+
*/
14+
class Exception extends \Exception
15+
{
16+
17+
}

src/Isbn/Hyphens.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ class Hyphens
3434
*
3535
* @param string $isbn
3636
* @return string
37+
* @throws Exception
3738
*/
3839
public function removeHyphens($isbn)
3940
{
40-
$isbn = str_replace(" ", "", $isbn);
41-
$isbn = str_replace("-", "", $isbn);
41+
if(is_string($isbn) === false) {
42+
throw new Exception('Invalid parameter type.');
43+
}
44+
45+
$isbn = str_replace(' ', '', $isbn);
46+
$isbn = str_replace('-', '', $isbn);
4247
return $isbn;
4348
}
4449

@@ -49,7 +54,7 @@ public function removeHyphens($isbn)
4954
* @param string $char
5055
* @return string
5156
*/
52-
public function fixHyphens($isbn, $char = "-")
57+
public function fixHyphens($isbn, $char = '-')
5358
{
5459
$isbn = $this->removeHyphens($isbn);
5560
return $this->addHyphens($isbn, $char);
@@ -60,9 +65,15 @@ public function fixHyphens($isbn, $char = "-")
6065
*
6166
* @param string $isbn
6267
* @param string $char
68+
* @throws Exception
6369
*/
64-
public function addHyphens($isbn, $char = "-")
70+
public function addHyphens($isbn, $char = '-')
6571
{
72+
if(is_string($isbn) === false ||
73+
is_string($char) === false) {
74+
throw new Exception('Invalid parameter type.');
75+
}
76+
6677
$this->isbn = $isbn;
6778
$this->isbnSplit = array();
6879

@@ -157,7 +168,7 @@ private function getRegistrantElement()
157168
if (isset($this->isbnSplit[0]) === true) {
158169
$soFar = implode('-', $this->isbnSplit);
159170
} else {
160-
$soFar = "978-".$this->isbnSplit[1];
171+
$soFar = '978-'.$this->isbnSplit[1];
161172
}
162173

163174
switch ($soFar) {

src/Isbn/Translate.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ public function __construct(CheckDigit $checkDigit)
3838
*
3939
* @param string $isbn
4040
* @return string
41+
* @throws Exception
4142
*/
4243
public function to10($isbn)
4344
{
45+
if(is_string($isbn) === false) {
46+
throw new Exception('Invalid parameter type.');
47+
}
48+
4449
if (strlen($isbn) > 13) {
4550
$isbn = substr($isbn, 4, -1);
4651
} else {
@@ -55,15 +60,20 @@ public function to10($isbn)
5560
*
5661
* @param string $isbn
5762
* @return string
63+
* @throws Exception
5864
*/
5965
public function to13($isbn)
6066
{
67+
if(is_string($isbn) === false) {
68+
throw new Exception('Invalid parameter type.');
69+
}
70+
6171
$isbn = substr($isbn, 0, -1);
6272

6373
if (strlen($isbn) > 9) {
64-
$isbn = "978-".$isbn;
74+
$isbn = '978-'.$isbn;
6575
} else {
66-
$isbn = "978".$isbn;
76+
$isbn = '978'.$isbn;
6777
}
6878

6979
return $isbn.$this->checkDigit->make($isbn);

src/Isbn/Validation.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,27 @@ public function isbn($isbn)
6161
*
6262
* @param string $isbn
6363
* @return boolean
64+
* @throws Exception
6465
*/
6566
public function isbn10($isbn)
6667
{
68+
if(is_string($isbn) === false) {
69+
throw new Exception('Invalid parameter type.');
70+
}
71+
6772
//Verify ISBN-10 scheme
6873
$isbn = $this->hyphens->removeHyphens($isbn);
6974
if (strlen($isbn) != 10) {
7075
return false;
7176
}
72-
if (preg_match("/\d{9}[0-9xX]/i",$isbn) == false) {
77+
if (preg_match('/\d{9}[0-9xX]/i',$isbn) == false) {
7378
return false;
7479
}
7580

7681
//Verify checksum
7782
$check = 0;
7883
for ($i = 0; $i < 10; $i++) {
79-
if ($isbn[$i] === "X") {
84+
if ($isbn[$i] === 'X') {
8085
$check += 10 * intval(10 - $i);
8186
} else {
8287
$check += intval($isbn[$i]) * intval(10 - $i);
@@ -90,15 +95,20 @@ public function isbn10($isbn)
9095
*
9196
* @param string $isbn
9297
* @return boolean
98+
* @throws Exception
9399
*/
94100
public function isbn13($isbn)
95101
{
102+
if(is_string($isbn) === false) {
103+
throw new Exception('Invalid parameter type.');
104+
}
105+
96106
//Verify ISBN-13 scheme
97107
$isbn = $this->hyphens->removeHyphens($isbn);
98108
if (strlen($isbn) != 13) {
99109
return false;
100110
}
101-
if (preg_match("/\d{13}/i",$isbn) == false) {
111+
if (preg_match('/\d{13}/i',$isbn) == false) {
102112
return false;
103113
}
104114

0 commit comments

Comments
 (0)