View Source

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

{zone-data:component-name}
Zend_CheckDigit
{zone-data}

{zone-data:proposer-list}
[Steven Brown|mailto:steven@newbeachmedia.com]
{zone-data}

{zone-data:revision}
0.1 - 7 February 2008: Created.
0.2 - 9 February 2008: Added more algorithms due to community request
{zone-data}

{zone-data:overview}
Zend_CheckDigit is a utility class to provide access to creation of check digits and validation using various check digit algorithms.
{zone-data}

{zone-data:references}
* [Check Digit Wikipedia Entry|http://en.wikipedia.org/wiki/Check_digit]
* [Luhn Algorithm Wikipeda Entry|http://en.wikipedia.org/wiki/Luhn_algorithm]
* [Verhoeff Algorithm Wikipeda Entry|http://en.wikipedia.org/wiki/Verhoeff_algorithm]
* [Code 39 Wikipedia Entry|http://en.wikipedia.org/wiki/Code_39]
* [Code 128 Character Set|http://www.code128barcodes.com/Code-128-character-set.html]
* [Code 128 Check Digit Algorithm|http://www.code128barcodes.com/code-128-check-digit.html]
{zone-data}

{zone-data:requirements}
* 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:
* VIN
* 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
{zone-data}

{zone-data:dependencies}
* Zend_Exception
{zone-data}

{zone-data:operation}
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.
{zone-data}

{zone-data:milestones}
* 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.
{zone-data}

{zone-data:class-list}
* 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
{zone-data}

{zone-data:use-cases}
Instantiate class:

{code}
$luhn = Zend_CheckDigit::luhn();
$verhoeff = Zend_CheckDigit::verhoeff();
{code}

Generate check digit:

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

Validate check digit:

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

{zone-data}

{zone-data:skeletons}
{code}
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 {}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>