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.
16
* @package Zend_Validate
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
23
* @see Zend_Validate_Abstract
25
require_once 'Zend/Validate/Abstract.php';
28
* Validator for counting all given files
31
* @package Zend_Validate
32
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
33
* @license http://framework.zend.com/license/new-bsd New BSD License
35
class Zend_Validate_File_Count extends Zend_Validate_Abstract
38
* @const string Error constants
40
const TOO_MUCH = 'fileCountTooMuch';
41
const TOO_LESS = 'fileCountTooLess';
45
* @var array Error message templates
47
protected $_messageTemplates = array(
48
self::TOO_MUCH => "Too much files, maximum '%max%' are allowed but '%count%' are given",
49
self::TOO_LESS => "Too less files, minimum '%min%' are expected but '%count%' are given"
53
* @var array Error message template variables
55
protected $_messageVariables = array(
64
* If null, there is no minimum file count
73
* If null, there is no maximum file count
93
* Sets validator options
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'
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
103
* @param integer|array $options Options for the adapter
104
* @param integer $max (Deprecated) Maximum value (implies $options is the minimum)
107
public function __construct($options)
109
if ($options instanceof Zend_Config) {
110
$options = $options->toArray();
111
} elseif (is_string($options) || is_numeric($options)) {
112
$options = array('max' => $options);
113
} elseif (!is_array($options)) {
114
require_once 'Zend/Validate/Exception.php';
115
throw new Zend_Validate_Exception ('Invalid options to validator provided');
118
if (1 < func_num_args()) {
119
trigger_error('Multiple arguments are deprecated in favor of an array of named arguments', E_USER_NOTICE);
120
$options['min'] = func_get_arg(0);
121
$options['max'] = func_get_arg(1);
124
if (isset($options['min'])) {
125
$this->setMin($options);
128
if (isset($options['max'])) {
129
$this->setMax($options);
134
* Returns the minimum file count
138
public function getMin()
144
* Sets the minimum file count
146
* @param integer|array $min The minimum file count
147
* @return Zend_Validate_File_Size Provides a fluent interface
148
* @throws Zend_Validate_Exception When min is greater than max
150
public function setMin($min)
152
if (is_array($min) and isset($min['min'])) {
156
if (!is_string($min) and !is_numeric($min)) {
157
require_once 'Zend/Validate/Exception.php';
158
throw new Zend_Validate_Exception ('Invalid options to validator provided');
161
$min = (integer) $min;
162
if (($this->_max !== null) && ($min > $this->_max)) {
163
require_once 'Zend/Validate/Exception.php';
164
throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >"
173
* Returns the maximum file count
177
public function getMax()
183
* Sets the maximum file count
185
* @param integer|array $max The maximum file count
186
* @return Zend_Validate_StringLength Provides a fluent interface
187
* @throws Zend_Validate_Exception When max is smaller than min
189
public function setMax($max)
191
if (is_array($max) and isset($max['max'])) {
195
if (!is_string($max) and !is_numeric($max)) {
196
require_once 'Zend/Validate/Exception.php';
197
throw new Zend_Validate_Exception ('Invalid options to validator provided');
200
$max = (integer) $max;
201
if (($this->_min !== null) && ($max < $this->_min)) {
202
require_once 'Zend/Validate/Exception.php';
203
throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but "
204
. "$max < {$this->_min}");
212
* Defined by Zend_Validate_Interface
214
* Returns true if and only if the file count of all checked files is at least min and
215
* not bigger than max (when max is not null). Attention: When checking with set min you
216
* must give all files with the first call, otherwise you will get an false.
218
* @param string|array $value Filenames to check for count
219
* @param array $file File data from Zend_File_Transfer
222
public function isValid($value, $file = null)
224
if (is_string($value)) {
225
$value = array($value);
228
foreach ($value as $file) {
229
if (!isset($this->_files[$file])) {
230
$this->_files[$file] = $file;
234
$this->_count = count($this->_files);
235
if (($this->_max !== null) && ($this->_count > $this->_max)) {
236
return $this->_throw($file, self::TOO_MUCH);
239
if (($this->_min !== null) && ($this->_count < $this->_min)) {
240
return $this->_throw($file, self::TOO_LESS);
247
* Throws an error of the given type
249
* @param string $file
250
* @param string $errorType
253
protected function _throw($file, $errorType)
255
if ($file !== null) {
256
$this->_value = $file['name'];
259
$this->_error($errorType);