00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026
00035 class Zend_Validate_File_Count extends Zend_Validate_Abstract
00036 {
00040 const TOO_MUCH = 'fileCountTooMuch';
00041 const TOO_LESS = 'fileCountTooLess';
00047 protected $_messageTemplates = array(
00048 self::TOO_MUCH => "Too much files, maximum '%max%' are allowed but '%count%' are given",
00049 self::TOO_LESS => "Too less 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
00119
00120 $options['min'] = func_get_arg(0);
00121 $options['max'] = func_get_arg(1);
00122 }
00123
00124 if (isset($options['min'])) {
00125 $this->setMin($options);
00126 }
00127
00128 if (isset($options['max'])) {
00129 $this->setMax($options);
00130 }
00131 }
00132
00138 public function getMin()
00139 {
00140 return $this->_min;
00141 }
00142
00150 public function setMin($min)
00151 {
00152 if (is_array($min) and isset($min['min'])) {
00153 $min = $min['min'];
00154 }
00155
00156 if (!is_string($min) and !is_numeric($min)) {
00157 require_once 'Zend/Validate/Exception.php';
00158 throw new Zend_Validate_Exception ('Invalid options to validator provided');
00159 }
00160
00161 $min = (integer) $min;
00162 if (($this->_max !== null) && ($min > $this->_max)) {
00163 require_once 'Zend/Validate/Exception.php';
00164 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >"
00165 . " {$this->_max}");
00166 }
00167
00168 $this->_min = $min;
00169 return $this;
00170 }
00171
00177 public function getMax()
00178 {
00179 return $this->_max;
00180 }
00181
00189 public function setMax($max)
00190 {
00191 if (is_array($max) and isset($max['max'])) {
00192 $max = $max['max'];
00193 }
00194
00195 if (!is_string($max) and !is_numeric($max)) {
00196 require_once 'Zend/Validate/Exception.php';
00197 throw new Zend_Validate_Exception ('Invalid options to validator provided');
00198 }
00199
00200 $max = (integer) $max;
00201 if (($this->_min !== null) && ($max < $this->_min)) {
00202 require_once 'Zend/Validate/Exception.php';
00203 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but "
00204 . "$max < {$this->_min}");
00205 }
00206
00207 $this->_max = $max;
00208 return $this;
00209 }
00210
00216 public function addFile($file)
00217 {
00218 if (is_string($file)) {
00219 $file = array($file);
00220 }
00221
00222 if (is_array($file)) {
00223 foreach ($file as $name) {
00224 if (!isset($this->_files[$name]) && !empty($name)) {
00225 $this->_files[$name] = $name;
00226 }
00227 }
00228 }
00229
00230 return $this;
00231 }
00232
00244 public function isValid($value, $file = null)
00245 {
00246 $this->addFile($value);
00247 $this->_count = count($this->_files);
00248 if (($this->_max !== null) && ($this->_count > $this->_max)) {
00249 return $this->_throw($file, self::TOO_MUCH);
00250 }
00251
00252 if (($this->_min !== null) && ($this->_count < $this->_min)) {
00253 return $this->_throw($file, self::TOO_LESS);
00254 }
00255
00256 return true;
00257 }
00258
00266 protected function _throw($file, $errorType)
00267 {
00268 if ($file !== null) {
00269 $this->_value = $file['name'];
00270 }
00271
00272 $this->_error($errorType);
00273 return false;
00274 }
00275 }