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

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