Browse Source

[ZF-8660] Zend_Validate_Barcode:

- added Identcode adapter
- minor changes

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19974 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 năm trước cách đây
mục cha
commit
a3842b8722

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

@@ -137,6 +137,19 @@
 
         <listitem>
             <para>
+                <emphasis>IDENTCODE</emphasis>: Identcode is used by Deutsche Post and DHL. It's an
+                specialized implementation of Code25.
+            </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
+                mainly used by the companies DP and DHL.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
                 <emphasis>ITF14</emphasis>: ITF14 is the GS1 implementation of an Interleaved Two
                 of Five bar code.
             </para>

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

@@ -190,9 +190,9 @@ class Zend_Validate_Barcode extends Zend_Validate_Abstract
         }
 
         $this->_value  = (string) $value;
-        $this->_length = strlen($value);
-        $adapter = $this->getAdapter();
-        $result  = $adapter->checkLength($value);
+        $adapter       = $this->getAdapter();
+        $this->_length = $adapter->getLength();
+        $result        = $adapter->checkLength($value);
         if (!$result) {
             $this->_error(self::INVALID_LENGTH);
             return false;
@@ -214,4 +214,4 @@ class Zend_Validate_Barcode extends Zend_Validate_Abstract
 
         return true;
     }
-}
+}

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

@@ -199,6 +199,7 @@ abstract class Zend_Validate_Barcode_AdapterAbstract
 
     /**
      * Validates the checksum (Modulo 10)
+     * GTIN implementation factor 3
      *
      * @param  string $value The barcode to validate
      * @return boolean
@@ -225,4 +226,34 @@ abstract class Zend_Validate_Barcode_AdapterAbstract
 
         return true;
     }
+
+    /**
+     * Validates the checksum (Modulo 10)
+     * IDENTCODE implementation factors 9 and 4
+     *
+     * @param  string $value The barcode to validate
+     * @return boolean
+     */
+    protected function _mod10ident($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] * 4;
+            } else {
+                $sum += $barcode[$length - $i] * 9;
+            }
+        }
+
+        $calc     = $sum % 10;
+        $checksum = ($calc === 0) ? 0 : (10 - $calc);
+        if ($value[$length + 1] != $checksum) {
+            return false;
+        }
+
+        return true;
+    }
 }

+ 52 - 0
library/Zend/Validate/Barcode/Identcode.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_Identcode 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 = '_mod10ident';
+}

+ 52 - 0
library/Zend/Validate/Barcode/Leitcode.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_Leitcode 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 = '_mod10ident';
+}

+ 41 - 24
tests/Zend/Validate/BarcodeTest.php

@@ -184,13 +184,28 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($barcode->isValid('0075678164125'));
     }
 
-    public function testEAN13()
+    public function testCODE25()
     {
-        $barcode = new Zend_Validate_Barcode('ean13');
+        $barcode = new Zend_Validate_Barcode('code25');
+        $this->assertTrue($barcode->isValid('00075678164125'));
+        $this->assertFalse($barcode->isValid('123'));
+        $barcode->setChecksum(true);
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
 
-        $this->assertTrue($barcode->isValid('0075678164125'));
+    public function testCODE93()
+    {
+        $barcode = new Zend_Validate_Barcode('code93');
+        $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
+        $this->assertFalse($barcode->isValid('00075678164124'));
+    }
+
+    public function testEAN8()
+    {
+        $barcode = new Zend_Validate_Barcode('ean8');
+        $this->assertTrue($barcode->isValid('67816413'));
         $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('0075678164124'));
+        $this->assertFalse($barcode->isValid('67816412'));
     }
 
     public function testEAN12()
@@ -201,20 +216,20 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($barcode->isValid('075678164124'));
     }
 
-    public function testEAN14()
+    public function testEAN13()
     {
-        $barcode = new Zend_Validate_Barcode('ean14');
-        $this->assertTrue($barcode->isValid('00075678164125'));
+        $barcode = new Zend_Validate_Barcode('ean13');
+        $this->assertTrue($barcode->isValid('0075678164125'));
         $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('075678164124'));
+        $this->assertFalse($barcode->isValid('0075678164124'));
     }
 
-    public function testEAN8()
+    public function testEAN14()
     {
-        $barcode = new Zend_Validate_Barcode('ean8');
-        $this->assertTrue($barcode->isValid('67816413'));
+        $barcode = new Zend_Validate_Barcode('ean14');
+        $this->assertTrue($barcode->isValid('00075678164125'));
         $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('67816412'));
+        $this->assertFalse($barcode->isValid('075678164124'));
     }
 
     public function testGTIN12()
@@ -241,20 +256,13 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($barcode->isValid('00075678164124'));
     }
 
-    public function testCODE25()
+    public function testIDENTCODE()
     {
-        $barcode = new Zend_Validate_Barcode('code25');
-        $this->assertTrue($barcode->isValid('00075678164125'));
+        $barcode = new Zend_Validate_Barcode('identcode');
+        $this->assertTrue($barcode->isValid('564000000050'));
         $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'));
+        $this->assertFalse($barcode->isValid('0563102430313'));
+        $this->assertFalse($barcode->isValid('563102430312'));
     }
 
     public function testITF14()
@@ -265,6 +273,15 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($barcode->isValid('00075678164124'));
     }
 
+    public function testLEITCODE()
+    {
+        $barcode = new Zend_Validate_Barcode('leitcode');
+        $this->assertTrue($barcode->isValid('21348075016401'));
+        $this->assertFalse($barcode->isValid('123'));
+        $this->assertFalse($barcode->isValid('021348075016401'));
+        $this->assertFalse($barcode->isValid('21348075016402'));
+    }
+
     public function testSSCC()
     {
         $barcode = new Zend_Validate_Barcode('sscc');