|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00035 class Zend_Validate_File_Count extends Zend_Validate_Abstract 00036 { 00040 const TOO_MANY = 'fileCountTooMany'; 00041 const TOO_FEW = 'fileCountTooFew'; 00047 protected $_messageTemplates = array( 00048 self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given", 00049 self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given", 00050 ); 00051 00055 protected $_messageVariables = array( 00056 'min' => '_min', 00057 'max' => '_max', 00058 'count' => '_count' 00059 ); 00060 00068 protected $_min; 00069 00077 protected $_max; 00078 00084 protected $_count; 00085 00090 protected $_files; 00091 00106 public function __construct($options) 00107 { 00108 if ($options instanceof Zend_Config) { 00109 $options = $options->toArray(); 00110 } elseif (is_string($options) || is_numeric($options)) { 00111 $options = array('max' => $options); 00112 } elseif (!is_array($options)) { 00113 require_once 'Zend/Validate/Exception.php'; 00114 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00115 } 00116 00117 if (1 < func_num_args()) { 00118 $options['min'] = func_get_arg(0); 00119 $options['max'] = func_get_arg(1); 00120 } 00121 00122 if (isset($options['min'])) { 00123 $this->setMin($options); 00124 } 00125 00126 if (isset($options['max'])) { 00127 $this->setMax($options); 00128 } 00129 } 00130 00136 public function getMin() 00137 { 00138 return $this->_min; 00139 } 00140 00148 public function setMin($min) 00149 { 00150 if (is_array($min) and isset($min['min'])) { 00151 $min = $min['min']; 00152 } 00153 00154 if (!is_string($min) and !is_numeric($min)) { 00155 require_once 'Zend/Validate/Exception.php'; 00156 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00157 } 00158 00159 $min = (integer) $min; 00160 if (($this->_max !== null) && ($min > $this->_max)) { 00161 require_once 'Zend/Validate/Exception.php'; 00162 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >" 00163 . " {$this->_max}"); 00164 } 00165 00166 $this->_min = $min; 00167 return $this; 00168 } 00169 00175 public function getMax() 00176 { 00177 return $this->_max; 00178 } 00179 00187 public function setMax($max) 00188 { 00189 if (is_array($max) and isset($max['max'])) { 00190 $max = $max['max']; 00191 } 00192 00193 if (!is_string($max) and !is_numeric($max)) { 00194 require_once 'Zend/Validate/Exception.php'; 00195 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00196 } 00197 00198 $max = (integer) $max; 00199 if (($this->_min !== null) && ($max < $this->_min)) { 00200 require_once 'Zend/Validate/Exception.php'; 00201 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but " 00202 . "$max < {$this->_min}"); 00203 } 00204 00205 $this->_max = $max; 00206 return $this; 00207 } 00208 00214 public function addFile($file) 00215 { 00216 if (is_string($file)) { 00217 $file = array($file); 00218 } 00219 00220 if (is_array($file)) { 00221 foreach ($file as $name) { 00222 if (!isset($this->_files[$name]) && !empty($name)) { 00223 $this->_files[$name] = $name; 00224 } 00225 } 00226 } 00227 00228 return $this; 00229 } 00230 00242 public function isValid($value, $file = null) 00243 { 00244 if (($file !== null) && !array_key_exists('destination', $file)) { 00245 $file['destination'] = dirname($value); 00246 } 00247 00248 if (($file !== null) && array_key_exists('tmp_name', $file)) { 00249 $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name']; 00250 } 00251 00252 if (($file === null) || !empty($file['tmp_name'])) { 00253 $this->addFile($value); 00254 } 00255 00256 $this->_count = count($this->_files); 00257 if (($this->_max !== null) && ($this->_count > $this->_max)) { 00258 return $this->_throw($file, self::TOO_MANY); 00259 } 00260 00261 if (($this->_min !== null) && ($this->_count < $this->_min)) { 00262 return $this->_throw($file, self::TOO_FEW); 00263 } 00264 00265 return true; 00266 } 00267 00275 protected function _throw($file, $errorType) 00276 { 00277 if ($file !== null) { 00278 $this->_value = $file['name']; 00279 } 00280 00281 $this->_error($errorType); 00282 return false; 00283 } 00284 }