PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validate/File/Count.php

https://bitbucket.org/nosen/jelly2
PHP | 284 lines | 127 code | 33 blank | 124 comment | 28 complexity | 42661cdcabb94d1a9caab2937f6c45f1 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @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. $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. */
  191. public function addFile($file)
  192. {
  193. if (is_string($file)) {
  194. $file = array($file);
  195. }
  196. if (is_array($file)) {
  197. foreach ($file as $name) {
  198. if (!isset($this->_files[$name]) && !empty($name)) {
  199. $this->_files[$name] = $name;
  200. }
  201. }
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Defined by Zend_Validate_Interface
  207. *
  208. * Returns true if and only if the file count of all checked files is at least min and
  209. * not bigger than max (when max is not null). Attention: When checking with set min you
  210. * must give all files with the first call, otherwise you will get an false.
  211. *
  212. * @param string|array $value Filenames to check for count
  213. * @param array $file File data from Zend_File_Transfer
  214. * @return boolean
  215. */
  216. public function isValid($value, $file = null)
  217. {
  218. if (($file !== null) && !array_key_exists('destination', $file)) {
  219. $file['destination'] = dirname($value);
  220. }
  221. if (($file !== null) && array_key_exists('tmp_name', $file)) {
  222. $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
  223. }
  224. if (($file === null) || !empty($file['tmp_name'])) {
  225. $this->addFile($value);
  226. }
  227. $this->_count = count($this->_files);
  228. if (($this->_max !== null) && ($this->_count > $this->_max)) {
  229. return $this->_throw($file, self::TOO_MANY);
  230. }
  231. if (($this->_min !== null) && ($this->_count < $this->_min)) {
  232. return $this->_throw($file, self::TOO_FEW);
  233. }
  234. return true;
  235. }
  236. /**
  237. * Throws an error of the given type
  238. *
  239. * @param string $file
  240. * @param string $errorType
  241. * @return false
  242. */
  243. protected function _throw($file, $errorType)
  244. {
  245. if ($file !== null) {
  246. $this->_value = $file['name'];
  247. }
  248. $this->_error($errorType);
  249. return false;
  250. }
  251. }