2
0
Quellcode durchsuchen

[GENERIC] Zend_Validate_Barcode:

- promote new component to core
- supports 14 different barcode definitions
- prepared for 2D and 3D barcodes
- new manual chapter
- allows simple usage of self defined barcode definitions
- base for new Zend_Barcode component

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19692 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas vor 16 Jahren
Ursprung
Commit
aed05dd167

+ 349 - 0
documentation/manual/en/module_specs/Zend_Validate-Barcode.xml

@@ -0,0 +1,349 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<sect2 id="zend.validate.set.barcode">
+    <title>Barcode</title>
+
+    <para>
+        <classname>Zend_Validate_Barcode</classname> allows you to check if a given value can be
+        represented as barcode.
+    </para>
+
+    <para>
+        <classname>Zend_Validate_Barcode</classname> supports multiple barcode standards and can be
+        extended with proprietary barcode implementations very easily. The following barcode
+        standards are supported:
+    </para>
+
+    <itemizedlist>
+        <listitem>
+            <para>
+                <emphasis>CODE25</emphasis>: Often called "two of five."
+            </para>
+
+            <para>
+                This barcode has no length limitation, but it must contain an even amount of
+                characters. It supports only digits, and the last digit can be an optional checksum
+                which is calculated with modulo 10. This standard is used worldwide and common on
+                the market.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>CODE39</emphasis>: CODE39 is one of the oldest available codes.
+            </para>
+
+            <para>
+                This barcode has a variable length. It supports digits, alphabetical characters
+                and 7 special characters. It can have an optional checksum which is calculated with
+                modulo 43. This standard is used worldwide and common within the industry.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>CODE93</emphasis>: CODE93 is the successor of CODE39.
+            </para>
+
+            <para>
+                This barcode has a variable length. It supports digits, alphabetical characters
+                and 7 special characters. It has an checksum which is calculated with modulo 47 and
+                contains 2 characters. This standard produces a denser code than CODE39 and is more
+                secure.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN8</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                These barcodes must have a length of 8 characters. It supports only digits, and
+                the last digit is always a checksum. This standard is used worldwide but has a very
+                limited range. It can be found on small articles where a longer barcode could not
+                be printed.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN12</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                This barcode must have a length of 12 characters. It supports only digits, and the
+                last digit is always a checksum which is calculated with modulo 10. This standard is
+                used within the USA and common on the market. It has been superceded by EAN13.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN13</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                This barcode must have a length of 13 characters. It supports only digits, and the
+                last digit is always a checksum which is calculated with modulo 10. This standard is
+                used worldwide and common on the market.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN14</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                This barcode must have a length of 14 characters. It supports only digits, and the
+                last digit is always a checksum which is calculated with modulo 10. This standard is
+                used worldwide and common on the market. It is the successor for EAN13.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>GTIN12</emphasis>: GTIN is the shortcut for "Global Trade Item Number".
+            </para>
+
+            <para>
+                This barcode uses the same standard as EAN12 and is its successor. It's commonly
+                used within the USA.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>GTIN13</emphasis>: GTIN is the shortcut for "Global Trade Item Number".
+            </para>
+
+            <para>
+                This barcode uses the same standard as EAN13 and is its successor. It is used
+                worldwide by industry.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>GTIN14</emphasis>: GTIN is the shortcut for "Global Trade Item Number".
+            </para>
+
+            <para>
+                This barcode uses the same standard as EAN14 and is its successor. It is used
+                worldwide and common on the market.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>ITF14</emphasis>: ITF14 is the GS1 implementation of an Interleaved Two
+                of Five bar code.
+            </para>
+
+            <para>
+                This barcode is a special variant of Interleaved 2 of 5. It must have a length of
+                14 characters and is based on GTIN14. It supports only digits, and the last digit
+                must be a checksum digit which is calculated with modulo 10. It is used worldwide
+                and common within the market.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>SSCC</emphasis>: SSCC is the shortcut for "Serial Shipping Container
+                Code".
+            </para>
+
+            <para>
+                This barcode is a variant of EAN barcode. It must have a length of 18 characters and
+                supports only digits. The last digit must be a checksum digit which is calculated
+                with modulo 10. It is commonly used by the transport industry.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>UPCA</emphasis>: UPC is the shortcut for "Univeral Product Code".
+            </para>
+
+            <para>
+                This barcode preceeded EAN13. It must have a length of 12 characters and supports
+                only digits. The last digit must be a checksum digit which is calculated with
+                modulo 10. It is commonly used within the USA.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>UPCE</emphasis>: UPCE is the short variant from UPCA.
+            </para>
+
+            <para>
+                This barcode is a smaller variant of UPCA. It must have a length of 6 characters and
+                supports only digits. There is no checksum within this barcode. It is commonly used
+                with small products where a UPCA barcode would not fit.
+            </para>
+        </listitem>
+    </itemizedlist>
+
+    <sect3 id="zend.validate.set.barcode.basic">
+        <title>Basic usage</title>
+
+        <para>
+            To validate if a given string is a barcode you just need to know its type.  See the
+            following example for an EAN13 barcode:
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$valid = new Zend_Validate_Barcode('EAN13');
+if ($valid->isValid($input)) {
+    // input appears to be valid
+} else {
+    // input is invalid
+}
+]]></programlisting>
+    </sect3>
+
+    <sect3 id="zend.validate.set.barcode.checksum">
+        <title>Optional checksum</title>
+
+        <para>
+            Some barcodes can be provided with an optional checksum. These barcodes would be valid
+            even without checksum. Still, when you provide a checksum, then you should also validate
+            it. By default, these barcode types perform no checksum validation. By using the
+            <property>checksum</property> option you can define if the checksum will be validated or
+            ignored.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$valid = new Zend_Validate_Barcode(array(
+    'adapter'  => 'EAN13',
+    'checksum' => false,
+));
+if ($valid->isValid($input)) {
+    // input appears to be valid
+} else {
+    // input is invalid
+}
+]]></programlisting>
+
+        <note>
+            <title>Reduced security by disabling checksum validation</title>
+
+            <para>
+                 By switching off checksum validation you will also reduce the security of the used
+                 barcodes. Additionally you should note that you can also turn off the checksum
+                 validation for those barcode types which must contain a checksum value. Barcodes
+                 which would not be valid could then be returned as valid even if they are not.
+            </para>
+        </note>
+    </sect3>
+
+    <sect3 id="zend.validate.set.barcode.custom">
+        <title>Writing custom adapters</title>
+
+        <para>
+            You may write custom barcode validators for usage with
+            <classname>Zend_Validate_Barcode</classname>; this is often necessary when dealing with
+            proprietary barcode types.  To write your own barcode validator, you need the following
+            information.
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Length</emphasis>: The length your barcode must have. It can have one
+                    of the following values:
+                </para>
+
+                <itemizedlist>
+                    <listitem>
+                        <para>
+                            <emphasis>Integer</emphasis>: A value greater 0, which means that the
+                            barcode must have this length.
+                        </para>
+                    </listitem>
+
+                    <listitem>
+                        <para>
+                            <emphasis>-1</emphasis>: There is no limitation for the length of this
+                            barcode.
+                        </para>
+                    </listitem>
+
+                    <listitem>
+                        <para>
+                            <emphasis>"even"</emphasis>: The length of this barcode must have a
+                            even amount of digits.
+                        </para>
+                    </listitem>
+
+                    <listitem>
+                        <para>
+                            <emphasis>"odd"</emphasis>: The length of this barcode must have a
+                            odd amount of digits.
+                        </para>
+                    </listitem>
+
+                    <listitem>
+                        <para>
+                            <emphasis>array</emphasis>: An array of integer values. The length of
+                            this barcode must have one of the set array values.
+                        </para>
+                    </listitem>
+                </itemizedlist>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Characters</emphasis>: A string which contains all allowed characters
+                    for this barcode. Also the integer value 128 is allowed, which means the first
+                    128 characters of the ASCII table.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Checksum</emphasis>: A string which will be used as callback for a
+                    method which does the checksum validation.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            Your custom barcode validator must extend
+            <classname>Zend_Validate_Barcode_AdapterAbstract</classname> or implement
+            <interface>Zend_Validate_Barcode_AdapterInterface</interface>.
+        </para>
+
+        <para>
+            As an example, let's create a validator that expects an even number of characters that
+            include all digits and the letters 'ABCDE', and which requires a checksum.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+class My_Barcode_MyBar extends Zend_Validate_Barcode_AdapterAbstract
+{
+    protected $_length     = 'even';
+    protected $_characters = '0123456789ABCDE';
+    protected $_checksum   = '_mod66';
+
+    protected function _mod66($barcode)
+    {
+        // do some validations and return a boolean
+    }
+}
+
+$valid = new Zend_Validate_Barcode('My_Barcode_MyBar');
+if ($valid->isValid($input)) {
+    // input appears to be valid
+} else {
+    // input is invalid
+}
+]]></programlisting>
+    </sect3>
+</sect2>
+<!--
+vim:se ts=4 sw=4 et:
+-->

