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

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

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