[ZF-6295] Generic:
[zend.git] / library / Zend / Validate / StringLength.php
blobb67d5292b2a250298d3d87cc5783c3a0e4456d25
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
19 * @version $Id$
22 /**
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
27 /**
28 * @category Zend
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';
39 /**
40 * @var array
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"
48 /**
49 * @var array
51 protected $_messageVariables = array(
52 'min' => '_min',
53 'max' => '_max'
56 /**
57 * Minimum length
59 * @var integer
61 protected $_min;
63 /**
64 * Maximum length
66 * If null, there is no maximum length
68 * @var integer|null
70 protected $_max;
72 /**
73 * Encoding to use
75 * @var string|null
77 protected $_encoding;
79 /**
80 * Sets validator options
82 * @param integer $min
83 * @param integer $max
84 * @return void
86 public function __construct($min = 0, $max = null, $encoding = null)
88 $this->setMin($min);
89 $this->setMax($max);
90 $this->setEncoding($encoding);
93 /**
94 * Returns the min option
96 * @return integer
98 public function getMin()
100 return $this->_min;
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 >"
118 . " $this->_max");
120 $this->_min = max(0, (integer) $min);
121 return $this;
125 * Returns the max option
127 * @return integer|null
129 public function getMax()
131 return $this->_max;
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)
143 if (null === $max) {
144 $this->_max = null;
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");
152 } else {
153 $this->_max = (integer) $max;
156 return $this;
160 * Returns the actual encoding
162 * @return string
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);
180 if (!$result) {
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;
189 return $this;
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
199 * @return boolean
201 public function isValid($value)
203 if (!is_string($value)) {
204 $this->_error(self::INVALID);
205 return false;
208 $this->_setValue($value);
209 if ($this->_encoding !== null) {
210 $length = iconv_strlen($value, $this->_encoding);
211 } else {
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)) {
224 return false;
225 } else {
226 return true;