+ 1 - 12
documentation/manual/en/module_specs/Zend_Validate-Set.xml

@@ -34,18 +34,7 @@
         </para>
     </sect2>
 
-    <sect2 id="zend.validate.set.barcode">
-        <title>Barcode</title>
-        <para>
-            This validator is instantiated with a barcode type against which you wish to validate a
-            barcode value. It currently supports "UPC-A" (Universal Product Code) and
-            "EAN-13" (European Article Number) barcode types, and the
-            <methodname>isValid()</methodname> method returns true if and only if the input successfully
-            validates against the barcode validation algorithm. You should remove all characters
-            other than the digits zero through nine (0-9) from the input value before passing it on
-            to the validator.
-        </para>
-    </sect2>
+    <xi:include href="Zend_Validate-Barcode.xml" />
 
     <sect2 id="zend.validate.set.between">
         <title>Between</title>

+ 143 - 31
library/Zend/Validate/Barcode.php

@@ -25,6 +25,11 @@
 require_once 'Zend/Validate/Abstract.php';
 
 /**
+ * @see Zend_Loader
+ */
+require_once 'Zend/Loader.php';
+
+/**
  * @category   Zend
  * @package    Zend_Validate
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -32,62 +37,141 @@ require_once 'Zend/Validate/Abstract.php';
  */
 class Zend_Validate_Barcode extends Zend_Validate_Abstract
 {
+    const INVALID        = 'barcodeInvalid';
+    const FAILED         = 'barcodeFailed';
+    const INVALID_CHARS  = 'barcodeInvalidChars';
+    const INVALID_LENGTH = 'barcodeInvalidLength';
+
+    protected $_messageTemplates = array(
+        self::FAILED         => "'%value%' failed checksum validation",
+        self::INVALID_CHARS  => "'%value%' contains invalid characters",
+        self::INVALID_LENGTH => "'%value%' should be %length% characters",
+        self::INVALID        => "Invalid type given, value should be string",
+    );
+
+    /**
+     * Additional variables available for validation failure messages
+     *
+     * @var array
+     */
+    protected $_messageVariables = array(
+        'length' => '_length'
+    );
+
     /**
-     * Barcode validator
+     * Length for the set subtype
      *
-     * @var Zend_Validate_Abstract
+     * @var integer
      */
-    protected $_barcodeValidator;
+    protected $_length;
+
+    /**
+     * Barcode adapter
+     *
+     * @var Zend_Validate_Barcode_BarcodeAdapter
+     */
+    protected $_adapter;
 
     /**
      * Generates the standard validator object
      *
-     * @param  string|Zend_Config $barcode Barcode validator to use
+     * @param  string|Zend_Config|
+     *         Zend_Validate_Barcode_BarcodeAdapter $adapter Barcode adapter to use
      * @return void
      * @throws Zend_Validate_Exception
      */
-    public function __construct($barcode)
+    public function __construct($adapter)
     {
-        if ($barcode instanceof Zend_Config) {
-            $barcode = $barcode->toArray();
-            if (array_key_exists('barcode', $barcode)) {
-                $barcode = $barcode['barcode'];
+        if ($adapter instanceof Zend_Config) {
+            $adapter = $adapter->toArray();
+        }
+
+        $options  = null;
+        $checksum = null;
+        if (is_array($adapter)) {
+            if (array_key_exists('options', $adapter)) {
+                $options = $adapter['options'];
+            }
+
+            if (array_key_exists('checksum', $adapter)) {
+                $checksum = $adapter['checksum'];
+            }
+
+            if (array_key_exists('adapter', $adapter)) {
+                $adapter = $adapter['adapter'];
             } else {
                 require_once 'Zend/Validate/Exception.php';
-                throw new Zend_Validate_Exception("Missing option 'barcode'");
+                throw new Zend_Validate_Exception("Missing option 'adapter'");
             }
         }
 
-        $this->setType($barcode);
+        $this->setAdapter($adapter, $options);
+        if ($checksum !== null) {
+            $this->setChecksum($checksum);
+        }
     }
 
     /**
-     * Sets a new barcode validator
+     * Returns the set adapter
      *
-     * @param  string $barcode - Barcode validator to use
+     * @return Zend_Validate_Barcode_BarcodeAdapter
+     */
+    public function getAdapter()
+    {
+        return $this->_adapter;
+    }
+
+    /**
+     * Sets a new barcode adapter
+     *
+     * @param  string|Zend_Validate_Barcode $adapter Barcode adapter to use
+     * @param  array  $options Options for this adapter
      * @return void
      * @throws Zend_Validate_Exception
      */
-    public function setType($barcode)
+    public function setAdapter($adapter, $options = null)
     {
-        switch (strtolower($barcode)) {
-            case 'upc':
-            case 'upc-a':
-                require_once 'Zend/Validate/Barcode/UpcA.php';
-                $class = 'Zend_Validate_Barcode_UpcA';
-                break;
-            case 'ean13':
-            case 'ean-13':
-                require_once 'Zend/Validate/Barcode/Ean13.php';
-                $class = 'Zend_Validate_Barcode_Ean13';
-                break;
-            default:
-                require_once 'Zend/Validate/Exception.php';
-                throw new Zend_Validate_Exception("Barcode type '$barcode' is not supported'");
-                break;
+        $adapter = ucfirst($adapter);
+        require_once 'Zend/Loader.php';
+        if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
+            $adapter = 'Zend_Validate_Barcode_' . $adapter;
         }
 
-        $this->_barcodeValidator = new $class;
+        if (!class_exists($adapter)) {
+            Zend_Loader::loadClass($adapter);
+        }
+
+        $this->_adapter = new $adapter($options);
+        if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
+            require_once 'Zend/Validate/Exception.php';
+            throw new Zend_Validate_Exception(
+                "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
+            );
+        }
+
+        return $this;
+    }
+
+    /**
+     * Returns the checksum option
+     *
+     * @return boolean
+     */
+    public function getChecksum()
+    {
+        return $this->getAdapter()->getCheck();
+    }
+
+    /**
+     * Sets the checksum option
+     *
+     * @param  boolean $checksum
+     * @return Zend_Validate_Barcode
+     */
+    public function setChecksum($checksum)
+    {
+        $this->getAdapter()->setCheck($checksum);
+        return $this;
     }
 
     /**
@@ -100,6 +184,34 @@ class Zend_Validate_Barcode extends Zend_Validate_Abstract
      */
     public function isValid($value)
     {
-        return call_user_func(array($this->_barcodeValidator, 'isValid'), $value);
+        if (!is_string($value)) {
+            $this->_error(self::INVALID);
+            return false;
+        }
+
+        $this->_value  = (string) $value;
+        $this->_length = strlen($value);
+        $adapter = $this->getAdapter();
+        $result  = $adapter->checkLength($value);
+        if (!$result) {
+            $this->_error(self::INVALID_LENGTH);
+            return false;
+        }
+
+        $result = $adapter->checkChars($value);
+        if (!$result) {
+            $this->_error(self::INVALID_CHARS);
+            return false;
+        }
+
+        if ($this->getChecksum()) {
+            $result = $adapter->checksum($value);
+            if (!$result) {
+                $this->_error(self::FAILED);
+                return false;
+            }
+        }
+
+        return true;
     }
 }

+ 228 - 0
library/Zend/Validate/Barcode/AdapterAbstract.php

@@ -0,0 +1,228 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterInterface
+ */
+require_once 'Zend/Validate/Barcode/AdapterInterface.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+abstract class Zend_Validate_Barcode_AdapterAbstract
+    implements Zend_Validate_Barcode_AdapterInterface
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer|array|string
+     */
+    protected $_length;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters;
+
+    /**
+     * Callback to checksum function
+     * @var string|array
+     */
+    protected $_checksum;
+
+    /**
+     * Is a checksum value included?
+     * @var boolean
+     */
+    protected $_hasChecksum = true;
+
+    /**
+     * Checks the length of a barcode
+     *
+     * @param  string $value The barcode to check for proper length
+     * @return boolean
+     */
+    public function checkLength($value)
+    {
+        if (!is_string($value)) {
+            return false;
+        }
+
+        $fixum  = strlen($value);
+        $found  = false;
+        $length = $this->getLength();
+        if (is_array($length)) {
+            foreach ($length as $value) {
+                if ($fixum == $value) {
+                    $found = true;
+                }
+
+                if ($value == -1) {
+                    $found = true;
+                }
+            }
+        } elseif ($fixum == $length) {
+            $found = true;
+        } elseif ($length == -1) {
+            $found = true;
+        } elseif ($length == 'even') {
+            $count = $fixum % 2;
+            $found = ($count == 0) ? true : false;
+        } elseif ($length == 'odd') {
+            $count = $fixum % 2;
+            $found = ($count == 1) ? true : false;
+        }
+
+        return $found;
+    }
+
+    /**
+     * Checks for allowed characters within the barcode
+     *
+     * @param  string $value The barcode to check for allowed characters
+     * @return boolean
+     */
+    public function checkChars($value)
+    {
+        if (!is_string($value)) {
+            return false;
+        }
+
+        $characters = $this->getCharacters();
+        if ($characters == 128) {
+            for ($x = 0; $x < 128; ++$x) {
+                $value = strtr($value, chr($x), '');
+            }
+        } else {
+            $chars = str_split($characters);
+            foreach ($chars as $char) {
+                $value = str_replace($char, '', $value);
+            }
+        }
+
+        if (strlen($value) > 0) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Validates the checksum
+     *
+     * @param  string $value The barcode to check the checksum for
+     * @return boolean
+     */
+    public function checksum($value)
+    {
+        $checksum = $this->getChecksum();
+        if (!empty($checksum)) {
+            if (method_exists($this, $checksum)) {
+                return call_user_func_array(array($this, $checksum), $value);
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Returns the allowed barcode length
+     *
+     * @return string
+     */
+    public function getLength()
+    {
+        return $this->_length;
+    }
+
+    /**
+     * Returns the allowed characters
+     *
+     * @return integer|string
+     */
+    public function getCharacters()
+    {
+        return $this->_characters;
+    }
+
+    /**
+     * Returns the checksum function name
+     *
+     */
+    public function getChecksum()
+    {
+        return $this->_checksum;
+    }
+
+    /**
+     * Returns if barcode uses checksum
+     *
+     * @return boolean
+     */
+    public function getCheck()
+    {
+        return $this->_hasChecksum;
+    }
+
+    /**
+     * Sets the checksum validation
+     *
+     * @param  boolean $check
+     * @return Zend_Validate_Barcode_AdapterAbstract
+     */
+    public function setCheck($check)
+    {
+        $this->_hasChecksum = (boolean) $check;
+        return $this;
+    }
+
+    /**
+     * Validates the checksum (Modulo 10)
+     *
+     * @param  string $value The barcode to validate
+     * @return boolean
+     */
+    protected function _mod10($value)
+    {
+        $barcode = substr($value, 0, -1);
+        $sum     = 0;
+        $length  = strlen($value) - 2;
+
+        for ($i = 0; $i < $length; $i++) {
+            if (($i % 2) === 0) {
+                $sum += $barcode[$length - $i] * 3;
+            } else {
+                $sum += $barcode[$length - $i];
+            }
+        }
+
+        $calc     = $sum % 10;
+        $checksum = ($calc === 0) ? 0 : (10 - $calc);
+        if ($value[$length + 1] != $checksum) {
+            return false;
+        }
+
+        return true;
+    }
+}

+ 68 - 0
library/Zend/Validate/Barcode/AdapterInterface.php

@@ -0,0 +1,68 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+interface Zend_Validate_Barcode_AdapterInterface
+{
+    /**
+     * Checks the length of a barcode
+     *
+     * @param  string $value  The barcode to check for proper length
+     * @return boolean
+     */
+    public function checkLength($value);
+
+    /**
+     * Checks for allowed characters within the barcode
+     *
+     * @param  string $value The barcode to check for allowed characters
+     * @return boolean
+     */
+    public function checkChars($value);
+
+    /**
+     * Validates the checksum
+     *
+     * @param string $value The barcode to check the checksum for
+     * @return boolean
+     */
+    public function checksum($value);
+
+    /**
+     * Returns if barcode uses a checksum
+     *
+     * @return boolean
+     */
+    public function getCheck();
+
+    /**
+     * Sets the checksum validation
+     *
+     * @param  boolean $check
+     * @return Zend_Validate_Barcode_Adapter Provides fluid interface
+     */
+    public function setCheck($check);
+}

+ 64 - 0
library/Zend/Validate/Barcode/Code25.php

@@ -0,0 +1,64 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:$
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Code25 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 'even';
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+
+    /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     * 
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

+ 88 - 0
library/Zend/Validate/Barcode/Code39.php

@@ -0,0 +1,88 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:$
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Code39 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = -1;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod43';
+
+    /**
+     * @var array
+     */
+    protected $_check = array(
+        '0' =>  0, '1' =>  1, '2' =>  2, '3' =>  3, '4' =>  4, '5' =>  5, '6' =>  6,
+        '7' =>  7, '8' =>  8, '9' =>  9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
+        'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
+        'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
+        'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
+        'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
+        '%' => 42,
+    );
+
+    /**
+     * Validates the checksum (Modulo 43)
+     *
+     * @param  string $value The barcode to validate
+     * @return boolean
+     */
+    protected function _mod43($value)
+    {
+        $checksum = substr($value, -1, 1);
+        $value    = str_split(substr($value, 0, -1));
+        $count    = 0;
+        foreach($value as $char) {
+            $count += $this->_check[$char];
+        }
+
+        $mod = $count % 43;
+        if ($mod == $this->_check[$checksum]) {
+            return true;
+        }
+
+        return false;
+    }
+}

+ 108 - 0
library/Zend/Validate/Barcode/Code93.php

@@ -0,0 +1,108 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:$
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Code93 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = -1;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_modCK';
+
+    /**
+     * Note that the characters !"§& are only synonyms
+     * @var array
+     */
+    protected $_check = array(
+        '0' =>  0, '1' =>  1, '2' =>  2, '3' =>  3, '4' =>  4, '5' =>  5, '6' =>  6,
+        '7' =>  7, '8' =>  8, '9' =>  9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
+        'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
+        'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
+        'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
+        'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
+        '%' => 42, '!' => 43, '"' => 44, '§' => 45, '&' => 46,
+    );
+
+    /**
+     * Validates the checksum (Modulo CK)
+     *
+     * @param  string $value The barcode to validate
+     * @return boolean
+     */
+    protected function _modCK($value)
+    {
+        $checksum = substr($value, -2, 2);
+        $value    = str_split(substr($value, 0, -2));
+        $count    = 0;
+        $length   = count($value) % 20;
+        foreach($value as $char) {
+            if ($length == 0) {
+                $length = 20;
+            }
+
+            $count += $this->_check[$char] * $length;
+            --$length;
+        }
+
+        $check   = array_search(($count % 47), $this->_check);
+        $value[] = $check;
+        $count   = 0;
+        $length  = count($value) % 15;
+        foreach($value as $char) {
+            if ($length == 0) {
+                $length = 15;
+            }
+
+            $count += $this->_check[$char] * $length;
+            --$length;
+        }
+        $check .= array_search(($count % 47), $this->_check);
+
+        if ($check == $checksum) {
+            return true;
+        }
+
+        return false;
+    }
+}

+ 52 - 0
library/Zend/Validate/Barcode/Ean12.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Ean12 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 12;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 12 - 67
library/Zend/Validate/Barcode/Ean13.php

@@ -20,9 +20,9 @@
  */
 
 /**
- * @see Zend_Validate_Abstract
+ * @see Zend_Validate_Barcode_AdapterAbstract
  */
-require_once 'Zend/Validate/Abstract.php';
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
 
 /**
  * @category   Zend
@@ -30,78 +30,23 @@ require_once 'Zend/Validate/Abstract.php';
  * @copyright  Copyright (c) 2005-2009 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
+class Zend_Validate_Barcode_Ean13 extends Zend_Validate_Barcode_AdapterAbstract
 {
     /**
-     * Validation failure message key for when the value is
-     * an invalid barcode
+     * Allowed barcode lengths
+     * @var integer
      */
-    const INVALID = 'ean13Invalid';
+    protected $_length = 13;
 
     /**
-     * Validation failure message key for when the value is
-     * not 13 characters long
+     * Allowed barcode characters
+     * @var string
      */
-    const INVALID_LENGTH = 'ean13InvalidLength';
+    protected $_characters = '0123456789';
 
     /**
-     * Validation failure message key for when the value
-     * does not only contain numeric characters
+     * Checksum function
+     * @var string
      */
-    const NOT_NUMERIC = 'ean13NotNumeric';
-
-    /**
-     * Validation failure message template definitions
-     *
-     * @var array
-     */
-    protected $_messageTemplates = array(
-        self::INVALID        => "'%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 (!is_string($value) || !ctype_digit($value)) {
-            $this->_error(self::NOT_NUMERIC);
-            return false;
-        }
-
-        $this->_setValue($value);
-        if (strlen($value) !== 13) {
-            $this->_error(self::INVALID_LENGTH);
-            return false;
-        }
-
-        $barcode = strrev(substr($value, 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 ($value[12] != $checksum) {
-            $this->_error(self::INVALID);
-            return false;
-        }
-
-        return true;
-    }
+    protected $_checksum = '_mod10';
 }

+ 52 - 0
library/Zend/Validate/Barcode/Ean14.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Ean14 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 14;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Ean8.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:$
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Ean8 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 8;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Gtin12.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Gtin12 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 12;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Gtin13.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Gtin13 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 13;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Gtin14.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Gtin14 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 14;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Itf14.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Itf14 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 14;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Sscc.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Ean13.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Sscc extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 18;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Upca.php

@@ -0,0 +1,52 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Upca extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 12;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+}

+ 64 - 0
library/Zend/Validate/Barcode/Upce.php

@@ -0,0 +1,64 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: UpcA.php 18028 2009-09-08 20:52:23Z thomas $
+ */
+
+/**
+ * @see Zend_Validate_Barcode_AdapterAbstract
+ */
+require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Validate
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Validate_Barcode_Upce extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 6;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Checksum function
+     * @var string
+     */
+    protected $_checksum = '_mod10';
+
+    /**
+     * Constructor
+     *
+     * Set check flag to false
+     * 
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

+ 211 - 25
tests/Zend/Validate/BarcodeTest.php

@@ -41,24 +41,6 @@ require_once 'Zend/Validate/Barcode.php';
  */
 class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
 {
-    public function testUpcA()
-    {
-        $barcode = new Zend_Validate_Barcode('upc-a');
-
-        $this->assertTrue($barcode->isValid('065100004327'));
-        $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('065100004328'));
-    }
-
-    public function testEan13()
-    {
-        $barcode = new Zend_Validate_Barcode('ean-13');
-
-        $this->assertTrue($barcode->isValid('0075678164125'));
-        $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('0075678164124'));
-    }
-
     /**
      * Test if EAN-13 contains only numeric characters
      *
@@ -66,7 +48,7 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
      */
     public function testEan13ContainsOnlyNumeric()
     {
-        $barcode = new Zend_Validate_Barcode('ean-13');
+        $barcode = new Zend_Validate_Barcode('ean13');
         $this->assertFalse($barcode->isValid('3RH1131-1BB40'));
     }
 
