|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_InArray extends Zend_Validate_Abstract 00034 { 00035 const NOT_IN_ARRAY = 'notInArray'; 00036 00040 protected $_messageTemplates = array( 00041 self::NOT_IN_ARRAY => "'%value%' was not found in the haystack", 00042 ); 00043 00049 protected $_haystack; 00050 00056 protected $_strict = false; 00057 00063 protected $_recursive = false; 00064 00071 public function __construct($options) 00072 { 00073 if ($options instanceof Zend_Config) { 00074 $options = $options->toArray(); 00075 } else if (!is_array($options)) { 00076 require_once 'Zend/Validate/Exception.php'; 00077 throw new Zend_Validate_Exception('Array expected as parameter'); 00078 } else { 00079 $count = func_num_args(); 00080 $temp = array(); 00081 if ($count > 1) { 00082 $temp['haystack'] = func_get_arg(0); 00083 $temp['strict'] = func_get_arg(1); 00084 $options = $temp; 00085 } else { 00086 $temp = func_get_arg(0); 00087 if (!array_key_exists('haystack', $options)) { 00088 $options = array(); 00089 $options['haystack'] = $temp; 00090 } else { 00091 $options = $temp; 00092 } 00093 } 00094 } 00095 00096 $this->setHaystack($options['haystack']); 00097 if (array_key_exists('strict', $options)) { 00098 $this->setStrict($options['strict']); 00099 } 00100 00101 if (array_key_exists('recursive', $options)) { 00102 $this->setRecursive($options['recursive']); 00103 } 00104 } 00105 00111 public function getHaystack() 00112 { 00113 return $this->_haystack; 00114 } 00115 00122 public function setHaystack(array $haystack) 00123 { 00124 $this->_haystack = $haystack; 00125 return $this; 00126 } 00127 00133 public function getStrict() 00134 { 00135 return $this->_strict; 00136 } 00137 00144 public function setStrict($strict) 00145 { 00146 $this->_strict = (boolean) $strict; 00147 return $this; 00148 } 00149 00155 public function getRecursive() 00156 { 00157 return $this->_recursive; 00158 } 00159 00166 public function setRecursive($recursive) 00167 { 00168 $this->_recursive = (boolean) $recursive; 00169 return $this; 00170 } 00171 00181 public function isValid($value) 00182 { 00183 $this->_setValue($value); 00184 if ($this->getRecursive()) { 00185 $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack)); 00186 foreach($iterator as $element) { 00187 if ($this->_strict) { 00188 if ($element === $value) { 00189 return true; 00190 } 00191 } else if ($element == $value) { 00192 return true; 00193 } 00194 } 00195 } else { 00196 if (in_array($value, $this->_haystack, $this->_strict)) { 00197 return true; 00198 } 00199 } 00200 00201 $this->_error(self::NOT_IN_ARRAY); 00202 return false; 00203 } 00204 }