Count.php 7.8 KB

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