@@ -76,16 +58,16 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
             $barcode = new Zend_Validate_Barcode('Zend');
             $this->fail("'Zend' is not a valid barcode type'");
         } catch (Exception $e) {
-            $this->assertContains("'Zend' is not supported", $e->getMessage());
+            $this->assertContains("No such file", $e->getMessage());
         }
     }
 
-    public function testSetType()
+    public function testSetAdapter()
     {
-        $barcode = new Zend_Validate_Barcode('upc-a');
+        $barcode = new Zend_Validate_Barcode('upca');
         $this->assertTrue($barcode->isValid('065100004327'));
 
-        $barcode->setType('ean-13');
+        $barcode->setAdapter('ean13');
         $this->assertTrue($barcode->isValid('0075678164125'));
     }
 
@@ -94,12 +76,216 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
      */
     public function testNonStringValidation()
     {
-        $barcode = new Zend_Validate_Barcode('upc-a');
+        $barcode = new Zend_Validate_Barcode('upca');
         $this->assertFalse($barcode->isValid(106510000.4327));
         $this->assertFalse($barcode->isValid(array('065100004327')));
 
-        $barcode = new Zend_Validate_Barcode('ean-13');
+        $barcode = new Zend_Validate_Barcode('ean13');
         $this->assertFalse($barcode->isValid(06510000.4327));
         $this->assertFalse($barcode->isValid(array('065100004327')));
     }
