8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
17
* @package Zend_Validate
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: StringLength.php 8064 2008-02-16 10:58:39Z thomas $
25
* @see Zend_Validate_Abstract
27
require_once 'Zend/Validate/Abstract.php';
32
* @package Zend_Validate
33
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
34
* @license http://framework.zend.com/license/new-bsd New BSD License
36
class Zend_Validate_StringLength extends Zend_Validate_Abstract
39
const TOO_SHORT = 'stringLengthTooShort';
40
const TOO_LONG = 'stringLengthTooLong';
45
protected $_messageTemplates = array(
46
self::TOO_SHORT => "'%value%' is less than %min% characters long",
47
self::TOO_LONG => "'%value%' is greater than %max% characters long"
53
protected $_messageVariables = array(
68
* If null, there is no maximum length
75
* Sets validator options
81
public function __construct($min = 0, $max = null)
88
* Returns the min option
92
public function getMin()
100
* @param integer $min
101
* @throws Zend_Validate_Exception
102
* @return Zend_Validate_StringLength Provides a fluent interface
104
public function setMin($min)
106
if (null !== $this->_max && $min > $this->_max) {
108
* @see Zend_Validate_Exception
110
require_once 'Zend/Validate/Exception.php';
111
throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
114
$this->_min = max(0, (integer) $min);
119
* Returns the max option
121
* @return integer|null
123
public function getMax()
129
* Sets the max option
131
* @param integer|null $max
132
* @throws Zend_Validate_Exception
133
* @return Zend_Validate_StringLength Provides a fluent interface
135
public function setMax($max)
139
} else if ($max < $this->_min) {
141
* @see Zend_Validate_Exception
143
require_once 'Zend/Validate/Exception.php';
144
throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
145
. "$max < $this->_min");
147
$this->_max = (integer) $max;
154
* Defined by Zend_Validate_Interface
156
* Returns true if and only if the string length of $value is at least the min option and
157
* no greater than the max option (when the max option is not null).
159
* @param string $value
162
public function isValid($value)
164
$valueString = (string) $value;
165
$this->_setValue($valueString);
166
$length = iconv_strlen($valueString);
167
if ($length < $this->_min) {
168
$this->_error(self::TOO_SHORT);
170
if (null !== $this->_max && $this->_max < $length) {
171
$this->_error(self::TOO_LONG);
173
if (count($this->_messages)) {