Просмотр исходного кода

[GENERIC] Zend_Validate_Barcode:

- added UPC-E variants for 7 and 8 digits

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19986 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 лет назад
Родитель
Сommit
ed16f6b4e7

+ 4 - 3
documentation/manual/en/module_specs/Zend_Validate-Barcode.xml

@@ -218,9 +218,10 @@
             </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.
+                This barcode is a smaller variant of UPCA. It can have a length of 6, 7 or 8
+                characters and supports only digits. When the barcode is 8 chars long it includes
+                a checksum which is calculated with modulo 10. It is commonly used with small
+                products where a UPCA barcode would not fit.
             </para>
         </listitem>
     </itemizedlist>

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

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

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

@@ -331,11 +331,13 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($barcode->isValid('123456789013'));
     }
 
-    public function testxxxxxxxxxxxxUPCE()
+    public function testUPCE()
     {
         $barcode = new Zend_Validate_Barcode('upce');
-        $this->assertTrue($barcode->isValid('123456'));
+        $this->assertTrue($barcode->isValid('02345673'));
+        $this->assertFalse($barcode->isValid('02345672'));
         $this->assertFalse($barcode->isValid('123'));
-        $this->assertFalse($barcode->isValid('1234567'));
+        $this->assertTrue($barcode->isValid('123456'));
+        $this->assertTrue($barcode->isValid('0234567'));
     }
 }