7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
29 * @package Zend_Validate
30 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Validate_StringLength
extends Zend_Validate_Abstract
35 const INVALID
= 'stringLengthInvalid';
36 const TOO_SHORT
= 'stringLengthTooShort';
37 const TOO_LONG
= 'stringLengthTooLong';
42 protected $_messageTemplates = array(
43 self
::INVALID
=> "Invalid type given, value should be a string",
44 self
::TOO_SHORT
=> "'%value%' is less than %min% characters long",
45 self
::TOO_LONG
=> "'%value%' is greater than %max% characters long"
51 protected $_messageVariables = array(
66 * If null, there is no maximum length
80 * Sets validator options
86 public function __construct($min = 0, $max = null, $encoding = null)
90 $this->setEncoding($encoding);
94 * Returns the min option
98 public function getMin()
104 * Sets the min option
106 * @param integer $min
107 * @throws Zend_Validate_Exception
108 * @return Zend_Validate_StringLength Provides a fluent interface
110 public function setMin($min)
112 if (null !== $this->_max
&& $min > $this->_max
) {
114 * @see Zend_Validate_Exception
116 require_once 'Zend/Validate/Exception.php';
117 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
120 $this->_min
= max(0, (integer) $min);
125 * Returns the max option
127 * @return integer|null
129 public function getMax()
135 * Sets the max option
137 * @param integer|null $max
138 * @throws Zend_Validate_Exception
139 * @return Zend_Validate_StringLength Provides a fluent interface
141 public function setMax($max)
145 } else if ($max < $this->_min
) {
147 * @see Zend_Validate_Exception
149 require_once 'Zend/Validate/Exception.php';
150 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
151 . "$max < $this->_min");
153 $this->_max
= (integer) $max;
160 * Returns the actual encoding
164 public function getEncoding()
166 return $this->_encoding
;
170 * Sets a new encoding to use
172 * @param string $encoding
173 * @return Zend_Validate_StringLength
175 public function setEncoding($encoding = null)
177 if ($encoding !== null) {
178 $orig = iconv_get_encoding('internal_encoding');
179 $result = iconv_set_encoding('internal_encoding', $encoding);
181 require_once 'Zend/Validate/Exception.php';
182 throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
185 iconv_set_encoding('internal_encoding', $orig);
188 $this->_encoding
= $encoding;
193 * Defined by Zend_Validate_Interface
195 * Returns true if and only if the string length of $value is at least the min option and
196 * no greater than the max option (when the max option is not null).
198 * @param string $value
201 public function isValid($value)
203 if (!is_string($value)) {
204 $this->_error(self
::INVALID
);
208 $this->_setValue($value);
209 if ($this->_encoding
!== null) {
210 $length = iconv_strlen($value, $this->_encoding
);
212 $length = iconv_strlen($value);
215 if ($length < $this->_min
) {
216 $this->_error(self
::TOO_SHORT
);
219 if (null !== $this->_max
&& $this->_max
< $length) {
220 $this->_error(self
::TOO_LONG
);
223 if (count($this->_messages
)) {