ZF-3174: Zend_Validate_StringLength fails when iconv internal encoding differs from input character set

Description

ZF-1913 broke Zend_Validate_StringLength in instances when PHP's iconv.internal_encoding setting differs from the character set of the data that's being validated.

The simple fix is to add a new character set option that defaults to UTF-8:


Index: library/Zend/Validate/StringLength.php
===================================================================
--- library/Zend/Validate/StringLength.php      (revision 9303)
+++ library/Zend/Validate/StringLength.php      (working copy)
@@ -54,7 +54,7 @@
         'min' => '_min',
         'max' => '_max'
     );
-
+
     /**
      * Minimum length
      *
@@ -70,18 +70,27 @@
      * @var integer|null
      */
     protected $_max;
+
+    /**
+     * String character set
+     *
+     * @var string
+     */
+    protected $_charset;

     /**
      * Sets validator options
      *
      * @param  integer $min
      * @param  integer $max
+     * @param  string  $charset
      * @return void
      */
-    public function __construct($min = 0, $max = null)
+    public function __construct($min = 0, $max = null, $charset = 'utf-8')
     {
         $this->setMin($min);
         $this->setMax($max);
+        $this->setCharset($charset);
     }

     /**
@@ -149,6 +158,28 @@

         return $this;
     }
+
+    /**
+     * Returns the character set
+     *
+     * @return string
+     */
+    public function getCharset()
+    {
+        return $this->_charset;
+    }
+
+    /**
+     * Sets the character set
+     *
+     * @param string $charset
+     * @return Zend_Validate_StringLength Provides a fluent interface
+     */
+    public function setCharset($charset)
+    {
+        $this->_charset = $charset;
+        return $this;
+    }

     /**
      * Defined by Zend_Validate_Interface
@@ -163,7 +194,7 @@
     {
         $valueString = (string) $value;
         $this->_setValue($valueString);
-        $length = iconv_strlen($valueString);
+        $length = iconv_strlen($valueString, $this->_charset);
         if ($length < $this->_min) {
             $this->_error(self::TOO_SHORT);
         }

Comments

Please evaluate and fix/categorize as necessary.

Duplication of ZF-3015. New feature implemented with r13278