File indexing completed on 2024-05-12 06:03:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_StringLength extends Zend_Validate_Abstract
0034 {
0035     const INVALID   = 'stringLengthInvalid';
0036     const TOO_SHORT = 'stringLengthTooShort';
0037     const TOO_LONG  = 'stringLengthTooLong';
0038 
0039     /**
0040      * @var array
0041      */
0042     protected $_messageTemplates = array(
0043         self::INVALID   => "Invalid type given. String expected",
0044         self::TOO_SHORT => "'%value%' is less than %min% characters long",
0045         self::TOO_LONG  => "'%value%' is more than %max% characters long",
0046     );
0047 
0048     /**
0049      * @var array
0050      */
0051     protected $_messageVariables = array(
0052         'min' => '_min',
0053         'max' => '_max'
0054     );
0055 
0056     /**
0057      * Minimum length
0058      *
0059      * @var integer
0060      */
0061     protected $_min;
0062 
0063     /**
0064      * Maximum length
0065      *
0066      * If null, there is no maximum length
0067      *
0068      * @var integer|null
0069      */
0070     protected $_max;
0071 
0072     /**
0073      * Encoding to use
0074      *
0075      * @var string|null
0076      */
0077     protected $_encoding;
0078 
0079     /**
0080      * Sets validator options
0081      *
0082      * @param integer|array|Zend_Config $options
0083      */
0084     public function __construct($options = array())
0085     {
0086         if ($options instanceof Zend_Config) {
0087             $options = $options->toArray();
0088         } else if (!is_array($options)) {
0089             $options     = func_get_args();
0090             $temp['min'] = array_shift($options);
0091             if (!empty($options)) {
0092                 $temp['max'] = array_shift($options);
0093             }
0094 
0095             if (!empty($options)) {
0096                 $temp['encoding'] = array_shift($options);
0097             }
0098 
0099             $options = $temp;
0100         }
0101 
0102         if (!array_key_exists('min', $options)) {
0103             $options['min'] = 0;
0104         }
0105 
0106         $this->setMin($options['min']);
0107         if (array_key_exists('max', $options)) {
0108             $this->setMax($options['max']);
0109         }
0110 
0111         if (array_key_exists('encoding', $options)) {
0112             $this->setEncoding($options['encoding']);
0113         }
0114     }
0115 
0116     /**
0117      * Returns the min option
0118      *
0119      * @return integer
0120      */
0121     public function getMin()
0122     {
0123         return $this->_min;
0124     }
0125 
0126     /**
0127      * Sets the min option
0128      *
0129      * @param  integer $min
0130      * @throws Zend_Validate_Exception
0131      * @return Zend_Validate_StringLength Provides a fluent interface
0132      */
0133     public function setMin($min)
0134     {
0135         if (null !== $this->_max && $min > $this->_max) {
0136             /**
0137              * @see Zend_Validate_Exception
0138              */
0139             // require_once 'Zend/Validate/Exception.php';
0140             throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
0141                                             . " $this->_max");
0142         }
0143         $this->_min = max(0, (integer) $min);
0144         return $this;
0145     }
0146 
0147     /**
0148      * Returns the max option
0149      *
0150      * @return integer|null
0151      */
0152     public function getMax()
0153     {
0154         return $this->_max;
0155     }
0156 
0157     /**
0158      * Sets the max option
0159      *
0160      * @param  integer|null $max
0161      * @throws Zend_Validate_Exception
0162      * @return Zend_Validate_StringLength Provides a fluent interface
0163      */
0164     public function setMax($max)
0165     {
0166         if (null === $max) {
0167             $this->_max = null;
0168         } else if ($max < $this->_min) {
0169             /**
0170              * @see Zend_Validate_Exception
0171              */
0172             // require_once 'Zend/Validate/Exception.php';
0173             throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
0174                                             . "$max < $this->_min");
0175         } else {
0176             $this->_max = (integer) $max;
0177         }
0178 
0179         return $this;
0180     }
0181 
0182     /**
0183      * Returns the actual encoding
0184      *
0185      * @return string
0186      */
0187     public function getEncoding()
0188     {
0189         return $this->_encoding;
0190     }
0191 
0192     /**
0193      * Sets a new encoding to use
0194      *
0195      * @param string $encoding
0196      * @throws Zend_Validate_Exception
0197      * @return Zend_Validate_StringLength
0198      */
0199     public function setEncoding($encoding = null)
0200     {
0201         if ($encoding !== null) {
0202             $orig = PHP_VERSION_ID < 50600
0203                         ? iconv_get_encoding('internal_encoding')
0204                         : ini_get('default_charset');
0205             if (PHP_VERSION_ID < 50600) {
0206                 if ($encoding) {
0207                     $result = iconv_set_encoding('internal_encoding', $encoding);
0208                 } else {
0209                     $result = false;
0210                 }
0211             } else {
0212                 ini_set('default_charset', $encoding);
0213                 $result = ini_get('default_charset');
0214             }
0215             if (!$result) {
0216                 // require_once 'Zend/Validate/Exception.php';
0217                 throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
0218             }
0219 
0220             if (PHP_VERSION_ID < 50600) {
0221                 iconv_set_encoding('internal_encoding', $orig);
0222             } else {
0223                 ini_set('default_charset', $orig);
0224             }
0225         }
0226         $this->_encoding = $encoding;
0227         return $this;
0228     }
0229 
0230     /**
0231      * Defined by Zend_Validate_Interface
0232      *
0233      * Returns true if and only if the string length of $value is at least the min option and
0234      * no greater than the max option (when the max option is not null).
0235      *
0236      * @param  string $value
0237      * @return boolean
0238      */
0239     public function isValid($value)
0240     {
0241         if (!is_string($value)) {
0242             $this->_error(self::INVALID);
0243             return false;
0244         }
0245 
0246         $this->_setValue($value);
0247         if ($this->_encoding !== null) {
0248             $length = iconv_strlen($value, $this->_encoding);
0249         } else {
0250             $length = iconv_strlen($value);
0251         }
0252 
0253         if ($length < $this->_min) {
0254             $this->_error(self::TOO_SHORT);
0255         }
0256 
0257         if (null !== $this->_max && $this->_max < $length) {
0258             $this->_error(self::TOO_LONG);
0259         }
0260 
0261         if (count($this->_messages)) {
0262             return false;
0263         } else {
0264             return true;
0265         }
0266     }
0267 }