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