View Source

<ac:macro ac:name="info"><ac:parameter ac:name="title">Zend_Validate_Barcode_EAN13 - Andries Seutens</ac:parameter></ac:macro>

<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_Validate_Barcode_EAN13
{zone-data}

{zone-data:proposer-list}
[Andries Seutens|mailto:andries.seutens@skynet.be]
[Darby Felton|mailto:darby@zend.com], Zend liaison
{zone-data}

{zone-data:revision}
1.0 - Initial draft: 3 july 2007
{zone-data}

{zone-data:overview}
Zend_Validate_Barcode_EAN13 is a simple component that validates the validity of an EAN13 barcode.
{zone-data}

{zone-data:references}
* [EAN13 Wikipedia Entry|http://en.wikipedia.org/wiki/European_Article_Number]
{zone-data}

{zone-data:requirements}
* This component *will* validate the validity of an EAN13 barcode.
* This component *will* calculate the proper checksums.
* This component *will not* validate a barcode from an *image*.
{zone-data}

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

{zone-data:operation}
The component is instantiated with a mind-link that ... it will rock the planet.
{zone-data}

{zone-data:milestones}
* Milestone 1: \[DONE\] write proposal
* Milestone 2: get the proposal approved
* Milestone 3: working prototype checked into the incubator
* Milestone 4: write unit test, and check into SVN
* Milestone 5: write documentation for this validator.
* Milestone 5: introduce "component" to core.
{zone-data}

{zone-data:class-list}
* Zend_Validate_Barcode_EAN13
{zone-data}

{zone-data:use-cases}
||UC-01: validating barcode||
{code:php}
$barcode = new Zend_Validate_Barcode_EAN13;
$result = $barcode->isValid('5701291191822');

var_dump($result); // boolean true
var_dump($barcode->getMessages()); // array empty
{code}

||UC-02: retrieving invalid length message||
{code:php}
$barcode = new Zend_Validate_Barcode_EAN13;
$result = $barcode->isValid('12345');

var_dump($result); // boolean false
var_dump($barcode->getMessages()); // array 0 => string ''12345' should be 13 characters' (length=31)
{code}

||UC-03: retrieving invalid barcode message||
{code:php}
$barcode = new Zend_Validate_Barcode_EAN13;
$result = $barcode->isValid('5701291191828');

var_dump($result); // boolean false
var_dump($barcode->getMessages()); // 0 => string ''5701291191828' is an invalid EAN13 barcode' (length=43)
{code}

{zone-data}

{zone-data:skeletons}
{code:php}
<?php

/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/


/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';


/**
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_Barcode_EAN13 extends Zend_Validate_Abstract
{
/**
* Validation failure message key for when the value is
* an invalid barcode
*/
const INVALID = 'stringInvalid';

/**
* Validation failure message key for when the value is
* not 13 characters long
*/
const INVALID_LENGTH = 'stringLength';

/**
* Validation failure message key for when the value is
* an empty string
*/
const STRING_EMPTY = 'stringEmpty';

/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = array(
self::INVALID => "'%value%' is an invalid EAN13 barcode",
self::INVALID_LENGTH => "'%value%' should be 13 characters",
self::STRING_EMPTY => "'%value%' is an empty string"
);

/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if $value contains a valid barcode
*
* @param string $value
* @return boolean
*/
public function isValid($value)
{
$valueString = (string) $value;
$this->_setValue($valueString);

if ('' === $valueString) {
$this->_error(self::STRING_EMPTY);
return false;
}

if (strlen($valueString) !== 13) {
$this->_error(self::INVALID_LENGTH);
return false;
}

$barcode = strrev(substr($valueString, 0, -1));
$oddSum = 0;
$evenSum = 0;

for ($i = 0; $i < 12; $i++) {
if ($i % 2 === 0) {
$oddSum += $barcode[$i] * 3;
} elseif ($i % 2 === 1) {
$evenSum += $barcode[$i];
}
}

$calculation = ($oddSum + $evenSum) % 10;
$checksum = ($calculation === 0) ? 0 : 10-$calculation;

if ($valueString[12] != $checksum) {
$this->_error(self::INVALID);
return false;
}

return true;
}
}

{code}
{zone-data}

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