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