Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezisbn13.php
Go to the documentation of this file.
1 <?php
20 class eZISBN13
21 {
22 const PREFIX_LENGTH = 3;
23 const CHECK_LENGTH = 1;
24 const LENGTH = 13;
25 const PREFIX_978 = 978;
26 const PREFIX_979 = 979;
27
34 public function __construct( $isbnNr = null, $separator = '-' )
35 {
36 if ( $isbnNr !== null )
37 {
38 $this->extractISBNNumber( $isbnNr, $separator );
39 }
40 else
41 {
42 $this->Prefix = false;
43 $this->RegistrationGroup = false;
44 $this->RegistrantElement = false;
45 $this->PublicationElement = false;
46 $this->CheckDigit = false;
47 }
48 }
49
54 function attributes()
55 {
56 return array( 'has_content',
57 'group_ranges',
58 'groups' );
59 }
60
66 function attribute( $value )
67 {
68 switch ( $value )
69 {
70 case "has_content":
71 {
72 return eZISBN13::hasRangeData();
73 }break;
74
75 case "groups":
76 {
77 $groupList = eZISBNGroup::fetchList();
78 return array( 'group_list' => $groupList,
79 'count' => count( $groupList ) );
80 }break;
81
82 case "group_ranges":
83 {
84 $groupList = eZISBNGroupRange::fetchList();
85 return array( 'group_list' => $groupList,
86 'count' => count( $groupList ) );
87 }break;
88 }
89 return null;
90 }
91
97 function hasAttribute( $value )
98 {
99 return in_array( $value, eZISBN13::attributes() );
100 }
101
106 function hasRangeData()
107 {
108 $db = eZDB::instance();
109 $tableList = $db->eZTableList();
110 if ( array_key_exists( 'ezisbn_group', $tableList ) and
111 array_key_exists( 'ezisbn_group_range', $tableList ) and
112 array_key_exists( 'ezisbn_registrant_range', $tableList ) )
113 {
114 $query = "SELECT count( ezisbn_group.id ) AS count
115 FROM ezisbn_group, ezisbn_group_range, ezisbn_registrant_range
116 WHERE ezisbn_group.group_number >= ezisbn_group_range.from_number AND
117 ezisbn_group.group_number <= ezisbn_group_range.to_number AND
118 ezisbn_group.id=ezisbn_registrant_range.isbn_group_id";
119 $countArray = $db->arrayQuery( $query );
120 return ( $countArray[0]['count'] > 0 );
121 }
122 else
123 return false;
124 }
125
138 function formatedISBNValue( $isbnNr = false, &$error, $separator = '-' )
139 {
140 if ( $isbnNr !== false )
141 {
142 $formatedISBN13 = preg_replace( "/[\s|\-]+/", "-", $isbnNr );
143 $status = $this->extractISBNNumber( $isbnNr, $error );
144
145 if ( $status === false )
146 {
147 $formatedISBN13 = substr( $isbnNr, 0, self::PREFIX_LENGTH );
148 if ( strlen( $this->RegistrationGroup ) > 0 )
149 {
150 $formatedISBN13 .= $separator . $this->RegistrationGroup;
151 if ( strlen( $this->RegistrantElement ) > 0 )
152 {
153 $formatedISBN13 .= $separator . $this->RegistrantElement . $separator .
154 $this->PublicationElement . $separator;
155 }
156 else
157 {
158 $offset = strlen( $this->RegistrationGroup ) + self::PREFIX_LENGTH;
159 $length = strlen( $isbnNr ) - $offset - self::CHECK_LENGTH;
160 $originalValue = substr( $isbnNr, $offset, $length );
161 $formatedISBN13 .= $originalValue;
162 }
163 }
164 else
165 {
166 $offset = self::PREFIX_LENGTH;
167 $length = strlen( $isbnNr ) - $offset - self::CHECK_LENGTH;
168 $originalValue = substr( $isbnNr, $offset, $length );
169 $formatedISBN13 .= $originalValue;
170 }
171
172 $length = strlen( $isbnNr );
173 $formatedISBN13 .= substr( $isbnNr, $length - self::CHECK_LENGTH, $length );
174 return $formatedISBN13;
175 }
176 }
177 else
178 {
179 $formatedISBN13 = $this->Prefix . $separator;
180 if ( strlen( $this->RegistrationGroup ) > 0 )
181 {
182 $formatedISBN13 .= $this->RegistrationGroup . $separator;
183 if ( strlen( $this->RegistrantElement ) > 0 )
184 {
185 $formatedISBN13 .= $this->RegistrantElement . $separator .
186 $this->PublicationElement . $separator;
187 }
188 else
189 {
190 $formatedISBN13 .= $this->RegistrantElement .
191 $this->PublicationElement . $separator;
192 }
193 }
194 else
195 {
196 $formatedISBN13 .= $this->RegistrationGroup .
197 $this->RegistrantElement .
198 $this->PublicationElement . $separator;
199 }
200 $formatedISBN13 .= $this->CheckDigit;
201 }
202
203 if ( strlen( $this->Prefix . $this->RegistrationGroup . $this->RegistrantElement . $this->PublicationElement . $this->CheckDigit ) == self::LENGTH )
204 {
205 $formatedISBN13 = $this->Prefix . $separator .
206 $this->RegistrationGroup . $separator .
207 $this->RegistrantElement . $separator .
208 $this->PublicationElement . $separator .
209 $this->CheckDigit;
210 }
211 return $formatedISBN13;
212 }
213
225 function extractISBNNumber( $isbnNr = false, &$error )
226 {
227 $ini = eZINI::instance( 'content.ini' );
228 $ean = preg_replace( "/[\s|\-]+/", "", $isbnNr );
229 if ( is_numeric( $ean ) and strlen( $ean ) == self::LENGTH )
230 {
231 $prefix = substr( $ean, 0, self::PREFIX_LENGTH );
232 $this->Prefix = $prefix;
233
234 $checkDigit = substr( $ean, 12, self::CHECK_LENGTH );
235 $this->CheckDigit = $checkDigit;
236 if ( $prefix == self::PREFIX_978 )
237 {
238 $registrantValue = false;
239 $groupValue = false;
240 $publicationValue = false;
241 $checkDigit = false;
242
243 $groupRange = eZISBNGroupRange::extractGroup( $ean );
244 $groupLength = false;
245 if ( $groupRange )
246 {
247 $groupLength = $groupRange->attribute( 'group_length' );
248 }
249
250 if ( $groupLength )
251 {
252 $groupValue = substr( $ean, self::PREFIX_LENGTH, $groupLength );
253 $this->RegistrationGroup = $groupValue;
254
255 $group = eZISBNGroup::fetchByGroup( $groupValue );
256 if ( $group instanceof eZISBNGroup )
257 {
258 $registrant = eZISBNRegistrantRange::extractRegistrant( $ean, $group, $groupRange, $registrantLength );
259 if ( $registrant instanceof eZISBNRegistrantRange and
260 $registrantLength > 0 )
261 {
262 $registrantOffset = self::PREFIX_LENGTH + $groupLength;
263 $registrantValue = substr( $ean, $registrantOffset, $registrantLength );
264
265 $this->RegistrantElement = $registrantValue;
266
267 $publicationOffset = $registrantOffset + $registrantLength;
268 $publicationLength = 12 - $publicationOffset;
269 $publicationValue = substr( $ean, $publicationOffset, $publicationLength );
270 $this->PublicationElement = $publicationValue;
271 }
272 else
273 {
274 $strictValidation = $ini->variable( 'ISBNSettings', 'StrictValidation' );
275 if ( $strictValidation == 'true' )
276 {
277 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'The registrant element of the ISBN number does not exist.' );
278 return false;
279 }
280 }
281 }
282 else
283 {
284 $strictValidation = $ini->variable( 'ISBNSettings', 'StrictValidation' );
285 if ( $strictValidation == 'true' )
286 {
287 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'The ISBN number has a incorrect registration group number.' );
288 return false;
289 }
290 }
291 }
292 else
293 {
294 $strictValidation = $ini->variable( 'ISBNSettings', 'StrictValidation' );
295 if ( $strictValidation == 'true' )
296 {
297 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'The group element of the ISBN number does not exist.' );
298 return false;
299 }
300 }
301 }
302 else
303 {
304 $strictValidation = $ini->variable( 'ISBNSettings', 'StrictValidation' );
305 if ( $strictValidation == 'true' )
306 {
307 $error = ezpI18n::tr( 'kernel/classes/datatypes', '%1 is not a valid prefix of the ISBN number.', null, array( $prefix ) );
308 return false;
309 }
310 }
311 }
312 else
313 {
314 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'All ISBN 13 characters need to be numeric' );
315 return false;
316 }
317 return true;
318 }
319
327 function validate( $isbnNr, &$error )
328 {
329 $valid = $this->validateISBN13Checksum( $isbnNr, $error );
330 if ( $valid == true )
331 {
332 $valid = $this->extractISBNNumber( $isbnNr, $error );
333 }
334 return $valid;
335 }
336
345 function validateISBN13Checksum ( $isbnNr, &$error )
346 {
347 if ( !$isbnNr )
348 return false;
349 $isbnNr = preg_replace( "/[\s|\-]+/", "", $isbnNr );
350 if ( substr( $isbnNr, 0, self::PREFIX_LENGTH ) != self::PREFIX_978 and
351 substr( $isbnNr, 0, self::PREFIX_LENGTH ) != self::PREFIX_979 )
352 {
353 $error = ezpI18n::tr( 'kernel/classes/datatypes',
354 '13 digit ISBN must start with 978 or 979' );
355 return false;
356 }
357
358 $checksum13 = 0;
359 $weight13 = 1;
360 if ( strlen( $isbnNr ) != self::LENGTH )
361 {
362 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'ISBN length is invalid' );
363 return false;
364 }
365
366 //compute checksum
367 $val = 0;
368 for ( $i = 0; $i < self::LENGTH; $i++ )
369 {
370 $val = $isbnNr{$i};
371 if ( !is_numeric( $isbnNr{$i} ) )
372 {
373 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'All ISBN 13 characters need to be numeric' );
374 return false;
375 }
376 $checksum13 = $checksum13 + $weight13 * $val;
377 $weight13 = ( $weight13 + 2 ) % 4;
378 }
379 if ( ( $checksum13 % 10 ) != 0 )
380 {
381 // Calculate the last digit from the 12 first numbers.
382 $checkDigit = ( 10 - ( ( $checksum13 - ( ( $weight13 + 2 ) % 4 ) * $val ) % 10 ) ) % 10;
383 //bad checksum
384 $error = ezpI18n::tr( 'kernel/classes/datatypes', 'Bad checksum, last digit should be %1', null, array( $checkDigit ) );
385 return false;
386 }
387
388 return true;
389 }
390
391 public $Prefix;
392 public $RegistrationGroup;
393 public $RegistrantElement;
394 public $PublicationElement;
395 public $CheckDigit;
396 }
397
398 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZISBN13
The class eZISBN13 handles ISBN-13 numbers.
Definition: ezisbn13.php:21
eZISBN13\PREFIX_978
const PREFIX_978
Definition: ezisbn13.php:25
eZISBN13\validateISBN13Checksum
validateISBN13Checksum( $isbnNr, &$error)
Definition: ezisbn13.php:345
eZISBN13\LENGTH
const LENGTH
Definition: ezisbn13.php:24
eZISBN13\PREFIX_979
const PREFIX_979
Definition: ezisbn13.php:26
eZISBN13\hasRangeData
hasRangeData()
Definition: ezisbn13.php:106
eZISBN13\attributes
attributes()
Definition: ezisbn13.php:54
eZISBN13\hasAttribute
hasAttribute( $value)
Definition: ezisbn13.php:97
eZISBN13\validate
validate( $isbnNr, &$error)
Definition: ezisbn13.php:327
eZISBN13\$CheckDigit
$CheckDigit
Definition: ezisbn13.php:395
eZISBN13\$RegistrantElement
$RegistrantElement
Definition: ezisbn13.php:393
eZISBN13\$Prefix
$Prefix
Definition: ezisbn13.php:391
eZISBN13\$PublicationElement
$PublicationElement
Definition: ezisbn13.php:394
eZISBN13\CHECK_LENGTH
const CHECK_LENGTH
Definition: ezisbn13.php:23
eZISBN13\extractISBNNumber
extractISBNNumber( $isbnNr=false, &$error)
Definition: ezisbn13.php:225
eZISBN13\attribute
attribute( $value)
Definition: ezisbn13.php:66
eZISBN13\PREFIX_LENGTH
const PREFIX_LENGTH
Definition: ezisbn13.php:22
eZISBN13\formatedISBNValue
formatedISBNValue( $isbnNr=false, &$error, $separator='-')
Definition: ezisbn13.php:138
eZISBN13\__construct
__construct( $isbnNr=null, $separator='-')
Definition: ezisbn13.php:34
eZISBN13\$RegistrationGroup
$RegistrationGroup
Definition: ezisbn13.php:392
eZISBNGroupRange\fetchList
static fetchList( $asObject=true)
Definition: ezisbngrouprange.php:104
eZISBNGroupRange\extractGroup
static extractGroup( $isbnNr)
Definition: ezisbngrouprange.php:119
eZISBNGroup
The class eZISBNGroup handles ISBN Registration groups.
Definition: ezisbngroup.php:24
eZISBNGroup\fetchList
static fetchList( $asObject=true)
Definition: ezisbngroup.php:82
eZISBNGroup\fetchByGroup
static fetchByGroup( $groupNumber, $asObject=true)
Definition: ezisbngroup.php:96
eZISBNRegistrantRange
The class eZISBNRegistrantRange handles Registrant ranges.
Definition: ezisbnregistrantrange.php:30
eZISBNRegistrantRange\extractRegistrant
static extractRegistrant( $isbnNr, $group, $groupRange, &$registrantLength)
Definition: ezisbnregistrantrange.php:151
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.