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