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

[GENERIC] Zend_Barcode: review preparation of human readable text to limit repetition of same code

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

+ 0 - 10
library/Zend/Barcode/Object/Code25.php

@@ -155,14 +155,4 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
 
         return $checksum;
     }
-
-    /**
-     * Retrieve text with checksum appended
-     *
-     * @return string
-     */
-    protected function _getTextWithChecksum()
-    {
-        return $this->_text . $this->getChecksum($this->_text);
-    }
 }

+ 6 - 11
library/Zend/Barcode/Object/Code39.php

@@ -137,7 +137,12 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
      */
     public function getTextToDisplay()
     {
-        return '*' . parent::getTextToDisplay() . '*';
+        $text = parent::getTextToDisplay();
+        if (substr($text, 0, 1) != '*' && substr($text, -1) != '*') {
+            return '*' . $text . '*';
+        } else {
+            return $text;
+        }
     }
 
     /**
@@ -179,14 +184,4 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
         }
         return array_search(($checksum % 43), $charset);
     }
-
-    /**
-     * Get text with appended checksum
-     *
-     * @return string
-     */
-    protected function _getTextWithChecksum()
-    {
-        return $this->_text . $this->getChecksum($this->_text);
-    }
 }

+ 0 - 33
library/Zend/Barcode/Object/Int25.php

@@ -79,39 +79,6 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
     }
 
     /**
-     * Retrieve text to encode
-     * @return string
-     */
-    public function getText()
-    {
-        if ($this->_withChecksum) {
-            $text =  $this->_getTextWithChecksum();
-        } else {
-            $text = $this->_text;
-        }
-        return (strlen($text) % 2 ? '0' . $text : $text);
-    }
-
-    /**
-     * Retrieve text to display
-     * @return string
-     */
-    public function getTextToDisplay()
-    {
-        if ($this->_withChecksumInText) {
-            $text = $this->_getTextWithChecksum();
-            return (strlen($text) % 2 ? '0' . $text : $text);
-        }
-
-        $text = $this->_text;
-        if ($this->_withChecksum) {
-            return (strlen($text) % 2 ? $text : '0' . $text);
-        }
-
-        return (strlen($text) % 2 ? '0' . $text : $text);
-    }
-
-    /**
      * Width of the barcode (in pixels)
      * @return integer
      */

+ 0 - 22
library/Zend/Barcode/Object/Itf14.php

@@ -85,28 +85,6 @@ class Zend_Barcode_Object_Itf14 extends Zend_Barcode_Object_Int25
     }
 
     /**
-     * Retrieve text to encode
-     * @return string
-     */
-    public function getText()
-    {
-        $text = $this->_getTextWithChecksum();
-        if (strlen($text) < 14) {
-            $text = str_repeat('0', 14 - strlen($text)) . $text;
-        }
-        return $text;
-    }
-
-    /**
-     * Retrieve text to display
-     * @return string
-     */
-    public function getTextToDisplay()
-    {
-        return $this->getText();
-    }
-
-    /**
      * Check allowed characters
      * @param string $value
      * @return string

+ 37 - 4
library/Zend/Barcode/Object/ObjectAbstract.php

@@ -168,11 +168,19 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     protected $_withChecksumInText = false;
 
     /**
+     * Fix barcode length (numeric or string like 'even')
      * @var $_barcodeLength integer | string
      */
     protected $_barcodeLength = null;
 
     /**
+     * Activate automatic addition of leading zeros 
+     * if barcode length is fixed
+     * @var $_addLeadingZeros boolean
+     */
+    protected $_addLeadingZeros = true;
+
+    /**
      * TTF font name: can be set before instanciation of the object
      * @var string
      */
@@ -494,10 +502,34 @@ abstract class Zend_Barcode_Object_ObjectAbstract
      */
     public function getText()
     {
+        $text = $this->_text;
         if ($this->_withChecksum) {
-            return $this->_getTextWithChecksum();
+            $text .= $this->getChecksum($this->_text);
         }
-        return $this->_text;
+        return $this->_addLeadingZeros($text);
+    }
+
+    /**
+     * Automatically add leading zeros if barcode length is fixed
+     * @param string $text
+     * @param boolean $withoutChecksum
+     */
+    protected function _addLeadingZeros($text, $withoutChecksum = false)
+    {
+        if ($this->_barcodeLength && $this->_addLeadingZeros) {
+            if (is_int($this->_barcodeLength)) {
+                $length = $withoutChecksum ? ($this->_barcodeLength - 1) : $this->_barcodeLength;
+                if (strlen($text) < $length) {
+                    $text = str_repeat('0', $length - strlen($text)) . $text;
+                }
+            } else {
+                if ($this->_barcodeLength == 'even') {
+                    $omitChecksum = (int) $this->_withChecksum && $withoutChecksum;
+                    $text = ((strlen($text) - $omitChecksum) % 2 ? '0' . $text : $text);
+                }
+            }
+        }
+        return $text;
     }
 
     /**
@@ -516,9 +548,10 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     public function getTextToDisplay()
     {
         if ($this->_withChecksumInText) {
-            return $this->_getTextWithChecksum();
+            return $this->getText();
+        } else {
+            return $this->_addLeadingZeros($this->_text, true);
         }
-        return $this->_text;
     }
 
     /**