Sfoglia il codice sorgente

[GENERIC, ZF-8642] Zend_Validate_Barcode:

- added EAN2 and EAN5 validators
- some minor fixes for variable line validators

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20007 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 anni fa
parent
commit
c807ca28d9

+ 43 - 7
documentation/manual/en/module_specs/Zend_Validate-Barcode.xml

@@ -72,9 +72,45 @@
 
             <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.
+                and 7 special characters. It has an optional 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>CODE93EXT</emphasis>: CODE93EXT is an extension of CODE93.
+            </para>
+
+            <para>
+                This barcode has the same properties as CODE93. Additionally it allows the usage of
+                all 128 ASCII characters. This standard is used worldwide and common within the
+                industry.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN2</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                These barcode must have 2 characters. It supports only digits and does not have a
+                checksum. This standard is mainly used as addition to EAN13 (ISBN) when printed on
+                books.
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>EAN5</emphasis>: EAN is the shortcut for "European Article Number".
+            </para>
+
+            <para>
+                These barcode must have 5 characters. It supports only digits and does not have a
+                checksum. This standard is mainly used as addition to EAN13 (ISBN) when printed on
+                books.
             </para>
         </listitem>
 
@@ -84,10 +120,10 @@
             </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.
+                These barcode can have 7 or 8 characters. It supports only digits. When it has a
+                length of 8 characters it includes 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>
 

+ 14 - 2
library/Zend/Validate/Barcode/Code93.php

@@ -48,7 +48,7 @@ class Zend_Validate_Barcode_Code93 extends Zend_Validate_Barcode_AdapterAbstract
      * Checksum function
      * @var string
      */
-    protected $_checksum = '_modCK';
+    protected $_checksum = '_code93';
 
     /**
      * Note that the characters !"§& are only synonyms
@@ -65,12 +65,24 @@ class Zend_Validate_Barcode_Code93 extends Zend_Validate_Barcode_AdapterAbstract
     );
 
     /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+
+    /**
      * Validates the checksum (Modulo CK)
      *
      * @param  string $value The barcode to validate
      * @return boolean
      */
-    protected function _modCK($value)
+    protected function _code93($value)
     {
         $checksum = substr($value, -2, 2);
         $value    = str_split(substr($value, 0, -2));

+ 58 - 0
library/Zend/Validate/Barcode/Code93ext.php

@@ -0,0 +1,58 @@
+<?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_Code93ext extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = -1;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = 128;
+
+    /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

+ 58 - 0
library/Zend/Validate/Barcode/Ean2.php

@@ -0,0 +1,58 @@
+<?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_Ean2 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 2;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

+ 58 - 0
library/Zend/Validate/Barcode/Ean5.php

@@ -0,0 +1,58 @@
+<?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_Ean5 extends Zend_Validate_Barcode_AdapterAbstract
+{
+    /**
+     * Allowed barcode lengths
+     * @var integer
+     */
+    protected $_length = 5;
+
+    /**
+     * Allowed barcode characters
+     * @var string
+     */
+    protected $_characters = '0123456789';
+
+    /**
+     * Constructor
+     *
+     * Sets check flag to false.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        $this->setCheck(false);
+    }
+}

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

@@ -36,7 +36,7 @@ class Zend_Validate_Barcode_Ean8 extends Zend_Validate_Barcode_AdapterAbstract
      * Allowed barcode lengths
      * @var integer
      */
-    protected $_length = 8;
+    protected $_length = array(7, 8);
 
     /**
      * Allowed barcode characters
@@ -49,4 +49,21 @@ class Zend_Validate_Barcode_Ean8 extends Zend_Validate_Barcode_AdapterAbstract
      * @var string
      */
     protected $_checksum = '_gtin';
+
+    /**
+     * Overrides parent checkLength
+     *
+     * @param string $value Value
+     * @return boolean
+     */
+    public function checkLength($value)
+    {
+        if (strlen($value) == 7) {
+            $this->setCheck(false);
+        } else {
+            $this->setCheck(true);
+        }
+
+        return parent::checkLength($value);
+    }
 }

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

@@ -48,5 +48,5 @@ class Zend_Validate_Barcode_Itf14 extends Zend_Validate_Barcode_AdapterAbstract
      * Checksum function
      * @var string
      */
-    protected $_checksum = '_mod10';
+    protected $_checksum = '_gtin';
 }

+ 2 - 12
library/Zend/Validate/Barcode/Upce.php

@@ -51,18 +51,6 @@ class Zend_Validate_Barcode_Upce extends Zend_Validate_Barcode_AdapterAbstract
     protected $_checksum = '_gtin';
 
     /**
-     * Constructor
-     *
-     * Set check flag to false
-     *
-     * @return void
-     */
-    public function __construct()
-    {
-        $this->setCheck(true);
-    }
-
-    /**
      * Overrides parent checkLength
      *
      * @param string $value Value
@@ -72,6 +60,8 @@ class Zend_Validate_Barcode_Upce extends Zend_Validate_Barcode_AdapterAbstract
     {
         if (strlen($value) != 8) {
             $this->setCheck(false);
+        } else {
+            $this->setCheck(true);
         }
 
         return parent::checkLength($value);

+ 37 - 4
tests/Zend/Validate/BarcodeTest.php

@@ -232,11 +232,43 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
 //        $this->assertFalse($barcode->isValid('159AZG'));
     }
 
-    public function testxxxxxxxxxxxxCODE93()
+    public function testCODE93()
     {
         $barcode = new Zend_Validate_Barcode('code93');
-        $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
-        $this->assertFalse($barcode->isValid('00075678164124'));
+        $this->assertTrue($barcode->isValid('TEST93+'));
+        $this->assertFalse($barcode->isValid('Test93+'));
+
+        $barcode->setChecksum(true);
+        $this->assertTrue($barcode->isValid('CODE 93E0'));
+        $this->assertFalse($barcode->isValid('CODE 93E1'));
+    }
+
+    public function testCODE93EXT()
+    {
+        $barcode = new Zend_Validate_Barcode('code93ext');
+        $this->assertTrue($barcode->isValid('TEST93+'));
+        $this->assertTrue($barcode->isValid('Test93+'));
+
+// @TODO: CODE93 EXTENDED CHECKSUM VALIDATION MISSING
+//        $barcode->setChecksum(true);
+//        $this->assertTrue($barcode->isValid('CODE 93E0'));
+//        $this->assertFalse($barcode->isValid('CODE 93E1'));
+    }
+
+    public function testEAN2()
+    {
+        $barcode = new Zend_Validate_Barcode('ean2');
+        $this->assertTrue($barcode->isValid('12'));
+        $this->assertFalse($barcode->isValid('1'));
+        $this->assertFalse($barcode->isValid('123'));
+    }
+
+    public function testEAN5()
+    {
+        $barcode = new Zend_Validate_Barcode('ean5');
+        $this->assertTrue($barcode->isValid('12345'));
+        $this->assertFalse($barcode->isValid('1234'));
+        $this->assertFalse($barcode->isValid('123456'));
     }
 
     public function testEAN8()
@@ -245,6 +277,7 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($barcode->isValid('12345670'));
         $this->assertFalse($barcode->isValid('123'));
         $this->assertFalse($barcode->isValid('12345671'));
+        $this->assertTrue($barcode->isValid('1234567'));
     }
 
     public function testEAN12()
@@ -312,7 +345,7 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($barcode->isValid('564000000051'));
     }
 
-    public function testxxxxxxxxxxxxITF14()
+    public function testITF14()
     {
         $barcode = new Zend_Validate_Barcode('itf14');
         $this->assertTrue($barcode->isValid('00075678164125'));