/package/app/app/vendor/ZendFramework/library/Zend/Validate/File/Count.php

https://github.com/richhl/kalturaCE · PHP · 275 lines · 119 code · 30 blank · 126 comment · 22 complexity · 8c0dbd19852dce92f965e673c145cf78 MD5 · raw file

  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Count.php 18148 2009-09-16 19:27:43Z thomas $
  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-2009 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_MUCH = 'fileCountTooMuch';
  39. const TOO_LESS = 'fileCountTooLess';
  40. /**#@-*/
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::TOO_MUCH => "Too much files, maximum '%max%' are allowed but '%count%' are given",
  46. self::TOO_LESS => "Too less 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. * @return void
  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. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  109. // trigger_error('Multiple arguments are deprecated in favor of an array of named arguments', E_USER_NOTICE);
  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_Validate_File_Count Provides a fluent interface
  134. * @throws Zend_Validate_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. require_once 'Zend/Validate/Exception.php';
  143. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  144. }
  145. $min = (integer) $min;
  146. if (($this->_max !== null) && ($min > $this->_max)) {
  147. require_once 'Zend/Validate/Exception.php';
  148. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >"
  149. . " {$this->_max}");
  150. }
  151. $this->_min = $min;
  152. return $this;
  153. }
  154. /**
  155. * Returns the maximum file count
  156. *
  157. * @return integer
  158. */
  159. public function getMax()
  160. {
  161. return $this->_max;
  162. }
  163. /**
  164. * Sets the maximum file count
  165. *
  166. * @param integer|array $max The maximum file count
  167. * @return Zend_Validate_StringLength Provides a fluent interface
  168. * @throws Zend_Validate_Exception When max is smaller than min
  169. */
  170. public function setMax($max)
  171. {
  172. if (is_array($max) and isset($max['max'])) {
  173. $max = $max['max'];
  174. }
  175. if (!is_string($max) and !is_numeric($max)) {
  176. require_once 'Zend/Validate/Exception.php';
  177. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  178. }
  179. $max = (integer) $max;
  180. if (($this->_min !== null) && ($max < $this->_min)) {
  181. require_once 'Zend/Validate/Exception.php';
  182. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but "
  183. . "$max < {$this->_min}");
  184. }
  185. $this->_max = $max;
  186. return $this;
  187. }
  188. /**
  189. * Adds a file for validation
  190. *
  191. * @param string|array $file
  192. */
  193. public function addFile($file)
  194. {
  195. if (is_string($file)) {
  196. $file = array($file);
  197. }
  198. if (is_array($file)) {
  199. foreach ($file as $name) {
  200. if (!isset($this->_files[$name]) && !empty($name)) {
  201. $this->_files[$name] = $name;
  202. }
  203. }
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Defined by Zend_Validate_Interface
  209. *
  210. * Returns true if and only if the file count of all checked files is at least min and
  211. * not bigger than max (when max is not null). Attention: When checking with set min you
  212. * must give all files with the first call, otherwise you will get an false.
  213. *
  214. * @param string|array $value Filenames to check for count
  215. * @param array $file File data from Zend_File_Transfer
  216. * @return boolean
  217. */
  218. public function isValid($value, $file = null)
  219. {
  220. $this->addFile($value);
  221. $this->_count = count($this->_files);
  222. if (($this->_max !== null) && ($this->_count > $this->_max)) {
  223. return $this->_throw($file, self::TOO_MUCH);
  224. }
  225. if (($this->_min !== null) && ($this->_count < $this->_min)) {
  226. return $this->_throw($file, self::TOO_LESS);
  227. }
  228. return true;
  229. }
  230. /**
  231. * Throws an error of the given type
  232. *
  233. * @param string $file
  234. * @param string $errorType
  235. * @return false
  236. */
  237. protected function _throw($file, $errorType)
  238. {
  239. if ($file !== null) {
  240. $this->_value = $file['name'];
  241. }
  242. $this->_error($errorType);
  243. return false;
  244. }
  245. }