depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Single.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
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, ?string $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();
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>" .
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>" .
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 : */
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
input element base class
Definition Input.php:22
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Input.php:576
$value
Input elements's value.
Definition Input.php:124
$label
Input element - HTML label.
Definition Input.php:37
$class
class for paragraph
Definition Input.php:152
$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
$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
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
HTML-single-choice input type i.e.
Definition Single.php:57
htmlList(?array $options=null, ?string $value=null)
Renders HTML - option list part of select/radio single element.
Definition Single.php:121
$list
Contains list of selectable options.
Definition Single.php:61
$skin
HTML skin type (radio or select).
Definition Single.php:75
typeCastValue()
Converts value to element specific type.
Definition Single.php:205
__construct(string $name, array $parameters, object $form)
single class constructor
Definition Single.php:85
__toString()
Renders element to HTML.
Definition Single.php:168
$listHtml
Contains list of html prerendered options.
Definition Single.php:70
setDefaults()
collects initial values across subclasses
Definition Single.php:102