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 https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Forms/Controls/FormControl.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Implements the basic functionality common to text input controls.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: abstract class TextBase extends FormControl
  35. 35: {
  36. 36:     /** @var string */
  37. 37:     protected $emptyValue = '';
  38. 38:  
  39. 39:     /** @var string */
  40. 40:     protected $tmpValue;
  41. 41:  
  42. 42:     /** @var array */
  43. 43:     protected $filters = array();
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /**
  48. 48:      * Sets control's value.
  49. 49:      * @param  string 
  50. 50:      * @return void 
  51. 51:      */
  52. 52:     public function setValue($value)
  53. 53:     {
  54. 54:         $value = (string) $value;
  55. 55:         foreach ($this->filters as $filter{
  56. 56:             $value = (string) call_user_func($filter$value);
  57. 57:         }
  58. 58:         $this->tmpValue = $this->value = $value === $this->translate($this->emptyValue'' $value;
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Loads HTTP data.
  65. 65:      * @param  array 
  66. 66:      * @return void 
  67. 67:      */
  68. 68:     public function loadHttpData($data)
  69. 69:     {
  70. 70:         $name $this->getName();
  71. 71:         $this->tmpValue = isset($data[$name]&& is_scalar($data[$name]$data[$nameNULL;
  72. 72:         $this->setValue($this->tmpValue);
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Sets the special value which is treated as empty string.
  79. 79:      * @param  string 
  80. 80:      * @return TextBase  provides a fluent interface
  81. 81:      */
  82. 82:     public function setEmptyValue($value)
  83. 83:     {
  84. 84:         $this->emptyValue = $value;
  85. 85:         return $this;
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 91:      * Returns the special value which is treated as empty string.
  92. 92:      * @return string 
  93. 93:      */
  94. 94:     final public function getEmptyValue()
  95. 95:     {
  96. 96:         return $this->emptyValue;
  97. 97:     }
  98. 98:  
  99. 99:  
  100. 100:  
  101. 101:     /**
  102. 102:      * Appends input string filter callback.
  103. 103:      * @param  callback 
  104. 104:      * @return TextBase  provides a fluent interface
  105. 105:      */
  106. 106:     public function addFilter($filter)
  107. 107:     {
  108. 108:         fixCallback($filter);
  109. 109:         if (!is_callable($filter)) {
  110. 110:             $able is_callable($filterTRUE$textual);
  111. 111:             throw new InvalidArgumentException("Filter '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  112. 112:         }
  113. 113:         $this->filters[$filter;
  114. 114:         return $this;
  115. 115:     }
  116. 116:  
  117. 117:  
  118. 118:  
  119. 119:     public function notifyRule(Rule $rule)
  120. 120:     {
  121. 121:         if (is_string($rule->operation&& strcasecmp($rule->operation':float'=== 0{
  122. 122:             $this->addFilter(array(__CLASS__'filterFloat'));
  123. 123:         }
  124. 124:  
  125. 125:         parent::notifyRule($rule);
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Min-length validator: has control's value minimal length?
  132. 132:      * @param  TextBase 
  133. 133:      * @param  int  length
  134. 134:      * @return bool 
  135. 135:      */
  136. 136:     public static function validateMinLength(TextBase $control$length)
  137. 137:     {
  138. 138:         // PHP bug #33268 iconv_strlen works since PHP 5.0.5
  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:         return preg_match('/^[^@\s]+@[^@\s]+\.[a-z]{2,10}$/i'$control->getValue());
  182. 182:     }
  183. 183:  
  184. 184:  
  185. 185:  
  186. 186:     /**
  187. 187:      * URL validator: is control's value valid URL?
  188. 188:      * @param  TextBase 
  189. 189:      * @return bool 
  190. 190:      */
  191. 191:     public static function validateUrl(TextBase $control)
  192. 192:     {
  193. 193:         return preg_match('/^.+\.[a-z]{2,6}(\\/.*)?$/i'$control->getValue());
  194. 194:     }
  195. 195:  
  196. 196:  
  197. 197:  
  198. 198:     /**
  199. 199:      * Regular expression validator: matches control's value regular expression?
  200. 200:      * @param  TextBase 
  201. 201:      * @param  string 
  202. 202:      * @return bool 
  203. 203:      */
  204. 204:     public static function validateRegexp(TextBase $control$regexp)
  205. 205:     {
  206. 206:         return preg_match($regexp$control->getValue());
  207. 207:     }
  208. 208:  
  209. 209:  
  210. 210:  
  211. 211:     /**
  212. 212:      * Integer validator: is a control's value decimal number?
  213. 213:      * @param  TextBase 
  214. 214:      * @return bool 
  215. 215:      */
  216. 216:     public static function validateInteger(TextBase $control)
  217. 217:     {
  218. 218:         return preg_match('/^-?[0-9]+$/'$control->getValue());
  219. 219:     }
  220. 220:  
  221. 221:  
  222. 222:  
  223. 223:     /**
  224. 224:      * Float validator: is a control's value float number?
  225. 225:      * @param  TextBase 
  226. 226:      * @return bool 
  227. 227:      */
  228. 228:     public static function validateFloat(TextBase $control)
  229. 229:     {
  230. 230:         return preg_match('/^-?[0-9]*[.,]?[0-9]+$/'$control->getValue());
  231. 231:     }
  232. 232:  
  233. 233:  
  234. 234:  
  235. 235:     /**
  236. 236:      * Rangle validator: is a control's value number in specified range?
  237. 237:      * @param  TextBase 
  238. 238:      * @param  array  min and max value pair
  239. 239:      * @return bool 
  240. 240:      */
  241. 241:     public static function validateRange(TextBase $control$range)
  242. 242:     {
  243. 243:         return ($range[0=== NULL || $control->getValue(>= $range[0]&& ($range[1=== NULL || $control->getValue(<= $range[1]);
  244. 244:     }
  245. 245:  
  246. 246:  
  247. 247:  
  248. 248:     /**
  249. 249:      * Float string cleanup.
  250. 250:      * @param  string 
  251. 251:      * @return string 
  252. 252:      */
  253. 253:     public static function filterFloat($s)
  254. 254:     {
  255. 255:         return str_replace(array(' '',')array('''.')$s);
  256. 256:     }
  257. 257: