-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisbn.test.php
332 lines (317 loc) · 12.2 KB
/
isbn.test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
* This class contains a method to do validity checks on ISBNs.
* it's pretty basic and should require no further docs
*
* Updated to be more flexible in the face of
*
* copyright 1999, 2004, 2008 Keith Nunn
* Released under the terms of the GNU General Public License v.2 or later
*/
class ISBNtest
{
private $isbn10 = FALSE; // the stripped ISBN-10, includes given checkdigit
private $isbn13 = FALSE; // the stripped ISBN-13 (or Bookland EAN), includes given checkdigit
private $gtin14 = FALSE; // the stripped GTIN-14 (or ISBN-14), includes given checkdigit
private $error = ""; // error to return, if required
private function get_isbn10_checkdigit()
// calculate the checkdigit for ISBN-10
{
if (strlen($this->isbn10) != 10)
{
return FALSE;
$this->error = "Given ISBN-10 is not 10 digits (" . $this->isbn10 . ")";
}
/*
* this checkdigit calculation could probably be expressed in less
* space using a lop, but this keeps it very clear what the math
* involved is
*/
$checkdigit = 11 - ( ( 10 * substr($this->isbn10,0,1) + 9 * substr($this->isbn10,1,1) + 8 * substr($this->isbn10,2,1) + 7 * substr($this->isbn10,3,1) + 6 * substr($this->isbn10,4,1) + 5 * substr($this->isbn10,5,1) + 4 * substr($this->isbn10,6,1) + 3 * substr($this->isbn10,7,1) + 2 * substr($this->isbn10,8,1) ) % 11);
/*
* convert the numeric check value
* into the single char version
*/
switch ( $checkdigit )
{
case 10:
$checkdigit = "X";
break;
case 11:
$checkdigit = 0;
break;
default:
}
return $checkdigit;
}
/***********************************************/
private function get_isbn13_checkdigit()
// calculate the checkdigit for ISBN-10
{
if (strlen($this->isbn13) != 13)
{
return FALSE;
$this->error = "Given ISBN-13 is not 10 digits (" . $this->isbn13 . ")";
}
/*
* this checkdigit calculation could probably be expressed in less
* space using a lop, but this keeps it very clear what the math
* involved is
*/
$checkdigit = 10 - ( ( 1 * substr($this->isbn13,0,1) + 3 * substr($this->isbn13,1,1) + 1 * substr($this->isbn13,2,1) + 3 * substr($this->isbn13,3,1) + 1 * substr($this->isbn13,4,1) + 3 * substr($this->isbn13,5,1) + 1 * substr($this->isbn13,6,1) + 3 * substr($this->isbn13,7,1) + 1 * substr($this->isbn13,8,1) + 3 * substr($this->isbn13,9,1) + 1 * substr($this->isbn13,10,1) + 3 * substr($this->isbn13,11,1) ) % 10 );
/*
* convert the numeric check value
* into the single char version
*/
if ( $checkdigit == 10 )
{
$checkdigit = "0";
}
return $checkdigit;
}
/***********************************************/
private function get_gtin14_checkdigit()
// calculate the checkdigit for GTIN
{
if (strlen($this->gtin14) != 14)
{
return FALSE;
$this->error = "Given GTIN is not 14 digits (" . $this->gtin14 . ")";
}
$checkdigit = 10 - ( ( 3 * substr($gtin14,0,1) + 1 * substr($gtin14,1,1) + 3 * substr($gtin14,2,1) + 1 * substr($gtin14,3,1) + 3 * substr($gtin14,4,1) + 1 * substr($gtin14,5,1) + 3 * substr($gtin14,6,1) + 1 * substr($gtin14,7,1) + 3 * substr($gtin14,8,1) + 1 * substr($gtin14,9,1) + 3 * substr($gtin14,10,1) + 1 * substr($gtin14,11,1) + 3 * substr($gtin14,12,1) ) % 10 );
/*
* convert the numeric check value
* into the single char version
*/
if ( $checkdigit == 10 )
{
$checkdigit = "0";
}
return $checkdigit;
}
/***********************************************/
public function set_isbn10($isbn)
{
$isbn = ereg_replace("[^0-9X]","",strtoupper($isbn)); // strip to the basic ISBN
if (strlen($isbn)==10)
{
$this->isbn10 = $isbn;
}
else
{
$this->error = "ISBN-10 given is not 10 digits ($isbn)";
return FALSE;
}
}
/***********************************************/
public function set_isbn13($isbn)
{
$isbn = ereg_replace("[^0-9]","",strtoupper($isbn)); // strip to the basic ISBN
if (strlen($isbn)==13)
{
$this->isbn13 = $isbn;
}
else
{
$this->error = "ISBN-13 given is not 13 digits ($isbn)";
return FALSE;
}
}
/***********************************************/
public function set_gtin14($isbn)
{
$isbn = ereg_replace("[^0-9]","",strtoupper($isbn)); // strip to the basic ISBN
if (strlen($isbn)==14)
{
$this->gtin14 = $isbn;
}
else
{
$this->error = "GTIN given is not 14 digits ($isbn)";
return FALSE;
}
}
/***********************************************/
public function set_isbn($isbn)
// trying to provide a common interface here so it's possible to cope
// if you don't know for sure what you have -- provided the data is valid
{
$isbn = ereg_replace("[^0-9X]","",strtoupper($isbn)); // strip to the basic ISBN
if (strlen($isbn)==14)
{
$this->set_gtin14($isbn);
return TRUE;
}
if (strlen($isbn)==13)
{
$this->set_isbn13($isbn);
return TRUE;
}
elseif (strlen($isbn)==10)
{
$this->isbn10 = $isbn;
return TRUE;
}
else
{
$this->error = "ISBN given is not 10, 13, or 14 digits ($isbn)";
return FALSE;
}
}
/***********************************************/
public function valid_isbn10($isbn="")
// report on the validity of the ISBN-10 we have or are given
{
if ($isbn != "") // If we've been given a new ISBN then use it.
{
$this->set_isbn10($isbn);
}
if ( FALSE === $this->isbn10 && FALSE !== $this->isbn13 )
{
if ( TRUE === $this->valid_isbn13() )
{
$this->get_isbn10();
}
}
if ( FALSE === $this->isbn10 || strlen($this->isbn10) != 10 )
{
$this->error = "ISBN-10 is not set";
return FALSE;
}
if ( (string) substr($this->isbn10,9,1) === (string) $this->get_isbn10_checkdigit() )
{
return TRUE;
}
else
{
$this->error = "Checkdigit failure";
return FALSE;
}
}
/***********************************************/
public function valid_isbn13($isbn="")
// report on the validity of the ISBN-13 we have or are given
{
if ($isbn != "") // if we've been given an isbn here, use it
{
$this->set_isbn13($isbn);
}
if ( FALSE === $this->isbn13 && FALSE !== $this->isbn10 )
{
if ( TRUE === $this->valid_isbn10() )
{
$this->get_isbn13();
}
}
if ( FALSE === $this->isbn13 || strlen($this->isbn13) != 13 )
{
$this->error = "ISBN-13 is not set";
return FALSE;
}
if ( (string) substr($this->isbn13,12,1) === (string) $this->get_isbn13_checkdigit() )
{
return TRUE;
}
else
{
$this->error = "Checkdigit failure";
return FALSE;
}
}
/***********************************************/
public function valid_gtin14($isbn="")
// report on the validity of the GTIN we have or are given
{
if ($isbn != "") // if we've been given an ISBN here, use it
{
$this->set_gtin14($isbn);
}
if ( substr($this->gtin14,13,1) === $this->get_gtin14_checkdigit() )
{
return TRUE;
}
else
{
$this->error = "Checkdigit failure";
return FALSE;
}
}
/***********************************************/
public function valid_isbn($isbn="")
// trying to provide a common interface here so it's possible to cope
// if you don't know for sure what you have -- provided the data is valid
{
if ($isbn != "") // if we've been given an ISBN then use it
{
$this->set_isbn($isbn);
}
if ( (isset($this->gtin14) && $this->valid_gtin14() == TRUE) || (isset($this->isbn13) && $this->valid_isbn13() == TRUE) || (isset($this->isbn10) && $this->valid_isbn10() == TRUE) ) // in this routine, we don't care what kind it is, only that it's valid.
{
return TRUE;
}
else
{
$this->error = "Checkdigit failure";
return FALSE;
}
}
/***********************************************/
public function get_isbn10()
// return the ISBN-10 that has been set or create one if we have a valid ISBN-13
{
if ( $this->isbn10 != FALSE )
{
return $this->isbn10;
}
elseif ( $this->valid_isbn13() != FALSE )
{
if ( eregi("979", $this->isbn13) )
{
$this->error = "979 Bookland EAN values can't be converted to ISBN-10";
return FALSE; // if it's a 979 prefix it can't be downgraded
}
else
{
$this->set_isbn10(substr($this->isbn13, 3, 10)); // invalid ISBN used as a temp value for next step
$checkdigit = $this->get_isbn10_checkdigit();
$this->set_isbn10(substr($this->isbn13, 3, 9) . $checkdigit); // true value (I hope)
return $this->isbn10;
}
}
else
{
$this->error = "No ISBN-10 value set or calculable";
return FALSE;
}
}
/*********************************************/
public function get_isbn13()
// return the ISBN-13 that has been set or create one if we have a valid ISBN-10
{
if ( $this->isbn13 != FALSE )
{
return $this->isbn13;
}
elseif ( $this->valid_isbn10() != FALSE )
{
$this->set_isbn13("978" . substr($this->isbn10, 0, 9) . "0"); // invalid ISBN used as a temp value for next step
$checkdigit = (string) $this->get_isbn13_checkdigit();
$this->set_isbn13("978" . substr($this->isbn10, 0, 9) . $checkdigit); // true value (I hope)
return $this->isbn13;
}
else
{
$this->error = "No ISBN-10 value set or calculable";
return FALSE;
}
}
/*********************************************/
public function get_error()
// return the error message
{
return $this->error;
}
/*********************************************/
}
?>