PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/raulgracia/zendframework2
PHP | 249 lines | 123 code | 32 blank | 94 comment | 28 complexity | 282a44ec40431efc6a7cc5bc894103c7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator\File;
  10. use Zend\Validator\AbstractValidator;
  11. use Zend\Validator\Exception;
  12. /**
  13. * Validator for counting all given files
  14. *
  15. */
  16. class Count extends AbstractValidator
  17. {
  18. /**#@+
  19. * @const string Error constants
  20. */
  21. const TOO_MANY = 'fileCountTooMany';
  22. const TOO_FEW = 'fileCountTooFew';
  23. /**#@-*/
  24. /**
  25. * @var array Error message templates
  26. */
  27. protected $messageTemplates = array(
  28. self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given",
  29. self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given",
  30. );
  31. /**
  32. * @var array Error message template variables
  33. */
  34. protected $messageVariables = array(
  35. 'min' => array('options' => 'min'),
  36. 'max' => array('options' => 'max'),
  37. 'count' => 'count'
  38. );
  39. /**
  40. * Actual filecount
  41. *
  42. * @var integer
  43. */
  44. protected $count;
  45. /**
  46. * Internal file array
  47. * @var array
  48. */
  49. protected $files;
  50. /**
  51. * Options for this validator
  52. *
  53. * @var array
  54. */
  55. protected $options = array(
  56. 'min' => null, // Minimum file count, if null there is no minimum file count
  57. 'max' => null, // Maximum file count, if null there is no maximum file count
  58. );
  59. /**
  60. * Sets validator options
  61. *
  62. * Min limits the file count, when used with max=null it is the maximum file count
  63. * It also accepts an array with the keys 'min' and 'max'
  64. *
  65. * If $options is a integer, it will be used as maximum file count
  66. * As Array is accepts the following keys:
  67. * 'min': Minimum filecount
  68. * 'max': Maximum filecount
  69. *
  70. * @param integer|array|\Traversable $options Options for the adapter
  71. */
  72. public function __construct($options = null)
  73. {
  74. if (is_string($options) || is_numeric($options)) {
  75. $options = array('max' => $options);
  76. }
  77. if (1 < func_num_args()) {
  78. $options['min'] = func_get_arg(0);
  79. $options['max'] = func_get_arg(1);
  80. }
  81. parent::__construct($options);
  82. }
  83. /**
  84. * Returns the minimum file count
  85. *
  86. * @return integer
  87. */
  88. public function getMin()
  89. {
  90. return $this->options['min'];
  91. }
  92. /**
  93. * Sets the minimum file count
  94. *
  95. * @param integer|array $min The minimum file count
  96. * @return Count Provides a fluent interface
  97. * @throws Exception\InvalidArgumentException When min is greater than max
  98. */
  99. public function setMin($min)
  100. {
  101. if (is_array($min) and isset($min['min'])) {
  102. $min = $min['min'];
  103. }
  104. if (!is_string($min) and !is_numeric($min)) {
  105. throw new Exception\InvalidArgumentException('Invalid options to validator provided');
  106. }
  107. $min = (integer) $min;
  108. if (($this->getMax() !== null) && ($min > $this->getMax())) {
  109. throw new Exception\InvalidArgumentException("The minimum must be less than or equal to the maximum file count, but $min >"
  110. . " {$this->getMax()}");
  111. }
  112. $this->options['min'] = $min;
  113. return $this;
  114. }
  115. /**
  116. * Returns the maximum file count
  117. *
  118. * @return integer
  119. */
  120. public function getMax()
  121. {
  122. return $this->options['max'];
  123. }
  124. /**
  125. * Sets the maximum file count
  126. *
  127. * @param integer|array $max The maximum file count
  128. * @return Count Provides a fluent interface
  129. * @throws Exception\InvalidArgumentException When max is smaller than min
  130. */
  131. public function setMax($max)
  132. {
  133. if (is_array($max) and isset($max['max'])) {
  134. $max = $max['max'];
  135. }
  136. if (!is_string($max) and !is_numeric($max)) {
  137. throw new Exception\InvalidArgumentException('Invalid options to validator provided');
  138. }
  139. $max = (integer) $max;
  140. if (($this->getMin() !== null) && ($max < $this->getMin())) {
  141. throw new Exception\InvalidArgumentException("The maximum must be greater than or equal to the minimum file count, but "
  142. . "$max < {$this->getMin()}");
  143. }
  144. $this->options['max'] = $max;
  145. return $this;
  146. }
  147. /**
  148. * Adds a file for validation
  149. *
  150. * @param string|array $file
  151. * @return Count
  152. */
  153. public function addFile($file)
  154. {
  155. if (is_string($file)) {
  156. $file = array($file);
  157. }
  158. if (is_array($file)) {
  159. foreach ($file as $name) {
  160. if (!isset($this->files[$name]) && !empty($name)) {
  161. $this->files[$name] = $name;
  162. }
  163. }
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Returns true if and only if the file count of all checked files is at least min and
  169. * not bigger than max (when max is not null). Attention: When checking with set min you
  170. * must give all files with the first call, otherwise you will get an false.
  171. *
  172. * @param string|array $value Filenames to check for count
  173. * @param array $file File data from \Zend\File\Transfer\Transfer
  174. * @return bool
  175. */
  176. public function isValid($value, $file = null)
  177. {
  178. if (($file !== null) && !array_key_exists('destination', $file)) {
  179. $file['destination'] = dirname($value);
  180. }
  181. if (($file !== null) && array_key_exists('tmp_name', $file)) {
  182. $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
  183. }
  184. if (($file === null) || !empty($file['tmp_name'])) {
  185. $this->addFile($value);
  186. }
  187. $this->count = count($this->files);
  188. if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
  189. return $this->throwError($file, self::TOO_MANY);
  190. }
  191. if (($this->getMin() !== null) && ($this->count < $this->getMin())) {
  192. return $this->throwError($file, self::TOO_FEW);
  193. }
  194. return true;
  195. }
  196. /**
  197. * Throws an error of the given type
  198. *
  199. * @param string $file
  200. * @param string $errorType
  201. * @return false
  202. */
  203. protected function throwError($file, $errorType)
  204. {
  205. if ($file !== null) {
  206. if (is_array($file)) {
  207. if (array_key_exists('name', $file)) {
  208. $this->value = $file['name'];
  209. }
  210. } elseif (is_string($file)) {
  211. $this->value = $file;
  212. }
  213. }
  214. $this->error($errorType);
  215. return false;
  216. }
  217. }