depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Elements/Number.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
45class Number extends Text
46{
50 protected $min;
54 protected $max;
58 protected $step;
59
69 protected function setDefaults(): void
70 {
71 parent::setDefaults();
72
73 $this->defaults['defaultValue'] = 0;
74 $this->defaults['min'] = null;
75 $this->defaults['max'] = null;
76 $this->defaults['step'] = null;
77 $this->defaults['errorMessage'] = _('Please enter a valid number');
78 }
79
85 public function __toString(): string
86 {
87 $value = $this->htmlValue();
88 $type = strtolower($this->type);
89 $inputAttributes = $this->htmlInputAttributes();
90 $marker = $this->htmlMarker();
91 $label = $this->htmlLabel();
92 $min = $this->htmlMin();
93 $max = $this->htmlMax();
94 $step = $this->htmlStep();
95 $wrapperAttributes = $this->htmlWrapperAttributes();
97 $list = $this->htmlList();
99
100 return "<p {$wrapperAttributes}>" .
101 "<label>" .
102 "<span class=\"depage-label\">{$label}{$marker}</span>" .
103 "<input name=\"{$this->name}\" type=\"{$type}\"{$max}{$min}{$step}{$inputAttributes} value=\"{$value}\">" .
104 $list .
105 "</label>" .
108 "</p>\n";
109 }
110
116 protected function htmlMin(): string
117 {
118 return ($this->min === null) ? "" : " min=\"" . $this->htmlEscape($this->min) . "\"";
119 }
120
126 protected function htmlMax(): string
127 {
128 return ($this->max === null) ? "" : " max=\"" . $this->htmlEscape($this->max) . "\"";
129 }
130
136 protected function htmlStep(): string
137 {
138 return ($this->step === null) ? "" : " step=\"" . $this->htmlEscape($this->step) . "\"";
139 }
140
148 protected function validatorCall(): bool
149 {
150 $parameters = [
151 'min' => $this->min,
152 'max' => $this->max,
153 ];
154
155 return $this->validator->validate($this->value, $parameters);
156 }
157
165 protected function typeCastValue(): void
166 {
167 $ptString = $this->value;
168
169 $pString = str_replace(" ", "", $ptString);
170
171 // assume if we have more than one mark it's a thousand mark -> remove them
172 if (substr_count($pString, ",") > 1) {
173 $pString = str_replace(",", "", $pString);
174 }
175 if (substr_count($pString, ".") > 1) {
176 $pString = str_replace(".", "", $pString);
177 }
178
179 // assume last entry is decimal mark
180 $commaset = strrpos($pString, ',');
181 $pointset = strrpos($pString, '.');
182
183 // remove all remaining marks but the decimal mark
184 if ($commaset > $pointset) {
185 $pString = str_replace(".", "", $pString);
186 } elseif ($commaset < $pointset) {
187 $pString = str_replace(",", "", $pString);
188 }
189
190 // normalize to dot
191 $pString = str_replace(",", ".", $pString);
192
193 if ($pString !== "" && preg_match("/^[-+]?\d*\.?\d*$/", $pString)) {
194 $this->value = (float) $pString;
195 } else {
196 $this->value = null;
197 }
198 }
199
208 public function getStringValue(): string
209 {
210 $decimals = 0;
211
212 if ($this->step !== null && ($pos = strpos($this->step, ".")) !== false) {
213 $decimals = strlen(substr($this->step, $pos + 1));
214 }
215
216 return number_format($this->getValue(), $decimals, '.', '');
217 }
218
226 public function isEmpty(): bool
227 {
228 return (
229 empty($this->value)
230 && $this->value !== 0
231 && $this->value !== .0
232 );
233 }
234}
235
236/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
$value
Input elements's value.
Definition Input.php:124
$label
Input element - HTML label.
Definition Input.php:37
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
$type
Input element type - HTML input type attribute.
Definition Input.php:26
$helpMessage
Extra help message.
Definition Input.php:230
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:656
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
getValue()
Returns the current input elements' value.
Definition Input.php:402
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
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:40
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:160
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:130