+
+    public function testInvalidChecksumAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode1');
+        $this->assertFalse($barcode->isValid('0000000'));
+        $this->assertTrue(array_key_exists('barcodeFailed', $barcode->getMessages()));
+        $this->assertFalse($barcode->getAdapter()->checksum('0000000'));
+    }
+
+    public function testInvalidCharAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode1');
+        $this->assertFalse($barcode->getAdapter()->checkChars(123));
+    }
+
+    public function testAscii128CharacterAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode2');
+        $this->assertFalse($barcode->getAdapter()->checkChars('1234QW!"'));
+    }
+
+    public function testInvalidLengthAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode2');
+        $this->assertFalse($barcode->getAdapter()->checkLength(123));
+    }
+
+    public function testArrayLengthAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode2');
+        $this->assertTrue($barcode->getAdapter()->checkLength('1'));
+        $this->assertFalse($barcode->getAdapter()->checkLength('12'));
+        $this->assertTrue($barcode->getAdapter()->checkLength('123'));
+        $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
+    }
+
+    public function testArrayLengthAdapter2()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode3.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode3');
+        $this->assertTrue($barcode->getAdapter()->checkLength('1'));
+        $this->assertTrue($barcode->getAdapter()->checkLength('12'));
+        $this->assertTrue($barcode->getAdapter()->checkLength('123'));
+        $this->assertTrue($barcode->getAdapter()->checkLength('1234'));
+    }
+
+    public function testOddLengthAdapter()
+    {
+        require_once dirname(__FILE__) . "/_files/MyBarcode4.php";
+        $barcode = new Zend_Validate_Barcode('MyBarcode4');
+        $this->assertTrue($barcode->getAdapter()->checkLength('1'));
+        $this->assertFalse($barcode->getAdapter()->checkLength('12'));
+        $this->assertTrue($barcode->getAdapter()->checkLength('123'));
+        $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
+    }
+
+    public function testInvalidAdapter()
+    {
+        $barcode = new Zend_Validate_Barcode('Ean13');
+        try {
+            require_once dirname(__FILE__) . "/_files/MyBarcode5.php";
+            $barcode->setAdapter('MyBarcode5');
+            $this->fails('Exception expected');
+        } catch (Exception $e) {
+            $this->assertContains('does not implement', $e->getMessage());
+        }
+    }
+
+    public function testArrayConstructAdapter()
+    {
+        $barcode = new Zend_Validate_Barcode(array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false));
+        $this->assertTrue($barcode->getAdapter() instanceof Zend_Validate_Barcode_Ean13);
+        $this->assertFalse($barcode->getChecksum());
+    }
+
+    public function testInvalidArrayConstructAdapter()
+    {
+        try {
+            $barcode = new Zend_Validate_Barcode(array('options' => 'unknown', 'checksum' => false));
+            $this->fails('Exception expected');
+        } catch (Exception $e) {
+            $this->assertContains('Missing option', $e->getMessage());
+        }
+    }
+
+    public function testConfigConstructAdapter()
+    {
+        $array = array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false);
+        require_once 'Zend/Config.php';
+        $config = new Zend_Config($array);
+
+        $barcode = new Zend_Validate_Barcode($config);
+        $this->assertTrue($barcode->isValid('0075678164125'));
+    }
+
+    public function testEAN13()
+    {
+        $barcode = new Zend_Validate_Barcode('ean13');
+
+        $this->assertTrue($barcode->isValid('0075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('0075678164124'));
+    }
+
+    public function testEAN12()
+    {
+        $barcode = new Zend_Validate_Barcode('ean12');
+        $this->assertTrue($barcode->isValid('075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('075678164124'));
+    }
+
+    public function testEAN14()
+    {
+        $barcode = new Zend_Validate_Barcode('ean14');
+        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('075678164124'));
+    }
+
+    public function testEAN8()
+    {
+        $barcode = new Zend_Validate_Barcode('ean8');
+        $this->assertTrue($barcode->isValid('67816413'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('67816412'));
+    }
+
+    public function testGTIN12()
+    {
+        $barcode = new Zend_Validate_Barcode('gtin12');
+        $this->assertTrue($barcode->isValid('075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('075678164124'));
+    }
+
+    public function testGTIN13()
+    {
+        $barcode = new Zend_Validate_Barcode('gtin13');
+        $this->assertTrue($barcode->isValid('0075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('0075678164124'));
+    }
+
+    public function testGTIN14()
+    {
+        $barcode = new Zend_Validate_Barcode('gtin14');
+        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
+
+    public function testCODE25()
+    {
+        $barcode = new Zend_Validate_Barcode('code25');
+        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $barcode->setChecksum(true);
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
+
+    public function testCODE93()
+    {
+        $barcode = new Zend_Validate_Barcode('code93');
+        $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
+
+    public function testITF14()
+    {
+        $barcode = new Zend_Validate_Barcode('itf14');
+        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
+
+    public function testSSCC()
+    {
+        $barcode = new Zend_Validate_Barcode('sscc');
+        $this->assertTrue($barcode->isValid('000000075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('000000075678164124'));
+    }
+
+    public function testUPCA()
+    {
+        $barcode = new Zend_Validate_Barcode('upca');
+        $this->assertTrue($barcode->isValid('065100004327'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('065100004328'));
+    }
+
+    public function testUPCE()
+    {
+        $barcode = new Zend_Validate_Barcode('upce');
+        $this->assertTrue($barcode->isValid('123456'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('1234567'));
+    }
 }

+ 8 - 0
tests/Zend/Validate/_files/MyBarcode1.php

@@ -0,0 +1,8 @@
+<?php
+class MyBarcode1 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    protected $_length     = -1;
+    protected $_characters = 0;
+    protected $_checksum   = 'invalid';
+}
+?>

+ 8 - 0
tests/Zend/Validate/_files/MyBarcode2.php

@@ -0,0 +1,8 @@
+<?php
+class MyBarcode2 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    protected $_length     = array(1,3,6);
+    protected $_characters = 128;
+    protected $_checksum   = '_mod10';
+}
+?>

+ 8 - 0
tests/Zend/Validate/_files/MyBarcode3.php

@@ -0,0 +1,8 @@
+<?php
+class MyBarcode3 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    protected $_length     = array(1,3,6, -1);
+    protected $_characters = 128;
+    protected $_checksum   = '_mod10';
+}
+?>

+ 8 - 0
tests/Zend/Validate/_files/MyBarcode4.php

@@ -0,0 +1,8 @@
+<?php
+class MyBarcode4 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    protected $_length     = 'odd';
+    protected $_characters = 128;
+    protected $_checksum   = '_mod10';
+}
+?>

+ 8 - 0
tests/Zend/Validate/_files/MyBarcode5.php

@@ -0,0 +1,8 @@
+<?php
+class MyBarcode5
+{
+    protected $_length     = 'odd';
+    protected $_characters = 128;
+    protected $_checksum   = '_mod10';
+}
+?>