[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 [email protected] so we can send you a copy immediately. 14 * 15 * @category Zend 16 * @package Zend_Validate 17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 18 * @license http://framework.zend.com/license/new-bsd New BSD License 19 * @version $Id$ 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-2010 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, value should be a string", 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 = iconv_get_encoding('internal_encoding'); 203 $result = iconv_set_encoding('internal_encoding', $encoding); 204 if (!$result) { 205 require_once 'Zend/Validate/Exception.php'; 206 throw new Zend_Validate_Exception('Given encoding not supported on this OS!'); 207 } 208 209 iconv_set_encoding('internal_encoding', $orig); 210 } 211 212 $this->_encoding = $encoding; 213 return $this; 214 } 215 216 /** 217 * Defined by Zend_Validate_Interface 218 * 219 * Returns true if and only if the string length of $value is at least the min option and 220 * no greater than the max option (when the max option is not null). 221 * 222 * @param string $value 223 * @return boolean 224 */ 225 public function isValid($value) 226 { 227 if (!is_string($value)) { 228 $this->_error(self::INVALID); 229 return false; 230 } 231 232 $this->_setValue($value); 233 if ($this->_encoding !== null) { 234 $length = iconv_strlen($value, $this->_encoding); 235 } else { 236 $length = iconv_strlen($value); 237 } 238 239 if ($length < $this->_min) { 240 $this->_error(self::TOO_SHORT); 241 } 242 243 if (null !== $this->_max && $this->_max < $length) { 244 $this->_error(self::TOO_LONG); 245 } 246 247 if (count($this->_messages)) { 248 return false; 249 } else { 250 return true; 251 } 252 } 253 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |