elements/number.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
44 class 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  $inputAttributes = $this->htmlInputAttributes();
88  $marker = $this->htmlMarker();
89  $label = $this->htmlLabel();
90  $min = $this->htmlMin();
91  $max = $this->htmlMax();
92  $step = $this->htmlStep();
93  $wrapperAttributes = $this->htmlWrapperAttributes();
95  $list = $this->htmlList();
96  $helpMessage = $this->htmlHelpMessage();
97 
98  return "<p {$wrapperAttributes}>" .
99  "<label>" .
100  "<span class=\"depage-label\">{$label}{$marker}</span>" .
101  "<input name=\"{$this->name}\" type=\"{$this->type}\"{$max}{$min}{$step}{$inputAttributes} value=\"{$value}\">" .
102  $list .
103  "</label>" .
104  $errorMessage .
105  $helpMessage .
106  "</p>\n";
107  }
108 
114  protected function htmlMin()
115  {
116  return ($this->min === null) ? "" : " min=\"" . $this->htmlEscape($this->min) . "\"";
117  }
118 
124  protected function htmlMax()
125  {
126  return ($this->max === null) ? "" : " max=\"" . $this->htmlEscape($this->max) . "\"";
127  }
128 
134  protected function htmlStep()
135  {
136  return ($this->step === null) ? "" : " step=\"" . $this->htmlEscape($this->step) . "\"";
137  }
138 
146  protected function validatorCall()
147  {
148  $parameters = array(
149  'min' => $this->min,
150  'max' => $this->max,
151  );
152 
153  return $this->validator->validate($this->value, $parameters);
154  }
155 
163  protected function typeCastValue()
164  {
165  $ptString = $this->value;
166 
167  $pString = str_replace(" ", "", $ptString);
168 
169  // assume if we have more than one mark it's a thousand mark -> remove them
170  if (substr_count($pString, ",") > 1) {
171  $pString = str_replace(",", "", $pString);
172  }
173  if (substr_count($pString, ".") > 1) {
174  $pString = str_replace(".", "", $pString);
175  }
176 
177  // assume last entry is decimal mark
178  $commaset = strrpos($pString, ',');
179  $pointset = strrpos($pString, '.');
180 
181  // remove all remaining marks but the decimal mark
182  if ($commaset > $pointset) {
183  $pString = str_replace(".", "", $pString);
184  } else if ($commaset < $pointset) {
185  $pString = str_replace(",", "", $pString);
186  }
187 
188  // normalize to dot
189  $pString = str_replace(",", ".", $pString);
190 
191  if ($pString !== "" && preg_match("/^[-+]?\d*\.?\d*$/", $pString)) {
192  $this->value = (float) $pString;
193  } else {
194  $this->value = null;
195  }
196  }
197 
205  public function isEmpty() {
206  return (
207  empty($this->value)
208  && $this->value !== 0
209  && $this->value !== .0
210  );
211  }
212 
213 }
214 
215 /* 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:193
__toString()
Renders element to HTML.
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
validatorCall()
custom validator call
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
HTML text input type.
Definition: text.php:38
htmlMax()
Renders HTML max attribute.
$max
Maximum range HTML attribute.
htmlValue()
Returns HTML-rendered element value.
Definition: input.php:504
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition: input.php:533
$value
Input elements&#39;s value.
Definition: input.php:79
$label
Input element - HTML label.
Definition: input.php:36
htmlInputAttributes()
renders text element specific HTML attributes
Definition: text.php:142
$min
Minimum range HTML attribute.
Steps break up forms into separate consecutive parts.
Definition: step.php:17
HTML number input type.
htmlMin()
Renders HTML min attribute.
validate($value, $parameters=array())
default validator.
Definition: validator.php:76
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
htmlList($options=null)
Renders HTML datalist.
Definition: text.php:112
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
typeCastValue()
Converts value to element specific type.
parent validator class
Definition: validator.php:14
htmlStep()
Renders HTML step attribute.
setDefaults()
collects initial values across subclasses.