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: return parent::getControl()->data('nette-empty-value', $this->emptyValue === '' ? NULL : $this->translate($this->emptyValue));
100: }
101:
102:
103:
104: public function notifyRule(NRule $rule)
105: {
106: if (is_string($rule->operation) && !$rule->isNegative) {
107: if (strcasecmp($rule->operation, ':float') === 0) {
108: $this->addFilter(callback(__CLASS__, 'filterFloat'));
109:
110: } elseif (strcasecmp($rule->operation, ':length') === 0) {
111: $this->control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
112:
113: } elseif (strcasecmp($rule->operation, ':maxLength') === 0) {
114: $this->control->maxlength = $rule->arg;
115: }
116: }
117:
118: parent::notifyRule($rule);
119: }
120:
121:
122:
123: 124: 125: 126: 127: 128:
129: public static function validateMinLength(NTextBase $control, $length)
130: {
131: return NString::length($control->getValue()) >= $length;
132: }
133:
134:
135:
136: 137: 138: 139: 140: 141:
142: public static function validateMaxLength(NTextBase $control, $length)
143: {
144: return NString::length($control->getValue()) <= $length;
145: }
146:
147:
148:
149: 150: 151: 152: 153: 154:
155: public static function validateLength(NTextBase $control, $range)
156: {
157: if (!is_array($range)) {
158: $range = array($range, $range);
159: }
160: $len = NString::length($control->getValue());
161: return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
162: }
163:
164:
165:
166: 167: 168: 169: 170:
171: public static function validateEmail(NTextBase $control)
172: {
173: $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; 174: $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; 175: $chars = "a-z0-9\x80-\xFF"; 176: $domain = "[$chars](?:[-$chars]{0,61}[$chars])"; 177: return (bool) NString::match($control->getValue(), "(^$localPart@(?:$domain?\\.)+[-$chars]{2,19}\\z)i");
178: }
179:
180:
181:
182: 183: 184: 185: 186:
187: public static function validateUrl(NTextBase $control)
188: {
189: $chars = "a-z0-9\x80-\xFF";
190: return (bool) NString::match($control->getValue(), "#^(?:https?://|)(?:[$chars](?:[-$chars]{0,61}[$chars])?\\.)+[-$chars]{2,19}(/\S*)?$#i");
191: }
192:
193:
194:
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: public static function validateInteger(NTextBase $control)
214: {
215: return (bool) NString::match($control->getValue(), '/^-?[0-9]+$/');
216: }
217:
218:
219:
220: 221: 222: 223: 224:
225: public static function validateFloat(NTextBase $control)
226: {
227: return (bool) NString::match($control->getValue(), '/^-?[0-9]*[.,]?[0-9]+$/');
228: }
229:
230:
231:
232: 233: 234: 235: 236: 237:
238: public static function validateRange(NTextBase $control, $range)
239: {
240: return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
241: }
242:
243:
244:
245: 246: 247: 248: 249:
250: public static function filterFloat($s)
251: {
252: return str_replace(array(' ', ','), array('', '.'), $s);
253: }
254:
255: }
256: