File indexing completed on 2025-01-19 05:21:38
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 * Validator for counting all given files 0029 * 0030 * @category Zend 0031 * @package Zend_Validate 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_Validate_File_Count extends Zend_Validate_Abstract 0036 { 0037 /**#@+ 0038 * @const string Error constants 0039 */ 0040 const TOO_MANY = 'fileCountTooMany'; 0041 const TOO_FEW = 'fileCountTooFew'; 0042 /**#@-*/ 0043 0044 /** 0045 * @var array Error message templates 0046 */ 0047 protected $_messageTemplates = array( 0048 self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given", 0049 self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given", 0050 ); 0051 0052 /** 0053 * @var array Error message template variables 0054 */ 0055 protected $_messageVariables = array( 0056 'min' => '_min', 0057 'max' => '_max', 0058 'count' => '_count' 0059 ); 0060 0061 /** 0062 * Minimum file count 0063 * 0064 * If null, there is no minimum file count 0065 * 0066 * @var integer 0067 */ 0068 protected $_min; 0069 0070 /** 0071 * Maximum file count 0072 * 0073 * If null, there is no maximum file count 0074 * 0075 * @var integer|null 0076 */ 0077 protected $_max; 0078 0079 /** 0080 * Actual filecount 0081 * 0082 * @var integer 0083 */ 0084 protected $_count; 0085 0086 /** 0087 * Internal file array 0088 * @var array 0089 */ 0090 protected $_files; 0091 0092 /** 0093 * Sets validator options 0094 * 0095 * Min limits the file count, when used with max=null it is the maximum file count 0096 * It also accepts an array with the keys 'min' and 'max' 0097 * 0098 * If $options is a integer, it will be used as maximum file count 0099 * As Array is accepts the following keys: 0100 * 'min': Minimum filecount 0101 * 'max': Maximum filecount 0102 * 0103 * @param integer|array|Zend_Config $options Options for the adapter 0104 * @throws Zend_Validate_Exception 0105 */ 0106 public function __construct($options) 0107 { 0108 if ($options instanceof Zend_Config) { 0109 $options = $options->toArray(); 0110 } elseif (is_string($options) || is_numeric($options)) { 0111 $options = array('max' => $options); 0112 } elseif (!is_array($options)) { 0113 // require_once 'Zend/Validate/Exception.php'; 0114 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0115 } 0116 0117 if (1 < func_num_args()) { 0118 $options['min'] = func_get_arg(0); 0119 $options['max'] = func_get_arg(1); 0120 } 0121 0122 if (isset($options['min'])) { 0123 $this->setMin($options); 0124 } 0125 0126 if (isset($options['max'])) { 0127 $this->setMax($options); 0128 } 0129 } 0130 0131 /** 0132 * Returns the minimum file count 0133 * 0134 * @return integer 0135 */ 0136 public function getMin() 0137 { 0138 return $this->_min; 0139 } 0140 0141 /** 0142 * Sets the minimum file count 0143 * 0144 * @param integer|array $min The minimum file count 0145 * @return Zend_Validate_File_Count Provides a fluent interface 0146 * @throws Zend_Validate_Exception When min is greater than max 0147 */ 0148 public function setMin($min) 0149 { 0150 if (is_array($min) and isset($min['min'])) { 0151 $min = $min['min']; 0152 } 0153 0154 if (!is_string($min) and !is_numeric($min)) { 0155 // require_once 'Zend/Validate/Exception.php'; 0156 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0157 } 0158 0159 $min = (integer) $min; 0160 if (($this->_max !== null) && ($min > $this->_max)) { 0161 // require_once 'Zend/Validate/Exception.php'; 0162 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >" 0163 . " {$this->_max}"); 0164 } 0165 0166 $this->_min = $min; 0167 return $this; 0168 } 0169 0170 /** 0171 * Returns the maximum file count 0172 * 0173 * @return integer 0174 */ 0175 public function getMax() 0176 { 0177 return $this->_max; 0178 } 0179 0180 /** 0181 * Sets the maximum file count 0182 * 0183 * @param integer|array $max The maximum file count 0184 * @return Zend_Validate_StringLength Provides a fluent interface 0185 * @throws Zend_Validate_Exception When max is smaller than min 0186 */ 0187 public function setMax($max) 0188 { 0189 if (is_array($max) and isset($max['max'])) { 0190 $max = $max['max']; 0191 } 0192 0193 if (!is_string($max) and !is_numeric($max)) { 0194 // require_once 'Zend/Validate/Exception.php'; 0195 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0196 } 0197 0198 $max = (integer) $max; 0199 if (($this->_min !== null) && ($max < $this->_min)) { 0200 // require_once 'Zend/Validate/Exception.php'; 0201 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but " 0202 . "$max < {$this->_min}"); 0203 } 0204 0205 $this->_max = $max; 0206 return $this; 0207 } 0208 0209 /** 0210 * Adds a file for validation 0211 * 0212 * @param string|array $file 0213 * @return $this 0214 */ 0215 public function addFile($file) 0216 { 0217 if (is_string($file)) { 0218 $file = array($file); 0219 } 0220 0221 if (is_array($file)) { 0222 foreach ($file as $name) { 0223 if (!isset($this->_files[$name]) && !empty($name)) { 0224 $this->_files[$name] = $name; 0225 } 0226 } 0227 } 0228 0229 return $this; 0230 } 0231 0232 /** 0233 * Defined by Zend_Validate_Interface 0234 * 0235 * Returns true if and only if the file count of all checked files is at least min and 0236 * not bigger than max (when max is not null). Attention: When checking with set min you 0237 * must give all files with the first call, otherwise you will get an false. 0238 * 0239 * @param string|array $value Filenames to check for count 0240 * @param array $file File data from Zend_File_Transfer 0241 * @return boolean 0242 */ 0243 public function isValid($value, $file = null) 0244 { 0245 if (($file !== null) && !array_key_exists('destination', $file)) { 0246 $file['destination'] = dirname($value); 0247 } 0248 0249 if (($file !== null) && array_key_exists('tmp_name', $file)) { 0250 $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name']; 0251 } 0252 0253 if (($file === null) || !empty($file['tmp_name'])) { 0254 $this->addFile($value); 0255 } 0256 0257 $this->_count = count($this->_files); 0258 if (($this->_max !== null) && ($this->_count > $this->_max)) { 0259 return $this->_throw($file, self::TOO_MANY); 0260 } 0261 0262 if (($this->_min !== null) && ($this->_count < $this->_min)) { 0263 return $this->_throw($file, self::TOO_FEW); 0264 } 0265 0266 return true; 0267 } 0268 0269 /** 0270 * Throws an error of the given type 0271 * 0272 * @param string $file 0273 * @param string $errorType 0274 * @return false 0275 */ 0276 protected function _throw($file, $errorType) 0277 { 0278 if ($file !== null) { 0279 $this->_value = $file['name']; 0280 } 0281 0282 $this->_error($errorType); 0283 return false; 0284 } 0285 }