PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Forms/Controls/TextBase.php

https://github.com/DocX/nette
PHP | 259 lines | 94 code | 59 blank | 106 comment | 10 complexity | 4c1236481e3491bef06f55d6feee1154 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Forms
  17. */
  18. /*namespace Nette\Forms;*/
  19. require_once dirname(__FILE__) . '/../../Forms/Controls/FormControl.php';
  20. /**
  21. * Implements the basic functionality common to text input controls.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Forms
  26. *
  27. * @property string $emptyValue
  28. */
  29. abstract class TextBase extends FormControl
  30. {
  31. /** @var string */
  32. protected $emptyValue = '';
  33. /** @var array */
  34. protected $filters = array();
  35. /**
  36. * Sets control's value.
  37. * @param string
  38. * @return TextBase provides a fluent interface
  39. */
  40. public function setValue($value)
  41. {
  42. $this->value = is_scalar($value) ? (string) $value : '';
  43. return $this;
  44. }
  45. /**
  46. * Returns control's value.
  47. * @return string
  48. */
  49. public function getValue()
  50. {
  51. $value = $this->value;
  52. foreach ($this->filters as $filter) {
  53. $value = (string) call_user_func($filter, $value);
  54. }
  55. return $value === $this->translate($this->emptyValue) ? '' : $value;
  56. }
  57. /**
  58. * Sets the special value which is treated as empty string.
  59. * @param string
  60. * @return TextBase provides a fluent interface
  61. */
  62. public function setEmptyValue($value)
  63. {
  64. $this->emptyValue = $value;
  65. return $this;
  66. }
  67. /**
  68. * Returns the special value which is treated as empty string.
  69. * @return string
  70. */
  71. final public function getEmptyValue()
  72. {
  73. return $this->emptyValue;
  74. }
  75. /**
  76. * Appends input string filter callback.
  77. * @param callback
  78. * @return TextBase provides a fluent interface
  79. */
  80. public function addFilter($filter)
  81. {
  82. /**/fixCallback($filter);/**/
  83. if (!is_callable($filter)) {
  84. $able = is_callable($filter, TRUE, $textual);
  85. throw new /*\*/InvalidArgumentException("Filter '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
  86. }
  87. $this->filters[] = $filter;
  88. return $this;
  89. }
  90. public function notifyRule(Rule $rule)
  91. {
  92. if (is_string($rule->operation) && strcasecmp($rule->operation, ':float') === 0) {
  93. $this->addFilter(array(__CLASS__, 'filterFloat'));
  94. }
  95. parent::notifyRule($rule);
  96. }
  97. /**
  98. * Min-length validator: has control's value minimal length?
  99. * @param TextBase
  100. * @param int length
  101. * @return bool
  102. */
  103. public static function validateMinLength(TextBase $control, $length)
  104. {
  105. return iconv_strlen($control->getValue(), 'UTF-8') >= $length;
  106. }
  107. /**
  108. * Max-length validator: is control's value length in limit?
  109. * @param TextBase
  110. * @param int length
  111. * @return bool
  112. */
  113. public static function validateMaxLength(TextBase $control, $length)
  114. {
  115. return iconv_strlen($control->getValue(), 'UTF-8') <= $length;
  116. }
  117. /**
  118. * Length validator: is control's value length in range?
  119. * @param TextBase
  120. * @param array min and max length pair
  121. * @return bool
  122. */
  123. public static function validateLength(TextBase $control, $range)
  124. {
  125. if (!is_array($range)) {
  126. $range = array($range, $range);
  127. }
  128. $len = iconv_strlen($control->getValue(), 'UTF-8');
  129. return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
  130. }
  131. /**
  132. * Email validator: is control's value valid email address?
  133. * @param TextBase
  134. * @return bool
  135. */
  136. public static function validateEmail(TextBase $control)
  137. {
  138. $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
  139. $localPart = "(\"([ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(\\.$atom+)*)"; // quoted or unquoted
  140. $chars = "a-z0-9\x80-\xFF"; // superset of IDN
  141. $domain = "[$chars]([-$chars]{0,61}[$chars])"; // RFC 1034 one domain component
  142. return (bool) preg_match("(^$localPart@($domain?\\.)+[a-z]{2,10}\\z)i", $control->getValue()); // strict top-level domain
  143. }
  144. /**
  145. * URL validator: is control's value valid URL?
  146. * @param TextBase
  147. * @return bool
  148. */
  149. public static function validateUrl(TextBase $control)
  150. {
  151. return (bool) preg_match('/^.+\.[a-z]{2,6}(\\/.*)?$/i', $control->getValue());
  152. }
  153. /**
  154. * Regular expression validator: matches control's value regular expression?
  155. * @param TextBase
  156. * @param string
  157. * @return bool
  158. */
  159. public static function validateRegexp(TextBase $control, $regexp)
  160. {
  161. return (bool) preg_match($regexp, $control->getValue());
  162. }
  163. /**
  164. * Integer validator: is a control's value decimal number?
  165. * @param TextBase
  166. * @return bool
  167. */
  168. public static function validateInteger(TextBase $control)
  169. {
  170. return (bool) preg_match('/^-?[0-9]+$/', $control->getValue());
  171. }
  172. /**
  173. * Float validator: is a control's value float number?
  174. * @param TextBase
  175. * @return bool
  176. */
  177. public static function validateFloat(TextBase $control)
  178. {
  179. return (bool) preg_match('/^-?[0-9]*[.,]?[0-9]+$/', $control->getValue());
  180. }
  181. /**
  182. * Rangle validator: is a control's value number in specified range?
  183. * @param TextBase
  184. * @param array min and max value pair
  185. * @return bool
  186. */
  187. public static function validateRange(TextBase $control, $range)
  188. {
  189. return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
  190. }
  191. /**
  192. * Float string cleanup.
  193. * @param string
  194. * @return string
  195. */
  196. public static function filterFloat($s)
  197. {
  198. return str_replace(array(' ', ','), array('', '.'), $s);
  199. }
  200. }