Elements/Number.php
Go to the documentation of this file.
1<?php
11
44class Number extends Text
45{
49 protected $min;
53 protected $max;
57 protected $step;
58
68 protected function setDefaults()
69 {
70 parent::setDefaults();
71
72 $this->defaults['defaultValue'] = 0;
73 $this->defaults['min'] = null;
74 $this->defaults['max'] = null;
75 $this->defaults['step'] = null;
76 $this->defaults['errorMessage'] = _('Please enter a valid number');
77 }
78
84 public function __toString()
85 {
86 $value = $this->htmlValue();
87 $type = strtolower($this->type);
88 $inputAttributes = $this->htmlInputAttributes();
89 $marker = $this->htmlMarker();
90 $label = $this->htmlLabel();
91 $min = $this->htmlMin();
92 $max = $this->htmlMax();
93 $step = $this->htmlStep();
94 $wrapperAttributes = $this->htmlWrapperAttributes();
96 $list = $this->htmlList();
98
99 return "<p {$wrapperAttributes}>" .
100 "<label>" .
101 "<span class=\"depage-label\">{$label}{$marker}</span>" .
102 "<input name=\"{$this->name}\" type=\"{$type}\"{$max}{$min}{$step}{$inputAttributes} value=\"{$value}\">" .
103 $list .
104 "</label>" .
107 "</p>\n";
108 }
109
115 protected function htmlMin()
116 {
117 return ($this->min === null) ? "" : " min=\"" . $this->htmlEscape($this->min) . "\"";
118 }
119
125 protected function htmlMax()
126 {
127 return ($this->max === null) ? "" : " max=\"" . $this->htmlEscape($this->max) . "\"";
128 }
129
135 protected function htmlStep()
136 {
137 return ($this->step === null) ? "" : " step=\"" . $this->htmlEscape($this->step) . "\"";
138 }
139
147 protected function validatorCall()
148 {
149 $parameters = array(
150 'min' => $this->min,
151 'max' => $this->max,
152 );
153
154 return $this->validator->validate($this->value, $parameters);
155 }
156
164 protected function typeCastValue()
165 {
166 $ptString = $this->value;
167
168 $pString = str_replace(" ", "", $ptString);
169
170 // assume if we have more than one mark it's a thousand mark -> remove them
171 if (substr_count($pString, ",") > 1) {
172 $pString = str_replace(",", "", $pString);
173 }
174 if (substr_count($pString, ".") > 1) {
175 $pString = str_replace(".", "", $pString);
176 }
177
178 // assume last entry is decimal mark
179 $commaset = strrpos($pString, ',');
180 $pointset = strrpos($pString, '.');
181
182 // remove all remaining marks but the decimal mark
183 if ($commaset > $pointset) {
184 $pString = str_replace(".", "", $pString);
185 } else if ($commaset < $pointset) {
186 $pString = str_replace(",", "", $pString);
187 }
188
189 // normalize to dot
190 $pString = str_replace(",", ".", $pString);
191
192 if ($pString !== "" && preg_match("/^[-+]?\d*\.?\d*$/", $pString)) {
193 $this->value = (float) $pString;
194 } else {
195 $this->value = null;
196 }
197 }
198
207 public function getStringValue()
208 {
209 $decimals = 0;
210
211 if ($this->step !== null && ($pos = strpos($this->step, ".")) !== false) {
212 $decimals = strlen(substr($this->step, $pos + 1));
213 }
214
215 return number_format($this->getValue(), $decimals, '.', '');
216 }
217
225 public function isEmpty() {
226 return (
227 empty($this->value)
228 && $this->value !== 0
229 && $this->value !== .0
230 );
231 }
232
233}
234
235/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
$value
Input elements's value.
Definition Input.php:123
$label
Input element - HTML label.
Definition Input.php:36
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
$type
Input element type - HTML input type attribute.
Definition Input.php:25
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
getValue()
Returns the current input elements' value.
Definition Input.php:402
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
htmlMax()
Renders HTML max attribute.
typeCastValue()
Converts value to element specific type.
htmlMin()
Renders HTML min attribute.
$min
Minimum range HTML attribute.
__toString()
Renders element to HTML.
validatorCall()
custom validator call
$max
Maximum range HTML attribute.
setDefaults()
collects initial values across subclasses.
htmlStep()
Renders HTML step attribute.
HTML text input type.
Definition Text.php:39
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:159
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:129
Classes for HTML input-elements.
Definition Address.php:9