Source for file TextBase.php

Documentation is available at TextBase.php

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