Переглянути джерело

[GENERIC] Zend_Barcode:
- simplify object's code when checksum is mandatory
- review validation of barcode to limit repetition of same code

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19979 44c647ce-9c0f-0410-b52a-842ac1e357ba

mikaelkael 16 роки тому
батько
коміт
f2fd4683d4

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

@@ -60,17 +60,6 @@ class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
     );
 
     /**
-     * Check allowed characters
-     * @param string $value
-     * @return string
-     * @throw Zend_Barcode_Object_Exception
-     */
-    public function validateText($value)
-    {
-        $this->_validateText($value);
-    }
-
-    /**
      * Width of the barcode (in pixels)
      * @return integer
      */

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

@@ -92,16 +92,6 @@ class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
     );
 
     /**
-     * Check for invalid characters
-     * @param   string $value    Text to be ckecked
-     * @return  void
-     */
-    public function validateText($value)
-    {
-        $this->_validateText($value);
-    }
-
-    /**
      * Partial check of Code39 barcode
      * @return void
      */

+ 2 - 1
library/Zend/Barcode/Object/Int25.php

@@ -43,6 +43,7 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
     private $_withBearerBars = false;
 
     /**
+     * Number of characters in the barcode
      * @var $_barcodeLength integer | string
      */
     protected $_barcodeLength = 'even';
@@ -75,7 +76,7 @@ class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
      */
     public function validateText($value)
     {
-        $this->_validateText($value, array('automaticPrepend' => 0, 'validator' => 'code25'));
+        $this->_validateText($value, array('validator' => 'code25'));
     }
 
     /**

+ 5 - 50
library/Zend/Barcode/Object/Itf14.php

@@ -38,60 +38,15 @@ class Zend_Barcode_Object_Itf14 extends Zend_Barcode_Object_Int25
 {
 
     /**
+     * Number of characters in the barcode
      * @var $_barcodeLength integer | string
      */
     protected $_barcodeLength = 14;
 
     /**
-     * Constructor
-     * @param array|Zend_Config $options
-     * @return void
+     * Activation of mandatory checksum
+     * to deactivate unauthorized modification
+     * @var $_mandatoryChecksum boolean
      */
-    public function __construct($options = null)
-    {
-        parent::__construct($options);
-
-        // Checksum is mandatory with Itf14
-        $this->_withChecksum = true;
-
-        // Default to true but not mandatory
-        $this->_withChecksumInText = true;
-    }
-
-    /**
-     * Activate/deactivate the automatic generation
-     * of the checksum character
-     * added to the barcode text
-     * @param boolean $value
-     * @return Zend_Barcode_Object
-     */
-    public function setWithChecksum($value)
-    {
-        // Checksum is mandatory with Itf14
-        return $this;
-    }
-
-    /**
-     * Activate/deactivate the automatic generation
-     * of the checksum character
-     * added to the barcode text
-     * @param boolean $value
-     * @return Zend_Barcode_Object
-     * @throw Zend_Barcode_Object_Exception
-     */
-    public function setWithChecksumInText($value)
-    {
-        return $this;
-    }
-
-    /**
-     * Check allowed characters
-     * @param string $value
-     * @return string
-     * @throw Zend_Barcode_Object_Exception
-     */
-    public function validateText($value)
-    {
-        $this->_validateText($value, array('automaticPrepend' => 0, 'substituteChecksumCharacter' => 0));
-    }
+    protected $_mandatoryChecksum = true;
 }

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

@@ -174,13 +174,26 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     protected $_barcodeLength = null;
 
     /**
-     * Activate automatic addition of leading zeros 
+     * Activate automatic addition of leading zeros
      * if barcode length is fixed
      * @var $_addLeadingZeros boolean
      */
     protected $_addLeadingZeros = true;
 
     /**
+     * Activation of mandatory checksum
+     * to deactivate unauthorized modification
+     * @var $_mandatoryChecksum boolean
+     */
+    protected $_mandatoryChecksum = false;
+
+    /**
+     * Character used to substitute checksum character for validation
+     * @var $_substituteChecksumCharacter mixed
+     */
+    protected $_substituteChecksumCharacter = 0;
+
+    /**
      * TTF font name: can be set before instanciation of the object
      * @var string
      */
@@ -203,6 +216,10 @@ abstract class Zend_Barcode_Object_ObjectAbstract
             $this->setOptions($options);
         }
         $this->_type = strtolower(substr(get_class($this), strlen($this->_barcodeNamespace) + 1));
+        if ($this->_mandatoryChecksum) {
+            $this->_withChecksum = true;
+            $this->_withChecksumInText = true;
+        }
     }
 
     /**
@@ -517,14 +534,14 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     protected function _addLeadingZeros($text, $withoutChecksum = false)
     {
         if ($this->_barcodeLength && $this->_addLeadingZeros) {
+            $omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
             if (is_int($this->_barcodeLength)) {
-                $length = $withoutChecksum ? ($this->_barcodeLength - 1) : $this->_barcodeLength;
+                $length = $this->_barcodeLength - $omitChecksum;
                 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);
                 }
             }
@@ -606,7 +623,9 @@ abstract class Zend_Barcode_Object_ObjectAbstract
      */
     public function setWithChecksum($value)
     {
-        $this->_withChecksum = (bool) $value;
+        if (!$this->_mandatoryChecksum) {
+            $this->_withChecksum = (bool) $value;
+        }
         return $this;
     }
 
@@ -630,7 +649,9 @@ abstract class Zend_Barcode_Object_ObjectAbstract
      */
     public function setWithChecksumInText($value)
     {
-        $this->_withChecksumInText = (bool) $value;
+        if (!$this->_mandatoryChecksum) {
+            $this->_withChecksumInText = (bool) $value;
+        }
         return $this;
     }
 
@@ -1164,11 +1185,14 @@ abstract class Zend_Barcode_Object_ObjectAbstract
     }
 
     /**
-     * Each child must verify allowed characters
-     *
+     * Check for invalid characters
+     * @param   string $value    Text to be ckecked
      * @return void
      */
-    abstract public function validateText($value);
+    public function validateText($value)
+    {
+        $this->_validateText($value);
+    }
 
     protected function _validateText($value, $options = array())
     {
@@ -1179,22 +1203,16 @@ abstract class Zend_Barcode_Object_ObjectAbstract
             '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);
-                }
-            }
+        $checksumCharacter = '';
+        $withChecksum = false;
+        if ($this->_mandatoryChecksum) {
+            $checksumCharacter = $this->_substituteChecksumCharacter;
+            $withChecksum = true;
         }
 
-        if (!$validator->isValid($value . $checksumCharacter)) {
+        $value = $this->_addLeadingZeros($value, $withChecksum) . $checksumCharacter;
+
+        if (!$validator->isValid($value)) {
             $message = implode("\n", $validator->getMessages());
 
             /**