depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Elements/Number.php
Go to the documentation of this file.
1
<?php
2
10
11
namespace
Depage\HtmlForm\Elements;
12
45
class
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
();
96
$errorMessage
= $this->
htmlErrorMessage
();
97
$list
= $this->
htmlList
();
98
$helpMessage
= $this->
htmlHelpMessage
();
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>"
.
106
$errorMessage
.
107
$helpMessage
.
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 : */
Depage\HtmlForm\Abstracts\Element\htmlEscape
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition
Element.php:263
Depage\HtmlForm\Abstracts\Input\$value
$value
Input elements's value.
Definition
Input.php:124
Depage\HtmlForm\Abstracts\Input\$label
$label
Input element - HTML label.
Definition
Input.php:37
Depage\HtmlForm\Abstracts\Input\$errorMessage
$errorMessage
Message that gets displayed in case of invalid input.
Definition
Input.php:218
Depage\HtmlForm\Abstracts\Input\htmlHelpMessage
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition
Input.php:685
Depage\HtmlForm\Abstracts\Input\htmlWrapperAttributes
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition
Input.php:636
Depage\HtmlForm\Abstracts\Input\htmlMarker
htmlMarker()
Returns elements' required-indicator.
Definition
Input.php:566
Depage\HtmlForm\Abstracts\Input\$type
$type
Input element type - HTML input type attribute.
Definition
Input.php:26
Depage\HtmlForm\Abstracts\Input\$helpMessage
$helpMessage
Extra help message.
Definition
Input.php:230
Depage\HtmlForm\Abstracts\Input\htmlValue
htmlValue()
Returns HTML-rendered element value.
Definition
Input.php:656
Depage\HtmlForm\Abstracts\Input\$marker
$marker
Input element - HTML marker text that marks required fields.
Definition
Input.php:92
Depage\HtmlForm\Abstracts\Input\getValue
getValue()
Returns the current input elements' value.
Definition
Input.php:402
Depage\HtmlForm\Abstracts\Input\htmlErrorMessage
htmlErrorMessage()
Returns HTML-rendered error message.
Definition
Input.php:666
Depage\HtmlForm\Elements\Number
HTML number input type.
Definition
Elements/Number.php:46
Depage\HtmlForm\Elements\Number\htmlMax
htmlMax()
Renders HTML max attribute.
Definition
Elements/Number.php:126
Depage\HtmlForm\Elements\Number\getStringValue
getStringValue()
getStringValue
Definition
Elements/Number.php:208
Depage\HtmlForm\Elements\Number\typeCastValue
typeCastValue()
Converts value to element specific type.
Definition
Elements/Number.php:165
Depage\HtmlForm\Elements\Number\htmlMin
htmlMin()
Renders HTML min attribute.
Definition
Elements/Number.php:116
Depage\HtmlForm\Elements\Number\$min
$min
Minimum range HTML attribute.
Definition
Elements/Number.php:50
Depage\HtmlForm\Elements\Number\__toString
__toString()
Renders element to HTML.
Definition
Elements/Number.php:85
Depage\HtmlForm\Elements\Number\$step
$step
Step HTML attribute.
Definition
Elements/Number.php:58
Depage\HtmlForm\Elements\Number\validatorCall
validatorCall()
custom validator call
Definition
Elements/Number.php:148
Depage\HtmlForm\Elements\Number\$max
$max
Maximum range HTML attribute.
Definition
Elements/Number.php:54
Depage\HtmlForm\Elements\Number\setDefaults
setDefaults()
collects initial values across subclasses.
Definition
Elements/Number.php:69
Depage\HtmlForm\Elements\Number\htmlStep
htmlStep()
Renders HTML step attribute.
Definition
Elements/Number.php:136
Depage\HtmlForm\Elements\Number\isEmpty
isEmpty()
custom empty check
Definition
Elements/Number.php:226
Depage\HtmlForm\Elements\Text
HTML text input type.
Definition
Text.php:40
Depage\HtmlForm\Elements\Text\htmlInputAttributes
htmlInputAttributes()
renders text element specific HTML attributes
Definition
Text.php:160
Depage\HtmlForm\Elements\Text\$list
$list
HTML list.
Definition
Text.php:59
Depage\HtmlForm\Elements\Text\htmlList
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
Definition
Text.php:130
Elements
Number.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net