depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Single.php
Go to the documentation of this file.
1
<?php
2
10
11
namespace
Depage\HtmlForm\Elements;
12
13
use Depage\HtmlForm\Abstracts;
14
56
class
Single
extends
Abstracts\Input
57
{
61
protected
$list
= [];
62
70
protected
$listHtml
= [];
71
75
protected
$skin
=
'radio'
;
76
85
public
function
__construct
(
string
$name
, array $parameters,
object
$form)
86
{
87
parent::__construct(
$name
, $parameters, $form);
88
89
$this->list = (isset($parameters[
'list'
]) && is_array($parameters[
'list'
])) ? $parameters[
'list'
] : [];
90
$this->listHtml = (isset($parameters[
'listHtml'
]) && is_array($parameters[
'listHtml'
])) ? $parameters[
'listHtml'
] : [];
91
}
92
102
protected
function
setDefaults
(): void
103
{
104
parent::setDefaults();
105
106
// single-choice-elements have values of type string
107
$this->defaults[
'defaultValue'
] =
''
;
108
$this->defaults[
'skin'
] =
'radio'
;
109
}
110
121
protected
function
htmlList
(?array $options =
null
, array|
string
|
null
$value
=
null
): string
122
{
123
if
(
$value
==
null
) {
124
$value
= $this->
htmlValue
();
125
}
126
if
($options ==
null
) {
127
$options =
$this->list
;
128
}
129
130
$options = $this->
htmlEscape
($options);
131
$list
=
''
;
132
133
if
($this->skin ===
"select"
) {
134
foreach
($options as $index => $option) {
135
if
(is_array($option)) {
136
$list
.=
"<optgroup label=\"{$index}\">"
. $this->
htmlList
($option,
$value
) .
"</optgroup>"
;
137
}
else
{
138
$selected = ((string) $index === (string)
$value
) ?
' selected'
:
''
;
139
$list
.=
"<option value=\"{$index}\"{$selected}>{$option}</option>"
;
140
}
141
}
142
}
else
{
143
$inputAttributes = $this->
htmlInputAttributes
();
144
145
foreach
($options as $index => $option) {
146
// typecasted for non-associative arrays
147
$selected = ((string) $index === (string)
$value
) ?
" checked=\"yes\""
:
''
;
148
$class
=
"input-single-option-"
. str_replace(
" "
,
"-"
, $index);
149
$optionHtml = $this->listHtml[$index] ??
"<span>{$option}</span>"
;
150
151
$list
.=
"<span>"
152
.
"<label class=\"{$class}\" title=\"{$option}\">"
153
.
"<input type=\"radio\" name=\"{$this->name}\"{$inputAttributes} value=\"{$index}\"{$selected}>"
154
. $optionHtml
155
.
"</label>"
156
.
"</span>"
;
157
}
158
}
159
160
return
$list
;
161
}
162
168
public
function
__toString
(): string
169
{
170
$marker
= $this->
htmlMarker
();
171
$label
= $this->htmlLabel();
172
$list
= $this->
htmlList
();
173
$wrapperAttributes = $this->
htmlWrapperAttributes
();
174
$errorMessage
= $this->
htmlErrorMessage
();
175
$helpMessage
= $this->
htmlHelpMessage
();
176
177
if
($this->skin ===
"select"
) {
178
// render HTML select
179
$inputAttributes = $this->
htmlInputAttributes
();
180
181
return
"<p {$wrapperAttributes}>"
182
.
"<label>"
183
.
"<span class=\"depage-label\">{$label}{$marker}</span>"
184
.
"<select name=\"{$this->name}\"{$inputAttributes}>{$list}</select>"
185
.
"</label>"
186
.
$errorMessage
187
.
$helpMessage
188
.
"</p>\n"
;
189
}
else
{
190
// render HTML radio button list
191
return
"<p {$wrapperAttributes}>"
192
.
"<span class=\"depage-label\">{$label}{$marker}</span>"
193
.
"<span>{$list}</span>"
194
.
$errorMessage
195
.
$helpMessage
196
.
"</p>\n"
;
197
}
198
}
199
205
protected
function
typeCastValue
(): void
206
{
207
// check if value is in list
208
$inList =
false
;
209
210
if
(in_array($this->value, array_keys($this->list))) {
211
$inList =
true
;
212
}
213
if
(!$inList) {
214
foreach
($this->list as $sub) {
215
if
(is_array($sub) && in_array($this->value, array_keys($sub))) {
216
$inList =
true
;
217
break
;
218
}
219
}
220
}
221
if
(!$inList) {
222
$this->value =
""
;
223
}
224
$this->value = (string) $this->value;
225
}
226
}
227
228
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Depage\HtmlForm\Abstracts\Element\$name
$name
Element name.
Definition
Element.php:36
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
input element base class
Definition
Input.php:22
Depage\HtmlForm\Abstracts\Input\htmlInputAttributes
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition
Input.php:576
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\$class
$class
class for paragraph
Definition
Input.php:152
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\$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\htmlErrorMessage
htmlErrorMessage()
Returns HTML-rendered error message.
Definition
Input.php:666
Depage\HtmlForm\Elements\Single
HTML-single-choice input type i.e.
Definition
Single.php:57
Depage\HtmlForm\Elements\Single\$list
$list
Contains list of selectable options.
Definition
Single.php:61
Depage\HtmlForm\Elements\Single\$skin
$skin
HTML skin type (radio or select).
Definition
Single.php:75
Depage\HtmlForm\Elements\Single\typeCastValue
typeCastValue()
Converts value to element specific type.
Definition
Single.php:205
Depage\HtmlForm\Elements\Single\__construct
__construct(string $name, array $parameters, object $form)
single class constructor
Definition
Single.php:85
Depage\HtmlForm\Elements\Single\__toString
__toString()
Renders element to HTML.
Definition
Single.php:168
Depage\HtmlForm\Elements\Single\$listHtml
$listHtml
Contains list of html prerendered options.
Definition
Single.php:70
Depage\HtmlForm\Elements\Single\htmlList
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML - option list part of select/radio single element.
Definition
Single.php:121
Depage\HtmlForm\Elements\Single\setDefaults
setDefaults()
collects initial values across subclasses
Definition
Single.php:102
Elements
Single.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net