Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/StringLength.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00033 class Zend_Validate_StringLength extends Zend_Validate_Abstract
00034 {
00035     const INVALID   = 'stringLengthInvalid';
00036     const TOO_SHORT = 'stringLengthTooShort';
00037     const TOO_LONG  = 'stringLengthTooLong';
00038 
00042     protected $_messageTemplates = array(
00043         self::INVALID   => "Invalid type given, value should be a string",
00044         self::TOO_SHORT => "'%value%' is less than %min% characters long",
00045         self::TOO_LONG  => "'%value%' is more than %max% characters long",
00046     );
00047 
00051     protected $_messageVariables = array(
00052         'min' => '_min',
00053         'max' => '_max'
00054     );
00055 
00061     protected $_min;
00062 
00070     protected $_max;
00071 
00077     protected $_encoding;
00078 
00085     public function __construct($options = array())
00086     {
00087         if ($options instanceof Zend_Config) {
00088             $options = $options->toArray();
00089         } else if (!is_array($options)) {
00090             $options     = func_get_args();
00091             $temp['min'] = array_shift($options);
00092             if (!empty($options)) {
00093                 $temp['max'] = array_shift($options);
00094             }
00095 
00096             if (!empty($options)) {
00097                 $temp['encoding'] = array_shift($options);
00098             }
00099 
00100             $options = $temp;
00101         }
00102 
00103         if (!array_key_exists('min', $options)) {
00104             $options['min'] = 0;
00105         }
00106 
00107         $this->setMin($options['min']);
00108         if (array_key_exists('max', $options)) {
00109             $this->setMax($options['max']);
00110         }
00111 
00112         if (array_key_exists('encoding', $options)) {
00113             $this->setEncoding($options['encoding']);
00114         }
00115     }
00116 
00122     public function getMin()
00123     {
00124         return $this->_min;
00125     }
00126 
00134     public function setMin($min)
00135     {
00136         if (null !== $this->_max && $min > $this->_max) {
00140             require_once 'Zend/Validate/Exception.php';
00141             throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
00142                                             . " $this->_max");
00143         }
00144         $this->_min = max(0, (integer) $min);
00145         return $this;
00146     }
00147 
00153     public function getMax()
00154     {
00155         return $this->_max;
00156     }
00157 
00165     public function setMax($max)
00166     {
00167         if (null === $max) {
00168             $this->_max = null;
00169         } else if ($max < $this->_min) {
00173             require_once 'Zend/Validate/Exception.php';
00174             throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
00175                                             . "$max < $this->_min");
00176         } else {
00177             $this->_max = (integer) $max;
00178         }
00179 
00180         return $this;
00181     }
00182 
00188     public function getEncoding()
00189     {
00190         return $this->_encoding;
00191     }
00192 
00199     public function setEncoding($encoding = null)
00200     {
00201         if ($encoding !== null) {
00202             $orig   = iconv_get_encoding('internal_encoding');
00203             $result = iconv_set_encoding('internal_encoding', $encoding);
00204             if (!$result) {
00205                 require_once 'Zend/Validate/Exception.php';
00206                 throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
00207             }
00208 
00209             iconv_set_encoding('internal_encoding', $orig);
00210         }
00211 
00212         $this->_encoding = $encoding;
00213         return $this;
00214     }
00215 
00225     public function isValid($value)
00226     {
00227         if (!is_string($value)) {
00228             $this->_error(self::INVALID);
00229             return false;
00230         }
00231 
00232         $this->_setValue($value);
00233         if ($this->_encoding !== null) {
00234             $length = iconv_strlen($value, $this->_encoding);
00235         } else {
00236             $length = iconv_strlen($value);
00237         }
00238 
00239         if ($length < $this->_min) {
00240             $this->_error(self::TOO_SHORT);
00241         }
00242 
00243         if (null !== $this->_max && $this->_max < $length) {
00244             $this->_error(self::TOO_LONG);
00245         }
00246 
00247         if (count($this->_messages)) {
00248             return false;
00249         } else {
00250             return true;
00251         }
00252     }
00253 }
 All Data Structures Namespaces Files Functions Variables Enumerations