Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / File / Count.php
blob4cb5202636d8b4f7362c5224c101bda3940a7c93
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Validator\File;
12 use Zend\Validator\AbstractValidator;
13 use Zend\Validator\Exception;
15 /**
16 * Validator for counting all given files
19 class Count extends AbstractValidator
21 /**#@+
22 * @const string Error constants
24 const TOO_MANY = 'fileCountTooMany';
25 const TOO_FEW = 'fileCountTooFew';
26 /**#@-*/
28 /**
29 * @var array Error message templates
31 protected $messageTemplates = array(
32 self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given",
33 self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given",
36 /**
37 * @var array Error message template variables
39 protected $messageVariables = array(
40 'min' => array('options' => 'min'),
41 'max' => array('options' => 'max'),
42 'count' => 'count'
45 /**
46 * Actual filecount
48 * @var int
50 protected $count;
52 /**
53 * Internal file array
54 * @var array
56 protected $files;
58 /**
59 * Options for this validator
61 * @var array
63 protected $options = array(
64 'min' => null, // Minimum file count, if null there is no minimum file count
65 'max' => null, // Maximum file count, if null there is no maximum file count
68 /**
69 * Sets validator options
71 * Min limits the file count, when used with max=null it is the maximum file count
72 * It also accepts an array with the keys 'min' and 'max'
74 * If $options is a integer, it will be used as maximum file count
75 * As Array is accepts the following keys:
76 * 'min': Minimum filecount
77 * 'max': Maximum filecount
79 * @param int|array|\Traversable $options Options for the adapter
81 public function __construct($options = null)
83 if (is_string($options) || is_numeric($options)) {
84 $options = array('max' => $options);
87 if (1 < func_num_args()) {
88 $options['min'] = func_get_arg(0);
89 $options['max'] = func_get_arg(1);
92 parent::__construct($options);
95 /**
96 * Returns the minimum file count
98 * @return int
100 public function getMin()
102 return $this->options['min'];
106 * Sets the minimum file count
108 * @param int|array $min The minimum file count
109 * @return Count Provides a fluent interface
110 * @throws Exception\InvalidArgumentException When min is greater than max
112 public function setMin($min)
114 if (is_array($min) and isset($min['min'])) {
115 $min = $min['min'];
118 if (!is_string($min) and !is_numeric($min)) {
119 throw new Exception\InvalidArgumentException('Invalid options to validator provided');
122 $min = (int) $min;
123 if (($this->getMax() !== null) && ($min > $this->getMax())) {
124 throw new Exception\InvalidArgumentException("The minimum must be less than or equal to the maximum file count, but $min >"
125 . " {$this->getMax()}");
128 $this->options['min'] = $min;
129 return $this;
133 * Returns the maximum file count
135 * @return int
137 public function getMax()
139 return $this->options['max'];
143 * Sets the maximum file count
145 * @param int|array $max The maximum file count
146 * @return Count Provides a fluent interface
147 * @throws Exception\InvalidArgumentException When max is smaller than min
149 public function setMax($max)
151 if (is_array($max) and isset($max['max'])) {
152 $max = $max['max'];
155 if (!is_string($max) and !is_numeric($max)) {
156 throw new Exception\InvalidArgumentException('Invalid options to validator provided');
159 $max = (int) $max;
160 if (($this->getMin() !== null) && ($max < $this->getMin())) {
161 throw new Exception\InvalidArgumentException("The maximum must be greater than or equal to the minimum file count, but "
162 . "$max < {$this->getMin()}");
165 $this->options['max'] = $max;
166 return $this;
170 * Adds a file for validation
172 * @param string|array $file
173 * @return Count
175 public function addFile($file)
177 if (is_string($file)) {
178 $file = array($file);
181 if (is_array($file)) {
182 foreach ($file as $name) {
183 if (!isset($this->files[$name]) && !empty($name)) {
184 $this->files[$name] = $name;
189 return $this;
193 * Returns true if and only if the file count of all checked files is at least min and
194 * not bigger than max (when max is not null). Attention: When checking with set min you
195 * must give all files with the first call, otherwise you will get an false.
197 * @param string|array $value Filenames to check for count
198 * @param array $file File data from \Zend\File\Transfer\Transfer
199 * @return bool
201 public function isValid($value, $file = null)
203 if (($file !== null) && !array_key_exists('destination', $file)) {
204 $file['destination'] = dirname($value);
207 if (($file !== null) && array_key_exists('tmp_name', $file)) {
208 $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
211 if (($file === null) || !empty($file['tmp_name'])) {
212 $this->addFile($value);
215 $this->count = count($this->files);
216 if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
217 return $this->throwError($file, self::TOO_MANY);
220 if (($this->getMin() !== null) && ($this->count < $this->getMin())) {
221 return $this->throwError($file, self::TOO_FEW);
224 return true;
228 * Throws an error of the given type
230 * @param string $file
231 * @param string $errorType
232 * @return false
234 protected function throwError($file, $errorType)
236 if ($file !== null) {
237 if (is_array($file)) {
238 if (array_key_exists('name', $file)) {
239 $this->value = $file['name'];
241 } elseif (is_string($file)) {
242 $this->value = $file;
246 $this->error($errorType);
247 return false;