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

/vendor/zendframework/zend-validator/src/File/Count.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 251 lines | 125 code | 32 blank | 94 comment | 28 complexity | 2459c02f7f309d57da000118369bcaaa MD5 | raw file
  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-2015 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 int
  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 an 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 int|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 int
  87. */
  88. public function getMin()
  89. {
  90. return $this->options['min'];
  91. }
  92. /**
  93. * Sets the minimum file count
  94. *
  95. * @param int|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 = (int) $min;
  108. if (($this->getMax() !== null) && ($min > $this->getMax())) {
  109. throw new Exception\InvalidArgumentException(
  110. "The minimum must be less than or equal to the maximum file count, but {$min} > {$this->getMax()}"
  111. );
  112. }
  113. $this->options['min'] = $min;
  114. return $this;
  115. }
  116. /**
  117. * Returns the maximum file count
  118. *
  119. * @return int
  120. */
  121. public function getMax()
  122. {
  123. return $this->options['max'];
  124. }
  125. /**
  126. * Sets the maximum file count
  127. *
  128. * @param int|array $max The maximum file count
  129. * @return Count Provides a fluent interface
  130. * @throws Exception\InvalidArgumentException When max is smaller than min
  131. */
  132. public function setMax($max)
  133. {
  134. if (is_array($max) and isset($max['max'])) {
  135. $max = $max['max'];
  136. }
  137. if (!is_string($max) and !is_numeric($max)) {
  138. throw new Exception\InvalidArgumentException('Invalid options to validator provided');
  139. }
  140. $max = (int) $max;
  141. if (($this->getMin() !== null) && ($max < $this->getMin())) {
  142. throw new Exception\InvalidArgumentException(
  143. "The maximum must be greater than or equal to the minimum file count, but {$max} < {$this->getMin()}"
  144. );
  145. }
  146. $this->options['max'] = $max;
  147. return $this;
  148. }
  149. /**
  150. * Adds a file for validation
  151. *
  152. * @param string|array $file
  153. * @return Count
  154. */
  155. public function addFile($file)
  156. {
  157. if (is_string($file)) {
  158. $file = array($file);
  159. }
  160. if (is_array($file)) {
  161. foreach ($file as $name) {
  162. if (!isset($this->files[$name]) && !empty($name)) {
  163. $this->files[$name] = $name;
  164. }
  165. }
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Returns true if and only if the file count of all checked files is at least min and
  171. * not bigger than max (when max is not null). Attention: When checking with set min you
  172. * must give all files with the first call, otherwise you will get a false.
  173. *
  174. * @param string|array $value Filenames to check for count
  175. * @param array $file File data from \Zend\File\Transfer\Transfer
  176. * @return bool
  177. */
  178. public function isValid($value, $file = null)
  179. {
  180. if (($file !== null) && !array_key_exists('destination', $file)) {
  181. $file['destination'] = dirname($value);
  182. }
  183. if (($file !== null) && array_key_exists('tmp_name', $file)) {
  184. $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
  185. }
  186. if (($file === null) || !empty($file['tmp_name'])) {
  187. $this->addFile($value);
  188. }
  189. $this->count = count($this->files);
  190. if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
  191. return $this->throwError($file, self::TOO_MANY);
  192. }
  193. if (($this->getMin() !== null) && ($this->count < $this->getMin())) {
  194. return $this->throwError($file, self::TOO_FEW);
  195. }
  196. return true;
  197. }
  198. /**
  199. * Throws an error of the given type
  200. *
  201. * @param string $file
  202. * @param string $errorType
  203. * @return false
  204. */
  205. protected function throwError($file, $errorType)
  206. {
  207. if ($file !== null) {
  208. if (is_array($file)) {
  209. if (array_key_exists('name', $file)) {
  210. $this->value = $file['name'];
  211. }
  212. } elseif (is_string($file)) {
  213. $this->value = $file;
  214. }
  215. }
  216. $this->error($errorType);
  217. return false;
  218. }
  219. }