PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nette/forms/src/Forms/Validator.php

https://gitlab.com/paveltizek/shop
PHP | 338 lines | 188 code | 57 blank | 93 comment | 18 complexity | c4c9db80d68d64f362206fcc5f8c8883 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. namespace Nette\Forms;
  7. use Nette;
  8. use Nette\Utils\Strings;
  9. use Nette\Utils\Validators;
  10. /**
  11. * Common validators.
  12. */
  13. class Validator extends Nette\Object
  14. {
  15. /** @var array */
  16. public static $messages = array(
  17. Form::PROTECTION => 'Your session has expired. Please return to the home page and try again.',
  18. Form::EQUAL => 'Please enter %s.',
  19. Form::NOT_EQUAL => 'This value should not be %s.',
  20. Form::FILLED => 'This field is required.',
  21. Form::BLANK => 'This field should be blank.',
  22. Form::MIN_LENGTH => 'Please enter at least %d characters.',
  23. Form::MAX_LENGTH => 'Please enter no more than %d characters.',
  24. Form::LENGTH => 'Please enter a value between %d and %d characters long.',
  25. Form::EMAIL => 'Please enter a valid email address.',
  26. Form::URL => 'Please enter a valid URL.',
  27. Form::INTEGER => 'Please enter a valid integer.',
  28. Form::FLOAT => 'Please enter a valid number.',
  29. Form::MIN => 'Please enter a value greater than or equal to %d.',
  30. Form::MAX => 'Please enter a value less than or equal to %d.',
  31. Form::RANGE => 'Please enter a value between %d and %d.',
  32. Form::MAX_FILE_SIZE => 'The size of the uploaded file can be up to %d bytes.',
  33. Form::MAX_POST_SIZE => 'The uploaded data exceeds the limit of %d bytes.',
  34. Form::MIME_TYPE => 'The uploaded file is not in the expected format.',
  35. Form::IMAGE => 'The uploaded file must be image in format JPEG, GIF or PNG.',
  36. Controls\SelectBox::VALID => 'Please select a valid option.',
  37. );
  38. /** @internal */
  39. public static function formatMessage(Rule $rule, $withValue = TRUE)
  40. {
  41. $message = $rule->message;
  42. if ($message instanceof Nette\Utils\Html) {
  43. return $message;
  44. } elseif ($message === NULL && is_string($rule->validator) && isset(static::$messages[$rule->validator])) {
  45. $message = static::$messages[$rule->validator];
  46. } elseif ($message == NULL) { // intentionally ==
  47. trigger_error("Missing validation message for control '{$rule->control->getName()}'.", E_USER_WARNING);
  48. }
  49. if ($translator = $rule->control->getForm()->getTranslator()) {
  50. $message = $translator->translate($message, is_int($rule->arg) ? $rule->arg : NULL);
  51. }
  52. $message = preg_replace_callback('#%(name|label|value|\d+\$[ds]|[ds])#', function ($m) use ($rule, $withValue) {
  53. static $i = -1;
  54. switch ($m[1]) {
  55. case 'name': return $rule->control->getName();
  56. case 'label': return $rule->control->translate($rule->control->caption);
  57. case 'value': return $withValue ? $rule->control->getValue() : $m[0];
  58. default:
  59. $args = is_array($rule->arg) ? $rule->arg : array($rule->arg);
  60. $i = (int) $m[1] ? $m[1] - 1 : $i + 1;
  61. return isset($args[$i]) ? ($args[$i] instanceof IControl ? ($withValue ? $args[$i]->getValue() : "%$i") : $args[$i]) : '';
  62. }
  63. }, $message);
  64. return $message;
  65. }
  66. /********************* default validators ****************d*g**/
  67. /**
  68. * Is control's value equal with second parameter?
  69. * @return bool
  70. */
  71. public static function validateEqual(IControl $control, $arg)
  72. {
  73. $value = $control->getValue();
  74. foreach ((is_array($value) ? $value : array($value)) as $val) {
  75. foreach ((is_array($arg) ? $arg : array($arg)) as $item) {
  76. if ((string) $val === (string) $item) {
  77. continue 2;
  78. }
  79. }
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. /**
  85. * Is control's value not equal with second parameter?
  86. * @return bool
  87. */
  88. public static function validateNotEqual(IControl $control, $arg)
  89. {
  90. return !static::validateEqual($control, $arg);
  91. }
  92. /**
  93. * Is control filled?
  94. * @return bool
  95. */
  96. public static function validateFilled(IControl $control)
  97. {
  98. return $control->isFilled();
  99. }
  100. /**
  101. * Is control not filled?
  102. * @return bool
  103. */
  104. public static function validateBlank(IControl $control)
  105. {
  106. return !$control->isFilled();
  107. }
  108. /**
  109. * Is control valid?
  110. * @return bool
  111. */
  112. public static function validateValid(IControl $control)
  113. {
  114. return $control->getRules()->validate();
  115. }
  116. /**
  117. * Is a control's value number in specified range?
  118. * @return bool
  119. */
  120. public static function validateRange(IControl $control, $range)
  121. {
  122. return Validators::isInRange($control->getValue(), $range);
  123. }
  124. /**
  125. * Is a control's value number greater than or equal to the specified minimum?
  126. * @return bool
  127. */
  128. public static function validateMin(IControl $control, $minimum)
  129. {
  130. return Validators::isInRange($control->getValue(), array($minimum, NULL));
  131. }
  132. /**
  133. * Is a control's value number less than or equal to the specified maximum?
  134. * @return bool
  135. */
  136. public static function validateMax(IControl $control, $maximum)
  137. {
  138. return Validators::isInRange($control->getValue(), array(NULL, $maximum));
  139. }
  140. /**
  141. * Count/length validator. Range is array, min and max length pair.
  142. * @return bool
  143. */
  144. public static function validateLength(IControl $control, $range)
  145. {
  146. if (!is_array($range)) {
  147. $range = array($range, $range);
  148. }
  149. $value = $control->getValue();
  150. return Validators::isInRange(is_array($value) ? count($value) : Strings::length($value), $range);
  151. }
  152. /**
  153. * Has control's value minimal count/length?
  154. * @return bool
  155. */
  156. public static function validateMinLength(IControl $control, $length)
  157. {
  158. return static::validateLength($control, array($length, NULL));
  159. }
  160. /**
  161. * Is control's value count/length in limit?
  162. * @return bool
  163. */
  164. public static function validateMaxLength(IControl $control, $length)
  165. {
  166. return static::validateLength($control, array(NULL, $length));
  167. }
  168. /**
  169. * Has been button pressed?
  170. * @return bool
  171. */
  172. public static function validateSubmitted(Controls\SubmitButton $control)
  173. {
  174. return $control->isSubmittedBy();
  175. }
  176. /**
  177. * Is control's value valid email address?
  178. * @return bool
  179. */
  180. public static function validateEmail(IControl $control)
  181. {
  182. return Validators::isEmail($control->getValue());
  183. }
  184. /**
  185. * Is control's value valid URL?
  186. * @return bool
  187. */
  188. public static function validateUrl(IControl $control)
  189. {
  190. if (Validators::isUrl($value = $control->getValue())) {
  191. return TRUE;
  192. } elseif (Validators::isUrl($value = "http://$value")) {
  193. $control->setValue($value);
  194. return TRUE;
  195. }
  196. return FALSE;
  197. }
  198. /**
  199. * Matches control's value regular expression?
  200. * @return bool
  201. */
  202. public static function validatePattern(IControl $control, $pattern)
  203. {
  204. return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u");
  205. }
  206. /**
  207. * Is a control's value decimal number?
  208. * @return bool
  209. */
  210. public static function validateInteger(IControl $control)
  211. {
  212. if (Validators::isNumericInt($value = $control->getValue())) {
  213. if (!is_float($tmp = $value * 1)) { // bigint leave as string
  214. $control->setValue($tmp);
  215. }
  216. return TRUE;
  217. }
  218. return FALSE;
  219. }
  220. /**
  221. * Is a control's value float number?
  222. * @return bool
  223. */
  224. public static function validateFloat(IControl $control)
  225. {
  226. $value = str_replace(array(' ', ','), array('', '.'), $control->getValue());
  227. if (Validators::isNumeric($value)) {
  228. $control->setValue((float) $value);
  229. return TRUE;
  230. }
  231. return FALSE;
  232. }
  233. /**
  234. * Is file size in limit?
  235. * @return bool
  236. */
  237. public static function validateFileSize(Controls\UploadControl $control, $limit)
  238. {
  239. foreach (static::toArray($control->getValue()) as $file) {
  240. if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
  241. return FALSE;
  242. }
  243. }
  244. return TRUE;
  245. }
  246. /**
  247. * Has file specified mime type?
  248. * @return bool
  249. */
  250. public static function validateMimeType(Controls\UploadControl $control, $mimeType)
  251. {
  252. $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
  253. foreach (static::toArray($control->getValue()) as $file) {
  254. $type = strtolower($file->getContentType());
  255. if (!in_array($type, $mimeTypes, TRUE) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
  256. return FALSE;
  257. }
  258. }
  259. return TRUE;
  260. }
  261. /**
  262. * Is file image?
  263. * @return bool
  264. */
  265. public static function validateImage(Controls\UploadControl $control)
  266. {
  267. foreach (static::toArray($control->getValue()) as $file) {
  268. if (!$file->isImage()) {
  269. return FALSE;
  270. }
  271. }
  272. return TRUE;
  273. }
  274. /**
  275. * @return array
  276. */
  277. private static function toArray($value)
  278. {
  279. return $value instanceof Nette\Http\FileUpload ? array($value) : (array) $value;
  280. }
  281. }