1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: abstract class NTextBase extends NFormControl
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 NCallback($filter);
94: return $this;
95: }
96:
97:
98: 99: 100: 101:
102: public function sanitize($value)
103: {
104: if ($this->control->maxlength) {
105: $value = NStrings::substring($value, 0, $this->control->maxlength);
106: }
107: if (strcasecmp($this->control->getName(), 'input') === 0) {
108: $value = NStrings::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 === NRule::VALIDATOR && !$rule->isNegative
119: && ($rule->operation === NForm::LENGTH || $rule->operation === NForm::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 === NForm::FLOAT) {
134: $this->addFilter(array(__CLASS__, 'filterFloat'));
135:
136: } elseif ($operation === NForm::LENGTH || $operation === NForm::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:
154: public static function validateMinLength(NTextBase $control, $length)
155: {
156: return NStrings::length($control->getValue()) >= $length;
157: }
158:
159:
160: 161: 162: 163: 164: 165: 166:
167: public static function validateMaxLength(NTextBase $control, $length)
168: {
169: return NStrings::length($control->getValue()) <= $length;
170: }
171:
172:
173: 174: 175: 176: 177: 178:
179: public static function validateLength(NTextBase $control, $range)
180: {
181: if (!is_array($range)) {
182: $range = array($range, $range);
183: }
184: return NValidators::isInRange(NStrings::length($control->getValue()), $range);
185: }
186:
187:
188: 189: 190: 191: 192:
193: public static function validateEmail(NTextBase $control)
194: {
195: return NValidators::isEmail($control->getValue());
196: }
197:
198:
199: 200: 201: 202: 203:
204: public static function validateUrl(NTextBase $control)
205: {
206: return NValidators::isUrl($control->getValue()) || NValidators::isUrl('http://' . $control->getValue());
207: }
208:
209:
210:
211: public static function validateRegexp(NTextBase $control, $regexp)
212: {
213: return (bool) NStrings::match($control->getValue(), $regexp);
214: }
215:
216:
217: 218: 219: 220: 221:
222: public static function validatePattern(NTextBase $control, $pattern)
223: {
224: return (bool) NStrings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
225: }
226:
227:
228: 229: 230: 231: 232:
233: public static function validateInteger(NTextBase $control)
234: {
235: return NValidators::isNumericInt($control->getValue());
236: }
237:
238:
239: 240: 241: 242: 243:
244: public static function validateFloat(NTextBase $control)
245: {
246: return NValidators::isNumeric(self::filterFloat($control->getValue()));
247: }
248:
249:
250: 251: 252: 253: 254: 255:
256: public static function validateRange(NTextBase $control, $range)
257: {
258: return NValidators::isInRange($control->getValue(), $range);
259: }
260:
261:
262: 263: 264: 265: 266: 267:
268: public static function filterFloat($s)
269: {
270: return str_replace(array(' ', ','), array('', '.'), $s);
271: }
272:
273: }
274: