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