1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Validate |
---|
17 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Count.php 21325 2010-03-04 20:26:36Z thomas $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * @see Zend_Validate_Abstract |
---|
24 | */ |
---|
25 | require_once 'Zend/Validate/Abstract.php'; |
---|
26 | |
---|
27 | /** |
---|
28 | * Validator for counting all given files |
---|
29 | * |
---|
30 | * @category Zend |
---|
31 | * @package Zend_Validate |
---|
32 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
33 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
34 | */ |
---|
35 | class Zend_Validate_File_Count extends Zend_Validate_Abstract |
---|
36 | { |
---|
37 | /**#@+ |
---|
38 | * @const string Error constants |
---|
39 | */ |
---|
40 | const TOO_MANY = 'fileCountTooMany'; |
---|
41 | const TOO_FEW = 'fileCountTooFew'; |
---|
42 | /**#@-*/ |
---|
43 | |
---|
44 | /** |
---|
45 | * @var array Error message templates |
---|
46 | */ |
---|
47 | protected $_messageTemplates = array( |
---|
48 | self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given", |
---|
49 | self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given", |
---|
50 | ); |
---|
51 | |
---|
52 | /** |
---|
53 | * @var array Error message template variables |
---|
54 | */ |
---|
55 | protected $_messageVariables = array( |
---|
56 | 'min' => '_min', |
---|
57 | 'max' => '_max', |
---|
58 | 'count' => '_count' |
---|
59 | ); |
---|
60 | |
---|
61 | /** |
---|
62 | * Minimum file count |
---|
63 | * |
---|
64 | * If null, there is no minimum file count |
---|
65 | * |
---|
66 | * @var integer |
---|
67 | */ |
---|
68 | protected $_min; |
---|
69 | |
---|
70 | /** |
---|
71 | * Maximum file count |
---|
72 | * |
---|
73 | * If null, there is no maximum file count |
---|
74 | * |
---|
75 | * @var integer|null |
---|
76 | */ |
---|
77 | protected $_max; |
---|
78 | |
---|
79 | /** |
---|
80 | * Actual filecount |
---|
81 | * |
---|
82 | * @var integer |
---|
83 | */ |
---|
84 | protected $_count; |
---|
85 | |
---|
86 | /** |
---|
87 | * Internal file array |
---|
88 | * @var array |
---|
89 | */ |
---|
90 | protected $_files; |
---|
91 | |
---|
92 | /** |
---|
93 | * Sets validator options |
---|
94 | * |
---|
95 | * Min limits the file count, when used with max=null it is the maximum file count |
---|
96 | * It also accepts an array with the keys 'min' and 'max' |
---|
97 | * |
---|
98 | * If $options is a integer, it will be used as maximum file count |
---|
99 | * As Array is accepts the following keys: |
---|
100 | * 'min': Minimum filecount |
---|
101 | * 'max': Maximum filecount |
---|
102 | * |
---|
103 | * @param integer|array|Zend_Config $options Options for the adapter |
---|
104 | * @return void |
---|
105 | */ |
---|
106 | public function __construct($options) |
---|
107 | { |
---|
108 | if ($options instanceof Zend_Config) { |
---|
109 | $options = $options->toArray(); |
---|
110 | } elseif (is_string($options) || is_numeric($options)) { |
---|
111 | $options = array('max' => $options); |
---|
112 | } elseif (!is_array($options)) { |
---|
113 | require_once 'Zend/Validate/Exception.php'; |
---|
114 | throw new Zend_Validate_Exception ('Invalid options to validator provided'); |
---|
115 | } |
---|
116 | |
---|
117 | if (1 < func_num_args()) { |
---|
118 | $options['min'] = func_get_arg(0); |
---|
119 | $options['max'] = func_get_arg(1); |
---|
120 | } |
---|
121 | |
---|
122 | if (isset($options['min'])) { |
---|
123 | $this->setMin($options); |
---|
124 | } |
---|
125 | |
---|
126 | if (isset($options['max'])) { |
---|
127 | $this->setMax($options); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Returns the minimum file count |
---|
133 | * |
---|
134 | * @return integer |
---|
135 | */ |
---|
136 | public function getMin() |
---|
137 | { |
---|
138 | return $this->_min; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Sets the minimum file count |
---|
143 | * |
---|
144 | * @param integer|array $min The minimum file count |
---|
145 | * @return Zend_Validate_File_Count Provides a fluent interface |
---|
146 | * @throws Zend_Validate_Exception When min is greater than max |
---|
147 | */ |
---|
148 | public function setMin($min) |
---|
149 | { |
---|
150 | if (is_array($min) and isset($min['min'])) { |
---|
151 | $min = $min['min']; |
---|
152 | } |
---|
153 | |
---|
154 | if (!is_string($min) and !is_numeric($min)) { |
---|
155 | require_once 'Zend/Validate/Exception.php'; |
---|
156 | throw new Zend_Validate_Exception ('Invalid options to validator provided'); |
---|
157 | } |
---|
158 | |
---|
159 | $min = (integer) $min; |
---|
160 | if (($this->_max !== null) && ($min > $this->_max)) { |
---|
161 | require_once 'Zend/Validate/Exception.php'; |
---|
162 | throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >" |
---|
163 | . " {$this->_max}"); |
---|
164 | } |
---|
165 | |
---|
166 | $this->_min = $min; |
---|
167 | return $this; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Returns the maximum file count |
---|
172 | * |
---|
173 | * @return integer |
---|
174 | */ |
---|
175 | public function getMax() |
---|
176 | { |
---|
177 | return $this->_max; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Sets the maximum file count |
---|
182 | * |
---|
183 | * @param integer|array $max The maximum file count |
---|
184 | * @return Zend_Validate_StringLength Provides a fluent interface |
---|
185 | * @throws Zend_Validate_Exception When max is smaller than min |
---|
186 | */ |
---|
187 | public function setMax($max) |
---|
188 | { |
---|
189 | if (is_array($max) and isset($max['max'])) { |
---|
190 | $max = $max['max']; |
---|
191 | } |
---|
192 | |
---|
193 | if (!is_string($max) and !is_numeric($max)) { |
---|
194 | require_once 'Zend/Validate/Exception.php'; |
---|
195 | throw new Zend_Validate_Exception ('Invalid options to validator provided'); |
---|
196 | } |
---|
197 | |
---|
198 | $max = (integer) $max; |
---|
199 | if (($this->_min !== null) && ($max < $this->_min)) { |
---|
200 | require_once 'Zend/Validate/Exception.php'; |
---|
201 | throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but " |
---|
202 | . "$max < {$this->_min}"); |
---|
203 | } |
---|
204 | |
---|
205 | $this->_max = $max; |
---|
206 | return $this; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Adds a file for validation |
---|
211 | * |
---|
212 | * @param string|array $file |
---|
213 | */ |
---|
214 | public function addFile($file) |
---|
215 | { |
---|
216 | if (is_string($file)) { |
---|
217 | $file = array($file); |
---|
218 | } |
---|
219 | |
---|
220 | if (is_array($file)) { |
---|
221 | foreach ($file as $name) { |
---|
222 | if (!isset($this->_files[$name]) && !empty($name)) { |
---|
223 | $this->_files[$name] = $name; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | return $this; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * Defined by Zend_Validate_Interface |
---|
233 | * |
---|
234 | * Returns true if and only if the file count of all checked files is at least min and |
---|
235 | * not bigger than max (when max is not null). Attention: When checking with set min you |
---|
236 | * must give all files with the first call, otherwise you will get an false. |
---|
237 | * |
---|
238 | * @param string|array $value Filenames to check for count |
---|
239 | * @param array $file File data from Zend_File_Transfer |
---|
240 | * @return boolean |
---|
241 | */ |
---|
242 | public function isValid($value, $file = null) |
---|
243 | { |
---|
244 | if (($file !== null) && !array_key_exists('destination', $file)) { |
---|
245 | $file['destination'] = dirname($value); |
---|
246 | } |
---|
247 | |
---|
248 | if (($file !== null) && array_key_exists('tmp_name', $file)) { |
---|
249 | $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name']; |
---|
250 | } |
---|
251 | |
---|
252 | if (($file === null) || !empty($file['tmp_name'])) { |
---|
253 | $this->addFile($value); |
---|
254 | } |
---|
255 | |
---|
256 | $this->_count = count($this->_files); |
---|
257 | if (($this->_max !== null) && ($this->_count > $this->_max)) { |
---|
258 | return $this->_throw($file, self::TOO_MANY); |
---|
259 | } |
---|
260 | |
---|
261 | if (($this->_min !== null) && ($this->_count < $this->_min)) { |
---|
262 | return $this->_throw($file, self::TOO_FEW); |
---|
263 | } |
---|
264 | |
---|
265 | return true; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * Throws an error of the given type |
---|
270 | * |
---|
271 | * @param string $file |
---|
272 | * @param string $errorType |
---|
273 | * @return false |
---|
274 | */ |
---|
275 | protected function _throw($file, $errorType) |
---|
276 | { |
---|
277 | if ($file !== null) { |
---|
278 | $this->_value = $file['name']; |
---|
279 | } |
---|
280 | |
---|
281 | $this->_error($errorType); |
---|
282 | return false; |
---|
283 | } |
---|
284 | } |
---|