Преглед изворни кода

[GENERIC] Zend_Validate_Barcode:

- finished CODE25 and CODE25INTERLEAVED implementation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20003 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas пре 16 година
родитељ
комит
3b908a9327

+ 17 - 5
documentation/manual/en/module_specs/Zend_Validate-Barcode.xml

@@ -17,14 +17,26 @@
     <itemizedlist>
         <listitem>
             <para>
-                <emphasis>CODE25</emphasis>: Often called "two of five."
+                <emphasis>CODE25</emphasis>: Often called "two of five" or "Code25 Industrial".
             </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.
+                This barcode has no length limitation. It supports only digits, and the last digit
+                can be an optional checksum which is calculated with modulo 10. This standard is
+                very old and nowadays not often used. Common usecases are within the industry.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>CODE25INTERLEAVED</emphasis>: Often called "Code 2 of 5 Interleaved".
+            </para>
+
+            <para>
+                This standard is a variant of CODE25. It 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. It is used worldwide
+                and common on the market.
             </para>
         </listitem>
 

+ 1 - 1
library/Zend/Validate/Barcode.php

@@ -131,7 +131,7 @@ class Zend_Validate_Barcode extends Zend_Validate_Abstract
      */
     public function setAdapter($adapter, $options = null)
     {
-        $adapter = ucfirst($adapter);
+        $adapter = ucfirst(strtolower($adapter));
         require_once 'Zend/Loader.php';
         if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
             $adapter = 'Zend_Validate_Barcode_' . $adapter;

+ 31 - 1
library/Zend/Validate/Barcode/AdapterAbstract.php

@@ -198,7 +198,7 @@ abstract class Zend_Validate_Barcode_AdapterAbstract
     }
 
     /**
-     * Validates the gtin checksum (Modulo 10)
+     * Validates the checksum (Modulo 10)
      * GTIN implementation factor 3
      *
      * @param  string $value The barcode to validate
@@ -256,4 +256,34 @@ abstract class Zend_Validate_Barcode_AdapterAbstract
 
         return true;
     }
+
+    /**
+     * Validates the checksum (Modulo 10)
+     * CODE25 implementation factor 3
+     *
+     * @param  string $value The barcode to validate
+     * @return boolean
+     */
+    protected function _code25($value)
+    {
+        $barcode = substr($value, 0, -1);
+        $sum     = 0;
+        $length  = strlen($barcode) - 1;
+
+        for ($i = 0; $i <= $length; $i++) {
+            if (($i % 2) === 0) {
+                $sum += $barcode[$i] * 3;
+            } else {
+                $sum += $barcode[$i];
+            }
+        }
+
+        $calc     = $sum % 10;
+        $checksum = ($calc === 0) ? 0 : (10 - $calc);
+        if ($value[$length + 1] != $checksum) {
+            return false;
+        }
+
+        return true;
+    }
 }

+ 3 - 3
library/Zend/Validate/Barcode/Code25.php

@@ -36,7 +36,7 @@ class Zend_Validate_Barcode_Code25 extends Zend_Validate_Barcode_AdapterAbstract
      * Allowed barcode lengths
      * @var integer
      */
-    protected $_length = 'even';
+    protected $_length = -1;
 
     /**
      * Allowed barcode characters
@@ -48,13 +48,13 @@ class Zend_Validate_Barcode_Code25 extends Zend_Validate_Barcode_AdapterAbstract
      * Checksum function
      * @var string
      */
-    protected $_checksum = '_mod10';
+    protected $_checksum = '_code25';
 
     /**
      * Constructor
      *
      * Sets check flag to false.
-     * 
+     *
      * @return void
      */
     public function __construct()

+ 64 - 0
library/Zend/Validate/Barcode/Code25interleaved.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_Code25interleaved 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 = '_code25';
+
+    /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

+ 17 - 3
tests/Zend/Validate/BarcodeTest.php

@@ -184,13 +184,27 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($barcode->isValid('0075678164125'));
     }
 
-    public function testxxxxxxxxxxxxCODE25()
+    public function testCODE25()
     {
         $barcode = new Zend_Validate_Barcode('code25');
-        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertTrue($barcode->isValid('0123456789101213'));
+        $this->assertTrue($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('123a'));
+
+        $barcode->setChecksum(true);
+        $this->assertTrue($barcode->isValid('0123456789101214'));
+        $this->assertFalse($barcode->isValid('0123456789101213'));
+    }
+
+    public function testCODE25INTERLEAVED()
+    {
+        $barcode = new Zend_Validate_Barcode('code25interleaved');
+        $this->assertTrue($barcode->isValid('0123456789101213'));
         $this->assertFalse($barcode->isValid('123'));
+
         $barcode->setChecksum(true);
-        $this->assertFalse($barcode->isValid('00075678164124'));
+        $this->assertTrue($barcode->isValid('0123456789101214'));
+        $this->assertFalse($barcode->isValid('0123456789101213'));
     }
 
     public function testCODE39()