00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_StringLength extends Zend_Validate_Abstract 00034 { 00035 const INVALID = 'stringLengthInvalid'; 00036 const TOO_SHORT = 'stringLengthTooShort'; 00037 const TOO_LONG = 'stringLengthTooLong'; 00038 00042 protected $_messageTemplates = array( 00043 self::INVALID => "Invalid type given, value should be a string", 00044 self::TOO_SHORT => "'%value%' is less than %min% characters long", 00045 self::TOO_LONG => "'%value%' is greater than %max% characters long" 00046 ); 00047 00051 protected $_messageVariables = array( 00052 'min' => '_min', 00053 'max' => '_max' 00054 ); 00055 00061 protected $_min; 00062 00070 protected $_max; 00071 00077 protected $_encoding; 00078 00086 public function __construct($min = 0, $max = null, $encoding = null) 00087 { 00088 $this->setMin($min); 00089 $this->setMax($max); 00090 $this->setEncoding($encoding); 00091 } 00092 00098 public function getMin() 00099 { 00100 return $this->_min; 00101 } 00102 00110 public function setMin($min) 00111 { 00112 if (null !== $this->_max && $min > $this->_max) { 00116 require_once 'Zend/Validate/Exception.php'; 00117 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >" 00118 . " $this->_max"); 00119 } 00120 $this->_min = max(0, (integer) $min); 00121 return $this; 00122 } 00123 00129 public function getMax() 00130 { 00131 return $this->_max; 00132 } 00133 00141 public function setMax($max) 00142 { 00143 if (null === $max) { 00144 $this->_max = null; 00145 } else if ($max < $this->_min) { 00149 require_once 'Zend/Validate/Exception.php'; 00150 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but " 00151 . "$max < $this->_min"); 00152 } else { 00153 $this->_max = (integer) $max; 00154 } 00155 00156 return $this; 00157 } 00158 00164 public function getEncoding() 00165 { 00166 return $this->_encoding; 00167 } 00168 00175 public function setEncoding($encoding = null) 00176 { 00177 if ($encoding !== null) { 00178 $orig = iconv_get_encoding('internal_encoding'); 00179 $result = iconv_set_encoding('internal_encoding', $encoding); 00180 if (!$result) { 00181 require_once 'Zend/Validate/Exception.php'; 00182 throw new Zend_Validate_Exception('Given encoding not supported on this OS!'); 00183 } 00184 00185 iconv_set_encoding('internal_encoding', $orig); 00186 } 00187 00188 $this->_encoding = $encoding; 00189 return $this; 00190 } 00191 00201 public function isValid($value) 00202 { 00203 if (!is_string($value)) { 00204 $this->_error(self::INVALID); 00205 return false; 00206 } 00207 00208 $this->_setValue($value); 00209 if ($this->_encoding !== null) { 00210 $length = iconv_strlen($value, $this->_encoding); 00211 } else { 00212 $length = iconv_strlen($value); 00213 } 00214 00215 if ($length < $this->_min) { 00216 $this->_error(self::TOO_SHORT); 00217 } 00218 00219 if (null !== $this->_max && $this->_max < $length) { 00220 $this->_error(self::TOO_LONG); 00221 } 00222 00223 if (count($this->_messages)) { 00224 return false; 00225 } else { 00226 return true; 00227 } 00228 } 00229 }