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