File indexing completed on 2024-12-22 05:37:12
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_Between extends Zend_Validate_Abstract 0034 { 0035 /** 0036 * Validation failure message key for when the value is not between the min and max, inclusively 0037 */ 0038 const NOT_BETWEEN = 'notBetween'; 0039 0040 /** 0041 * Validation failure message key for when the value is not strictly between the min and max 0042 */ 0043 const NOT_BETWEEN_STRICT = 'notBetweenStrict'; 0044 0045 /** 0046 * Validation failure message template definitions 0047 * 0048 * @var array 0049 */ 0050 protected $_messageTemplates = array( 0051 self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively", 0052 self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'" 0053 ); 0054 0055 /** 0056 * Additional variables available for validation failure messages 0057 * 0058 * @var array 0059 */ 0060 protected $_messageVariables = array( 0061 'min' => '_min', 0062 'max' => '_max' 0063 ); 0064 0065 /** 0066 * Minimum value 0067 * 0068 * @var mixed 0069 */ 0070 protected $_min; 0071 0072 /** 0073 * Maximum value 0074 * 0075 * @var mixed 0076 */ 0077 protected $_max; 0078 0079 /** 0080 * Whether to do inclusive comparisons, allowing equivalence to min and/or max 0081 * 0082 * If false, then strict comparisons are done, and the value may equal neither 0083 * the min nor max options 0084 * 0085 * @var boolean 0086 */ 0087 protected $_inclusive; 0088 0089 /** 0090 * Sets validator options 0091 * Accepts the following option keys: 0092 * 'min' => scalar, minimum border 0093 * 'max' => scalar, maximum border 0094 * 'inclusive' => boolean, inclusive border values 0095 * 0096 * @param array|Zend_Config $options 0097 * @throws Zend_Validate_Exception 0098 */ 0099 public function __construct($options) 0100 { 0101 if ($options instanceof Zend_Config) { 0102 $options = $options->toArray(); 0103 } else if (!is_array($options)) { 0104 $options = func_get_args(); 0105 $temp['min'] = array_shift($options); 0106 if (!empty($options)) { 0107 $temp['max'] = array_shift($options); 0108 } 0109 0110 if (!empty($options)) { 0111 $temp['inclusive'] = array_shift($options); 0112 } 0113 0114 $options = $temp; 0115 } 0116 0117 if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) { 0118 // require_once 'Zend/Validate/Exception.php'; 0119 throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given"); 0120 } 0121 0122 if (!array_key_exists('inclusive', $options)) { 0123 $options['inclusive'] = true; 0124 } 0125 0126 $this->setMin($options['min']) 0127 ->setMax($options['max']) 0128 ->setInclusive($options['inclusive']); 0129 } 0130 0131 /** 0132 * Returns the min option 0133 * 0134 * @return mixed 0135 */ 0136 public function getMin() 0137 { 0138 return $this->_min; 0139 } 0140 0141 /** 0142 * Sets the min option 0143 * 0144 * @param mixed $min 0145 * @return Zend_Validate_Between Provides a fluent interface 0146 */ 0147 public function setMin($min) 0148 { 0149 $this->_min = $min; 0150 return $this; 0151 } 0152 0153 /** 0154 * Returns the max option 0155 * 0156 * @return mixed 0157 */ 0158 public function getMax() 0159 { 0160 return $this->_max; 0161 } 0162 0163 /** 0164 * Sets the max option 0165 * 0166 * @param mixed $max 0167 * @return Zend_Validate_Between Provides a fluent interface 0168 */ 0169 public function setMax($max) 0170 { 0171 $this->_max = $max; 0172 return $this; 0173 } 0174 0175 /** 0176 * Returns the inclusive option 0177 * 0178 * @return boolean 0179 */ 0180 public function getInclusive() 0181 { 0182 return $this->_inclusive; 0183 } 0184 0185 /** 0186 * Sets the inclusive option 0187 * 0188 * @param boolean $inclusive 0189 * @return Zend_Validate_Between Provides a fluent interface 0190 */ 0191 public function setInclusive($inclusive) 0192 { 0193 $this->_inclusive = $inclusive; 0194 return $this; 0195 } 0196 0197 /** 0198 * Defined by Zend_Validate_Interface 0199 * 0200 * Returns true if and only if $value is between min and max options, inclusively 0201 * if inclusive option is true. 0202 * 0203 * @param mixed $value 0204 * @return boolean 0205 */ 0206 public function isValid($value) 0207 { 0208 $this->_setValue($value); 0209 0210 if ($this->_inclusive) { 0211 if ($this->_min > $value || $value > $this->_max) { 0212 $this->_error(self::NOT_BETWEEN); 0213 return false; 0214 } 0215 } else { 0216 if ($this->_min >= $value || $value >= $this->_max) { 0217 $this->_error(self::NOT_BETWEEN_STRICT); 0218 return false; 0219 } 0220 } 0221 return true; 0222 } 0223 0224 }