~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Validate/File/Count.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 
19
 * @version   $Id: $
 
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-2008 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_MUCH = 'fileCountTooMuch';
 
41
    const TOO_LESS = 'fileCountTooLess';
 
42
    /**#@-*/
 
43
 
 
44
    /**
 
45
     * @var array Error message templates
 
46
     */
 
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"
 
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 $options Options for the adapter
 
104
     * @param  integer $max (Deprecated) Maximum value (implies $options is the minimum)
 
105
     * @return void
 
106
     */
 
107
    public function __construct($options)
 
108
    {
 
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');
 
116
        }
 
117
 
 
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);
 
122
        }
 
123
 
 
124
        if (isset($options['min'])) {
 
125
            $this->setMin($options);
 
126
        }
 
127
 
 
128
        if (isset($options['max'])) {
 
129
            $this->setMax($options);
 
130
        }
 
131
    }
 
132
 
 
133
    /**
 
134
     * Returns the minimum file count
 
135
     *
 
136
     * @return integer
 
137
     */
 
138
    public function getMin()
 
139
    {
 
140
        return $this->_min;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Sets the minimum file count
 
145
     *
 
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
 
149
     */
 
150
    public function setMin($min)
 
151
    {
 
152
        if (is_array($min) and isset($min['min'])) {
 
153
            $min = $min['min'];
 
154
        }
 
155
 
 
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');
 
159
        }
 
160
 
 
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 >"
 
165
                                            . " {$this->_max}");
 
166
        }
 
167
 
 
168
        $this->_min = $min;
 
169
        return $this;
 
170
    }
 
171
 
 
172
    /**
 
173
     * Returns the maximum file count
 
174
     *
 
175
     * @return integer
 
176
     */
 
177
    public function getMax()
 
178
    {
 
179
        return $this->_max;
 
180
    }
 
181
 
 
182
    /**
 
183
     * Sets the maximum file count
 
184
     *
 
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
 
188
     */
 
189
    public function setMax($max)
 
190
    {
 
191
        if (is_array($max) and isset($max['max'])) {
 
192
            $max = $max['max'];
 
193
        }
 
194
 
 
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');
 
198
        }
 
199
 
 
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}");
 
205
        }
 
206
 
 
207
        $this->_max = $max;
 
208
        return $this;
 
209
    }
 
210
 
 
211
    /**
 
212
     * Defined by Zend_Validate_Interface
 
213
     *
 
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.
 
217
     *
 
218
     * @param  string|array $value Filenames to check for count
 
219
     * @param  array        $file  File data from Zend_File_Transfer
 
220
     * @return boolean
 
221
     */
 
222
    public function isValid($value, $file = null)
 
223
    {
 
224
        if (is_string($value)) {
 
225
            $value = array($value);
 
226
        }
 
227
 
 
228
        foreach ($value as $file) {
 
229
            if (!isset($this->_files[$file])) {
 
230
                $this->_files[$file] = $file;
 
231
            }
 
232
        }
 
233
 
 
234
        $this->_count = count($this->_files);
 
235
        if (($this->_max !== null) && ($this->_count > $this->_max)) {
 
236
            return $this->_throw($file, self::TOO_MUCH);
 
237
        }
 
238
 
 
239
        if (($this->_min !== null) && ($this->_count < $this->_min)) {
 
240
            return $this->_throw($file, self::TOO_LESS);
 
241
        }
 
242
 
 
243
        return true;
 
244
    }
 
245
 
 
246
    /**
 
247
     * Throws an error of the given type
 
248
     *
 
249
     * @param  string $file
 
250
     * @param  string $errorType
 
251
     * @return false
 
252
     */
 
253
    protected function _throw($file, $errorType)
 
254
    {
 
255
        if ($file !== null) {
 
256
            $this->_value = $file['name'];
 
257
        }
 
258
 
 
259
        $this->_error($errorType);
 
260
        return false;
 
261
    }
 
262
}