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 NTextBase extends NFormControl
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 notifyRule(NRule $rule)
99: {
100: if (is_string($rule->operation) && strcasecmp($rule->operation, ':float') === 0) {
101: $this->addFilter(array(__CLASS__, 'filterFloat'));
102: }
103:
104: parent::notifyRule($rule);
105: }
106:
107:
108:
109: 110: 111: 112: 113: 114:
115: public static function validateMinLength(NTextBase $control, $length)
116: {
117: return iconv_strlen($control->getValue(), 'UTF-8') >= $length;
118: }
119:
120:
121:
122: 123: 124: 125: 126: 127:
128: public static function validateMaxLength(NTextBase $control, $length)
129: {
130: return iconv_strlen($control->getValue(), 'UTF-8') <= $length;
131: }
132:
133:
134:
135: 136: 137: 138: 139: 140:
141: public static function validateLength(NTextBase $control, $range)
142: {
143: if (!is_array($range)) {
144: $range = array($range, $range);
145: }
146: $len = iconv_strlen($control->getValue(), 'UTF-8');
147: return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
148: }
149:
150:
151:
152: 153: 154: 155: 156:
157: public static function validateEmail(NTextBase $control)
158: {
159: $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]";
160: $localPart = "(\"([ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(\\.$atom+)*)";
161: $chars = "a-z0-9\x80-\xFF";
162: $domain = "[$chars]([-$chars]{0,61}[$chars])";
163: return (bool) preg_match("(^$localPart@($domain?\\.)+[-$chars]{2,19}\\z)i", $control->getValue());
164: }
165:
166:
167:
168: 169: 170: 171: 172:
173: public static function validateUrl(NTextBase $control)
174: {
175: return (bool) preg_match('/^.+\.[a-z]{2,6}(\\/.*)?$/i', $control->getValue());
176: }
177:
178:
179:
180: 181: 182: 183: 184: 185:
186: public static function validateRegexp(NTextBase $control, $regexp)
187: {
188: return (bool) preg_match($regexp, $control->getValue());
189: }
190:
191:
192:
193: 194: 195: 196: 197:
198: public static function validateInteger(NTextBase $control)
199: {
200: return (bool) preg_match('/^-?[0-9]+$/', $control->getValue());
201: }
202:
203:
204:
205: 206: 207: 208: 209:
210: public static function validateFloat(NTextBase $control)
211: {
212: return (bool) preg_match('/^-?[0-9]*[.,]?[0-9]+$/', $control->getValue());
213: }
214:
215:
216:
217: 218: 219: 220: 221: 222:
223: public static function validateRange(NTextBase $control, $range)
224: {
225: return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
226: }
227:
228:
229:
230: 231: 232: 233: 234:
235: public static function filterFloat($s)
236: {
237: return str_replace(array(' ', ','), array('', '.'), $s);
238: }
239:
240: }
241: