File
6 years ago
Hostname
6 years ago
Abstract.php
6 years ago
Alnum.php
6 years ago
Alpha.php
6 years ago
Between.php
6 years ago
Callback.php
6 years ago
Ccnum.php
6 years ago
CreditCard.php
6 years ago
Date.php
6 years ago
Digits.php
6 years ago
Exception.php
6 years ago
Float.php
6 years ago
GreaterThan.php
6 years ago
Hex.php
6 years ago
Hostname.php
6 years ago
Iban.php
6 years ago
Identical.php
6 years ago
InArray.php
6 years ago
Int.php
6 years ago
Interface.php
6 years ago
Ip.php
6 years ago
Isbn.php
6 years ago
LessThan.php
6 years ago
NotEmpty.php
6 years ago
PostCode.php
6 years ago
Regex.php
6 years ago
StringLength.php
6 years ago
StringLength.php
264 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Zend Framework |
| 4 | * |
| 5 | * LICENSE |
| 6 | * |
| 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. |
| 14 | * |
| 15 | * @category Zend |
| 16 | * @package Zend_Validate |
| 17 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 19 | * @version $Id: StringLength.php 23775 2011-03-01 17:25:24Z ralph $ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @see Zend_Validate_Abstract |
| 24 | */ |
| 25 | // require_once 'Zend/Validate/Abstract.php'; |
| 26 | |
| 27 | /** |
| 28 | * @category Zend |
| 29 | * @package Zend_Validate |
| 30 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 31 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 32 | */ |
| 33 | class Zend_Validate_StringLength extends Zend_Validate_Abstract |
| 34 | { |
| 35 | const INVALID = 'stringLengthInvalid'; |
| 36 | const TOO_SHORT = 'stringLengthTooShort'; |
| 37 | const TOO_LONG = 'stringLengthTooLong'; |
| 38 | |
| 39 | /** |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $_messageTemplates = array( |
| 43 | self::INVALID => "Invalid type given. String expected", |
| 44 | self::TOO_SHORT => "'%value%' is less than %min% characters long", |
| 45 | self::TOO_LONG => "'%value%' is more than %max% characters long", |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * @var array |
| 50 | */ |
| 51 | protected $_messageVariables = array( |
| 52 | 'min' => '_min', |
| 53 | 'max' => '_max' |
| 54 | ); |
| 55 | |
| 56 | /** |
| 57 | * Minimum length |
| 58 | * |
| 59 | * @var integer |
| 60 | */ |
| 61 | protected $_min; |
| 62 | |
| 63 | /** |
| 64 | * Maximum length |
| 65 | * |
| 66 | * If null, there is no maximum length |
| 67 | * |
| 68 | * @var integer|null |
| 69 | */ |
| 70 | protected $_max; |
| 71 | |
| 72 | /** |
| 73 | * Encoding to use |
| 74 | * |
| 75 | * @var string|null |
| 76 | */ |
| 77 | protected $_encoding; |
| 78 | |
| 79 | /** |
| 80 | * Sets validator options |
| 81 | * |
| 82 | * @param integer|array|Zend_Config $options |
| 83 | * @return void |
| 84 | */ |
| 85 | public function __construct($options = array()) |
| 86 | { |
| 87 | if ($options instanceof Zend_Config) { |
| 88 | $options = $options->toArray(); |
| 89 | } else if (!is_array($options)) { |
| 90 | $options = func_get_args(); |
| 91 | $temp['min'] = array_shift($options); |
| 92 | if (!empty($options)) { |
| 93 | $temp['max'] = array_shift($options); |
| 94 | } |
| 95 | |
| 96 | if (!empty($options)) { |
| 97 | $temp['encoding'] = array_shift($options); |
| 98 | } |
| 99 | |
| 100 | $options = $temp; |
| 101 | } |
| 102 | |
| 103 | if (!array_key_exists('min', $options)) { |
| 104 | $options['min'] = 0; |
| 105 | } |
| 106 | |
| 107 | $this->setMin($options['min']); |
| 108 | if (array_key_exists('max', $options)) { |
| 109 | $this->setMax($options['max']); |
| 110 | } |
| 111 | |
| 112 | if (array_key_exists('encoding', $options)) { |
| 113 | $this->setEncoding($options['encoding']); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the min option |
| 119 | * |
| 120 | * @return integer |
| 121 | */ |
| 122 | public function getMin() |
| 123 | { |
| 124 | return $this->_min; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets the min option |
| 129 | * |
| 130 | * @param integer $min |
| 131 | * @throws Zend_Validate_Exception |
| 132 | * @return Zend_Validate_StringLength Provides a fluent interface |
| 133 | */ |
| 134 | public function setMin($min) |
| 135 | { |
| 136 | if (null !== $this->_max && $min > $this->_max) { |
| 137 | /** |
| 138 | * @see Zend_Validate_Exception |
| 139 | */ |
| 140 | // require_once 'Zend/Validate/Exception.php'; |
| 141 | throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >" |
| 142 | . " $this->_max"); |
| 143 | } |
| 144 | $this->_min = max(0, (integer) $min); |
| 145 | return $this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the max option |
| 150 | * |
| 151 | * @return integer|null |
| 152 | */ |
| 153 | public function getMax() |
| 154 | { |
| 155 | return $this->_max; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets the max option |
| 160 | * |
| 161 | * @param integer|null $max |
| 162 | * @throws Zend_Validate_Exception |
| 163 | * @return Zend_Validate_StringLength Provides a fluent interface |
| 164 | */ |
| 165 | public function setMax($max) |
| 166 | { |
| 167 | if (null === $max) { |
| 168 | $this->_max = null; |
| 169 | } else if ($max < $this->_min) { |
| 170 | /** |
| 171 | * @see Zend_Validate_Exception |
| 172 | */ |
| 173 | // require_once 'Zend/Validate/Exception.php'; |
| 174 | throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but " |
| 175 | . "$max < $this->_min"); |
| 176 | } else { |
| 177 | $this->_max = (integer) $max; |
| 178 | } |
| 179 | |
| 180 | return $this; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the actual encoding |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | public function getEncoding() |
| 189 | { |
| 190 | return $this->_encoding; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets a new encoding to use |
| 195 | * |
| 196 | * @param string $encoding |
| 197 | * @return Zend_Validate_StringLength |
| 198 | */ |
| 199 | public function setEncoding($encoding = null) |
| 200 | { |
| 201 | if ($encoding !== null) { |
| 202 | $orig = PHP_VERSION_ID < 50600 |
| 203 | ? iconv_get_encoding('internal_encoding') |
| 204 | : ini_get('default_charset'); |
| 205 | if (PHP_VERSION_ID < 50600) { |
| 206 | $result = iconv_set_encoding('internal_encoding', $encoding); |
| 207 | } else { |
| 208 | $result = @ini_set('default_charset', $encoding); |
| 209 | } |
| 210 | if (!$result) { |
| 211 | require_once 'Zend/Validate/Exception.php'; |
| 212 | throw new Zend_Validate_Exception('Given encoding not supported on this OS!'); |
| 213 | } |
| 214 | |
| 215 | if (PHP_VERSION_ID < 50600) { |
| 216 | iconv_set_encoding('internal_encoding', $orig); |
| 217 | } else { |
| 218 | @ini_set('default_charset', $orig); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $this->_encoding = $encoding; |
| 223 | return $this; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Defined by Zend_Validate_Interface |
| 228 | * |
| 229 | * Returns true if and only if the string length of $value is at least the min option and |
| 230 | * no greater than the max option (when the max option is not null). |
| 231 | * |
| 232 | * @param string $value |
| 233 | * @return boolean |
| 234 | */ |
| 235 | public function isValid($value) |
| 236 | { |
| 237 | if (!is_string($value)) { |
| 238 | $this->_error(self::INVALID); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | $this->_setValue($value); |
| 243 | if ($this->_encoding !== null) { |
| 244 | $length = iconv_strlen($value, $this->_encoding); |
| 245 | } else { |
| 246 | $length = iconv_strlen($value); |
| 247 | } |
| 248 | |
| 249 | if ($length < $this->_min) { |
| 250 | $this->_error(self::TOO_SHORT); |
| 251 | } |
| 252 | |
| 253 | if (null !== $this->_max && $this->_max < $length) { |
| 254 | $this->_error(self::TOO_LONG); |
| 255 | } |
| 256 | |
| 257 | if (count($this->_messages)) { |
| 258 | return false; |
| 259 | } else { |
| 260 | return true; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 |