|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Between extends Zend_Validate_Abstract 00034 { 00038 const NOT_BETWEEN = 'notBetween'; 00039 00043 const NOT_BETWEEN_STRICT = 'notBetweenStrict'; 00044 00050 protected $_messageTemplates = array( 00051 self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively", 00052 self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'" 00053 ); 00054 00060 protected $_messageVariables = array( 00061 'min' => '_min', 00062 'max' => '_max' 00063 ); 00064 00070 protected $_min; 00071 00077 protected $_max; 00078 00087 protected $_inclusive; 00088 00099 public function __construct($options) 00100 { 00101 if ($options instanceof Zend_Config) { 00102 $options = $options->toArray(); 00103 } else if (!is_array($options)) { 00104 $options = func_get_args(); 00105 $temp['min'] = array_shift($options); 00106 if (!empty($options)) { 00107 $temp['max'] = array_shift($options); 00108 } 00109 00110 if (!empty($options)) { 00111 $temp['inclusive'] = array_shift($options); 00112 } 00113 00114 $options = $temp; 00115 } 00116 00117 if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) { 00118 require_once 'Zend/Validate/Exception.php'; 00119 throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given"); 00120 } 00121 00122 if (!array_key_exists('inclusive', $options)) { 00123 $options['inclusive'] = true; 00124 } 00125 00126 $this->setMin($options['min']) 00127 ->setMax($options['max']) 00128 ->setInclusive($options['inclusive']); 00129 } 00130 00136 public function getMin() 00137 { 00138 return $this->_min; 00139 } 00140 00147 public function setMin($min) 00148 { 00149 $this->_min = $min; 00150 return $this; 00151 } 00152 00158 public function getMax() 00159 { 00160 return $this->_max; 00161 } 00162 00169 public function setMax($max) 00170 { 00171 $this->_max = $max; 00172 return $this; 00173 } 00174 00180 public function getInclusive() 00181 { 00182 return $this->_inclusive; 00183 } 00184 00191 public function setInclusive($inclusive) 00192 { 00193 $this->_inclusive = $inclusive; 00194 return $this; 00195 } 00196 00206 public function isValid($value) 00207 { 00208 $this->_setValue($value); 00209 00210 if ($this->_inclusive) { 00211 if ($this->_min > $value || $value > $this->_max) { 00212 $this->_error(self::NOT_BETWEEN); 00213 return false; 00214 } 00215 } else { 00216 if ($this->_min >= $value || $value >= $this->_max) { 00217 $this->_error(self::NOT_BETWEEN_STRICT); 00218 return false; 00219 } 00220 } 00221 return true; 00222 } 00223 00224 }