浏览代码

[GENERIC] Zend_Barcode: review validation of barcode to limit repetition of same code

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19952 44c647ce-9c0f-0410-b52a-842ac1e357ba
mikaelkael 16 年之前
父节点
当前提交
148b70e739

+ 5 - 17
library/Zend/Barcode/Object/Code25.php

@@ -67,19 +67,7 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
      */
     public function validateText($value)
     {
-        $validator = new Zend_Validate_Barcode(array(
-            'adapter'  => 'code25',
-            'checksum' => false,
-        ));
-        if (!$validator->isValid($value)) {
-            $message = implode("\n", $validator->getMessages());
-
-            /**
-             * @see Zend_Barcode_Object_Exception
-             */
-            require_once 'Zend/Barcode/Object/Exception.php';
-            throw new Zend_Barcode_Object_Exception($message);
-        }
+        $this->_validateText($value);
     }
 
     /**
@@ -90,7 +78,7 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
     {
         $quietZone       = $this->getQuietZone();
         $startCharacter  = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
-        $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth) 
+        $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth)
                          * $this->_factor;
         $encodedData     = strlen($this->getText()) * $characterLength;
         $stopCharacter   = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
@@ -144,8 +132,8 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
 
     /**
      * Get barcode checksum
-     * 
-     * @param  string $text 
+     *
+     * @param  string $text
      * @return int
      */
     public function getChecksum($text)
@@ -170,7 +158,7 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
 
     /**
      * Retrieve text with checksum appended
-     * 
+     *
      * @return string
      */
     protected function _getTextWithChecksum()

+ 4 - 16
library/Zend/Barcode/Object/Code39.php

@@ -98,19 +98,7 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
      */
     public function validateText($value)
     {
-        $validator = new Zend_Validate_Barcode(array(
-            'adapter'  => 'code39',
-            'checksum' => false,
-        ));
-
-        if (!$validator->isValid($value)) {
-            $message = implode("\n", $validator->getMessages());
-            /**
-             * @see Zend_Barcode_Object_Exception
-             */
-            require_once 'Zend/Barcode/Object/Exception.php';
-            throw new Zend_Barcode_Object_Exception($message);
-        }
+        $this->_validateText($value);
     }
 
     /**
@@ -176,8 +164,8 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
 
     /**
      * Get barcode checksum
-     * 
-     * @param  string $text 
+     *
+     * @param  string $text
      * @return int
      */
     public function getChecksum($text)
@@ -194,7 +182,7 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
 
     /**
      * Get text with appended checksum
-     * 
+     *
      * @return string
      */
     protected function _getTextWithChecksum()

+ 10 - 6
library/Zend/Barcode/Object/Int25.php

@@ -43,6 +43,11 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
     private $_withBearerBars = false;
 
     /**
+     * @var $_barcodeLength integer | string
+     */
+    protected $_barcodeLength = 'even';
+
+    /**
      * Activate/deactivate drawing of bearer bars
      * @param boolean $value
      * @return Zend_Barcode_Object_Int25
@@ -70,8 +75,7 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
      */
     public function validateText($value)
     {
-        $value = (strlen($value) % 2 ? '0' . $value : $value);
-        parent::validateText($value);
+        $this->_validateText($value, array('automaticPrepend' => 0, 'validator' => 'code25'));
     }
 
     /**
@@ -146,15 +150,15 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
             // Interleave
             for ($ibar = 0; $ibar < 5; $ibar ++) {
                 // Draws char1 bar (fore color)
-                $barWidth = (substr($this->_codingMap[$char1], $ibar, 1)) 
-                          ? $this->_barThickWidth 
+                $barWidth = (substr($this->_codingMap[$char1], $ibar, 1))
+                          ? $this->_barThickWidth
                           : $this->_barThinWidth;
 
                 $barcodeTable[] = array(1, $barWidth, 0, 1);
 
                 // Left space corresponding to char2 (background color)
-                $barWidth = (substr($this->_codingMap[$char2], $ibar, 1)) 
-                          ? $this->_barThickWidth 
+                $barWidth = (substr($this->_codingMap[$char2], $ibar, 1))
+                          ? $this->_barThickWidth
                           : $this->_barThinWidth;
                 $barcodeTable[] = array(0, $barWidth, 0 , 1);
             }

+ 7 - 20
library/Zend/Barcode/Object/Itf14.php

@@ -36,6 +36,12 @@ require_once 'Zend/Validate/Barcode.php';
  */
 class Zend_Barcode_Object_Itf14 extends Zend_Barcode_Object_Int25
 {
+
+    /**
+     * @var $_barcodeLength integer | string
+     */
+    protected $_barcodeLength = 14;
+
     /**
      * Constructor
      * @param array|Zend_Config $options
@@ -108,25 +114,6 @@ class Zend_Barcode_Object_Itf14 extends Zend_Barcode_Object_Int25
      */
     public function validateText($value)
     {
-        $validator = new Zend_Validate_Barcode(array(
-            'adapter'  => 'itf14',
-            'checksum' => false,
-        ));
-
-        // prepend '0'
-        if (strlen($value) < 13) {
-            $value = str_repeat('0', 13 - strlen($value)) . $value;
-        }
-
-        // add a '0' because checksum is mandatory
-        if (!$validator->isValid($value . '0')) {
-            $message = implode("\n", $validator->getMessages());
-
-            /**
-             * @see Zend_Barcode_Object_Exception
-             */
-            require_once 'Zend/Barcode/Object/Exception.php';
-            throw new Zend_Barcode_Object_Exception($message);
-        }
+        $this->_validateText($value, array('automaticPrepend' => 0, 'substituteChecksumCharacter' => 0));
     }
 }

+ 40 - 0
library/Zend/Barcode/Object/ObjectAbstract.php

@@ -168,6 +168,11 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     protected $_withChecksumInText = false;
 
     /**
+     * @var $_barcodeLength integer | string
+     */
+    protected $_barcodeLength = null;
+
+    /**
      * TTF font name: can be set before instanciation of the object
      * @var string
      */
@@ -1132,6 +1137,41 @@ abstract class Zend_Barcode_Object_ObjectAbstract
      */
     abstract public function validateText($value);
 
+    protected function _validateText($value, $options = array())
+    {
+        $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
+
+        $validator = new Zend_Validate_Barcode(array(
+            'adapter'  => $validatorName,
+            'checksum' => false,
+        ));
+
+        $checksumCharacter = (isset($options['substituteChecksumCharacter'])) ? $options['substituteChecksumCharacter'] : '';
+
+        if (isset($options['automaticPrepend'])) {
+            if (is_int($this->_barcodeLength)) {
+                if (strlen($value) < ($this->_barcodeLength)) {
+                    $length = $this->_barcodeLength - strlen($checksumCharacter);
+                    $value = str_repeat($options['automaticPrepend'], $length - strlen($value)) . $value;
+                }
+            } else {
+                if ($this->_barcodeLength == 'even') {
+                    $value = (strlen($value) % 2 ? $options['automaticPrepend'] . $value : $value);
+                }
+            }
+        }
+
+        if (!$validator->isValid($value . $checksumCharacter)) {
+            $message = implode("\n", $validator->getMessages());
+
+            /**
+             * @see Zend_Barcode_Object_Exception
+             */
+            require_once 'Zend/Barcode/Object/Exception.php';
+            throw new Zend_Barcode_Object_Exception($message);
+        }
+    }
+
     /**
      * Each child must prepare the barcode and return
      * a table like array(