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