From c53d8d026021f97075fb2f4940ba22793c38fb6e Mon Sep 17 00:00:00 2001 From: davehauenstein Date: Wed, 15 Apr 2009 22:06:42 +0000 Subject: added toolbar; functionality includes refresh button to get back to original page, print article, email a link to the article with a personal note git-svn-id: http://arc90labs-readability.googlecode.com/svn/trunk@31 d4e419ec-0920-11de-bbfd-a7c1bc4c261e --- lib/Zend/Validate/Barcode/Ean13.php | 112 ++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 lib/Zend/Validate/Barcode/Ean13.php (limited to 'lib/Zend/Validate/Barcode/Ean13.php') diff --git a/lib/Zend/Validate/Barcode/Ean13.php b/lib/Zend/Validate/Barcode/Ean13.php new file mode 100644 index 0000000..393091d --- /dev/null +++ b/lib/Zend/Validate/Barcode/Ean13.php @@ -0,0 +1,112 @@ + "'%value%' is an invalid EAN-13 barcode", + self::INVALID_LENGTH => "'%value%' should be 13 characters", + self::NOT_NUMERIC => "'%value%' should contain only numeric characters", + ); + + /** + * 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) + { + if (false === ctype_digit($value)) { + $this->_error(self::NOT_NUMERIC); + return false; + } + + $valueString = (string) $value; + $this->_setValue($valueString); + + 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; + } +} -- cgit v1.2.3