ZF-1277: adding validation of isbn

Comments

I expect people will be open to adding this. A code submission would be appreciated :)

I think it should be added like Zend_Validate_Isbn but how we do the difference between ISBN-10 and ISBN-13 or is ISBN-10 deprecated enough that we can lose sight of? Or should we add Zend_Validate_Isbn_13Digits and Zend_Validate_Isbn_10Digits? DonĀ“t know.

What do you think?

Here a small quick and dirty solution for ISBN-13 and ISBN-10 without formatting (and no check for valide input):

<?php

function validateIsbn10($isbn)
{   
    $remainder = $isbn{9};
    $checksum  = 0;
    
    for ($i=10, $a=0; $i>1; $i--, $a++)
    {
        $num       = $isbn{$a};
        $checksum += $num * $i;
    }
    
    return ((11 - ($checksum % 11)) == $remainder);
}

function validateIsbn13($isbn)
{
    $remainder = $isbn{12};
    $checksum  = 0;
    
    for ($i=0; $i<12; $i++)
    {
        $multi     = (($i % 2) == 1) ? 1 : 3;
        $num       = $isbn{$i};
        $checksum += $num * $multi;
    }
    
    return ((10 - ($checksum % 10)) == $remainder);
}

$isbn = '9783866400016';
var_dump(validateIsbn13($isbn)); // true

$isbn = '0306406153';
var_dump(validateIsbn10($isbn)); // true

?>

The Zend_Validate_Isbn can configure before which isbn versions are alowed.


$isbn13 = '9783866400016';
$isbn10 = '0306406153';

$validator = new Zend_Validate_Isbn();
$validator ->allowVersion(10);
$validator->isValid($isbn10); // true
$validator->isValid($isbn13); // false

$validator ->allowVersion(13);
$validator->isValid($isbn10); // true
$validator->isValid($isbn13); // true

Attached propsal for Zend_Validate_Isbn.

$isbn13 = '9783866400016';
$isbn10 = '0306406152';

$oIsbn = new Zend_Validate_Isbn;
$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN13);

var_dump($oIsbn->isValid($isbn13)); // true
var_dump($oIsbn->isValid($isbn10)); // false

$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN10);

var_dump($oIsbn->isValid($isbn13)); // false
var_dump($oIsbn->isValid($isbn10)); // true

Attached new version of propsal (small changes).

require_once 'Zend/Filter/Digits.php';

$isbn13  = '9783866400016';
$isbn10  = '0306406152';
$isbn13s = '978-3-866-40001-6';
$isbn10s = '0-306-40615-2';

$oDigits = new Zend_Filter_Digits();
$isbn13sc = $oDigits->filter($isbn13s);
$isbn10sc = $oDigits->filter($isbn10s);

$oIsbn = new Zend_Validate_Isbn;
$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN13);

var_dump($oIsbn->isValid($isbn13)); // true
var_dump($oIsbn->isValid($isbn10)); // false
var_dump($oIsbn->isValid($isbn13s)); // false
var_dump($oIsbn->isValid($isbn10s)); // false
var_dump($oIsbn->isValid($isbn13sc)); // true
var_dump($oIsbn->isValid($isbn10sc)); // false

$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN10);

var_dump($oIsbn->isValid($isbn13)); // false
var_dump($oIsbn->isValid($isbn10)); // true
var_dump($oIsbn->isValid($isbn13s)); // false
var_dump($oIsbn->isValid($isbn10s)); // false
var_dump($oIsbn->isValid($isbn13sc)); // false
var_dump($oIsbn->isValid($isbn10sc)); // true

In Zend_Validate_Isbn_V3.php: It can test isbn numbers with valid string separators " " and "-" and the isbn can start wth "isbn" because it is a valid isbn and must not filtered to digits manuely. secondary it can test a isbn by all versions in one call.

I like that you implemented sth. to validate the isbn generally and not only ISBN-10 or ISBN-13.

I think we should split up filtering and validation. We can use filtering in validation but filtering should be in a seperate class (e.g. Zend_Filter_Isbn).

Assign to Darby.

There's a proposal related to this "issue", which can be found here.

Personally, I don't like to see ISBN-10 and ISBN-13 wrapped in one class, they should be 2 seperate validators if you ask me. I also think that these validators should go under the Zend_Validate_Barcode_* namespace.

Reassigning to [~andries].

ISBN-10 is now dead, the transition period ended on 1 Jan 2007.

ISBN-13 validation support is already available in ZF via the barcode validator as ISBN-13 is the same as a "bookland" EAN-13 barcode.

I think this ticket should be closed.

There is isbn validator at Zend Validate Barcode.