PageRenderTime 161ms CodeModel.GetById 127ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/nette-dev/Forms/Controls/TextBase.php

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