View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}


[Steven Brown|]

0.1 - 7 February 2008: Created.
0.2 - 9 February 2008: Added more algorithms due to community request

Zend_CheckDigit is a utility class to provide access to creation of check digits and validation using various check digit algorithms.

* [Check Digit Wikipedia Entry|]
* [Luhn Algorithm Wikipeda Entry|]
* [Verhoeff Algorithm Wikipeda Entry|]
* [Code 39 Wikipedia Entry|]
* [Code 128 Character Set|]
* [Code 128 Check Digit Algorithm|]

* This component *will* allow developers to generate check digits from a given number
* This component *will* allow developers to validate a given number (with the check digit included)
* This component *will* implement the Luhn algorithm
* This component *will* implement the Verhoeff algorithm
* This component *will* implement the Code 39 mod 43 barcode algorithm
* This component *will* implement the Code 128 mod 103 barcode algorithm
* This component *will* implement the ISBN-10 algorithm
* This component *will* implement the ISBN-13 algorithm
* This component *will* implement the EAN-13 algorithm
* This component *will* implement the UPC-A algorithm

Additional algorithms that might be considered include:
* Luhn Mod N
* EAN-8
* Other algorithms listed on the Wikipedia page

Some other notes:
* It may be possible to place this under Zend_Crypt as it is somewhat related
* Zend_Validate_Ccnum uses Luhn
* Zend_Validate_Barcode uses Luhn and has some crossover with the barcode algorithms
* May want to add Zend_Validate classes for each algorithm

* Zend_Exception

In the fashion of existing Zend Framework components a class might be created for each algorithm. The base Zend_CheckDigit class might have static functions to return the class for a specific algorithm.

It might be useful to create an interface for each algorithm with the following methods:

* generate($number) will return the check digit (int)
* append($number) will return the original number with the check digit added to the end
* isValid($number) will return a boolean, true if the check digit contained within $number is correct, false if it is not

It is assumed that the check digit will always be the last digit for isValid, unless the algorithm follows another structure. It would also be possible for append to return a formatted number for algorithms such as ISBN, although this might use a seperate format($number) method.

* Milestone 1: Design notes...whatever they are
* Milestone 2: Working prototype checked into the incubator supporting use cases
* Milestone 3: Unit tests exist, work, and are checked into SVN.
* Milestone 4: Initial documentation exists.

* Zend_CheckDigit (factory class)
* Zend_CheckDigit_Exception (maybe used when a number is not properly formatted?)
* Zend_CheckDigit_Luhn
* Zend_CheckDigit_Verhoeff
* Zend_CheckDigit_Code39
* Zend_CheckDigit_Code128
* Zend_CheckDigit_Isbn10
* Zend_CheckDigit_Isbn13
* Zend_CheckDigit_Ean13
* Zend_CheckDigit_Upc

It may be possible to merge ISBN-10 and ISBN-13 however this may cause issues with length validation. Code128 might need to be split into A, B and C though it should be possible to keep it as one and use the first character to determine with character set is being used. It may be necessary to specify UPC as UPC-A since other formats do exist but are apparently mostly redundant.

Additional classes may be added depending on algorithms that are implemented, such as:

* Zend_CheckDigit_Vin
* Zend_CheckDigit_LuhnModN
* Zend_CheckDigit_Ean8

Instantiate class:

$luhn = Zend_CheckDigit::luhn();
$verhoeff = Zend_CheckDigit::verhoeff();

Generate check digit:

$checkDigit = $luhn->generate(876); // returns 3
$fullNumber = $luhn->append(876); // returns 8763

Validate check digit:

$luhn->isValid(8763); // returns true
$luhn->isValid(1111); // returns false


class Zend_CheckDigit_Exception extends Zend_Exception {}

class Zend_CheckDigit {

public function luhn() {}

public function verhoeff() {}

public function code39() {}

public function code128() {}

public function isbn10() {}

public function isbn13() {}

public function ean13() {}

public function upc() {}


interface Zend_CheckDigit_Interface {

public function generate($number) {}

public function append($number) {}

public function isValid($number) {}


class Zend_CheckDigit_Luhn implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Verhoeff implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Code39 implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Code128 implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Isbn10 implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Isbn13 implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Ean13 implements Zend_CheckDigit_Interface {}

class Zend_CheckDigit_Upc implements Zend_CheckDigit_Interface {}