PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/File/Count.php

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