1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10: 
 11: 
 12: namespace Nette\Forms;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18:  19:  20:  21:  22:  23:  24: 
 25: abstract class TextBase extends FormControl
 26: {
 27:     
 28:     protected $emptyValue = '';
 29: 
 30:     
 31:     protected $filters = array();
 32: 
 33: 
 34: 
 35:      36:  37:  38:  39: 
 40:     public function setValue($value)
 41:     {
 42:         $this->value = is_scalar($value) ? (string) $value : '';
 43:         return $this;
 44:     }
 45: 
 46: 
 47: 
 48:      49:  50:  51: 
 52:     public function getValue()
 53:     {
 54:         $value = $this->value;
 55:         foreach ($this->filters as $filter) {
 56:             $value = (string) $filter($value);
 57:         }
 58:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 59:     }
 60: 
 61: 
 62: 
 63:      64:  65:  66:  67: 
 68:     public function setEmptyValue($value)
 69:     {
 70:         $this->emptyValue = (string) $value;
 71:         return $this;
 72:     }
 73: 
 74: 
 75: 
 76:      77:  78:  79: 
 80:     final public function getEmptyValue()
 81:     {
 82:         return $this->emptyValue;
 83:     }
 84: 
 85: 
 86: 
 87:      88:  89:  90:  91: 
 92:     public function addFilter($filter)
 93:     {
 94:         $this->filters[] = callback($filter);
 95:         return $this;
 96:     }
 97: 
 98: 
 99: 
100:     public function notifyRule(Rule $rule)
101:     {
102:         if (is_string($rule->operation) && strcasecmp($rule->operation, ':float') === 0) {
103:             $this->addFilter(array(__CLASS__, 'filterFloat'));
104:         }
105: 
106:         parent::notifyRule($rule);
107:     }
108: 
109: 
110: 
111:     112: 113: 114: 115: 116: 
117:     public static function validateMinLength(TextBase $control, $length)
118:     {
119:         return iconv_strlen($control->getValue(), 'UTF-8') >= $length;
120:     }
121: 
122: 
123: 
124:     125: 126: 127: 128: 129: 
130:     public static function validateMaxLength(TextBase $control, $length)
131:     {
132:         return iconv_strlen($control->getValue(), 'UTF-8') <= $length;
133:     }
134: 
135: 
136: 
137:     138: 139: 140: 141: 142: 
143:     public static function validateLength(TextBase $control, $range)
144:     {
145:         if (!is_array($range)) {
146:             $range = array($range, $range);
147:         }
148:         $len = iconv_strlen($control->getValue(), 'UTF-8');
149:         return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
150:     }
151: 
152: 
153: 
154:     155: 156: 157: 158: 
159:     public static function validateEmail(TextBase $control)
160:     {
161:         $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; 
162:         $localPart = "(\"([ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(\\.$atom+)*)"; 
163:         $chars = "a-z0-9\x80-\xFF"; 
164:         $domain = "[$chars]([-$chars]{0,61}[$chars])"; 
165:         return (bool) preg_match("(^$localPart@($domain?\\.)+[-$chars]{2,19}\\z)i", $control->getValue());
166:     }
167: 
168: 
169: 
170:     171: 172: 173: 174: 
175:     public static function validateUrl(TextBase $control)
176:     {
177:         return (bool) preg_match('/^.+\.[a-z]{2,6}(\\/.*)?$/i', $control->getValue());
178:     }
179: 
180: 
181: 
182:     183: 184: 185: 186: 187: 
188:     public static function validateRegexp(TextBase $control, $regexp)
189:     {
190:         return (bool) preg_match($regexp, $control->getValue());
191:     }
192: 
193: 
194: 
195:     196: 197: 198: 199: 
200:     public static function validateInteger(TextBase $control)
201:     {
202:         return (bool) preg_match('/^-?[0-9]+$/', $control->getValue());
203:     }
204: 
205: 
206: 
207:     208: 209: 210: 211: 
212:     public static function validateFloat(TextBase $control)
213:     {
214:         return (bool) preg_match('/^-?[0-9]*[.,]?[0-9]+$/', $control->getValue());
215:     }
216: 
217: 
218: 
219:     220: 221: 222: 223: 224: 
225:     public static function validateRange(TextBase $control, $range)
226:     {
227:         return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
228:     }
229: 
230: 
231: 
232:     233: 234: 235: 236: 
237:     public static function filterFloat($s)
238:     {
239:         return str_replace(array(' ', ','), array('', '.'), $s);
240:     }
241: 
242: }
243: