single.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
12 use depage\htmlform\abstracts;
13 
55 class single extends abstracts\input
56 {
60  protected $list = array();
61 
70  public function __construct($name, $parameters, $form)
71  {
72  parent::__construct($name, $parameters, $form);
73 
74  $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : array();
75  }
76 
86  protected function setDefaults()
87  {
88  parent::setDefaults();
89 
90  // single-choice-elements have values of type string
91  $this->defaults['defaultValue'] = '';
92  $this->defaults['skin'] = 'radio';
93  }
94 
105  protected function htmlList($options = null, $value = null)
106  {
107  if ($value == null) $value = $this->htmlValue();
108  if ($options == null) $options = $this->list;
109 
110  $options = $this->htmlEscape($options);
111  $list = '';
112 
113  if ($this->skin === "select") {
114  foreach ($options as $index => $option) {
115  if (is_array($option)) {
116  $list .= "<optgroup label=\"{$index}\">" . $this->htmlList($option, $value) . "</optgroup>";
117  } else {
118  $selected = ((string) $index === (string) $value) ? ' selected' : '';
119  $list .= "<option value=\"{$index}\"{$selected}>{$option}</option>";
120  }
121  }
122  } else {
123  $inputAttributes = $this->htmlInputAttributes();
124 
125  foreach ($options as $index => $option) {
126  // typecasted for non-associative arrays
127  $selected = ((string) $index === (string) $value) ? " checked=\"yes\"" : '';
128 
129  $list .= "<span>" .
130  "<label>" .
131  "<input type=\"radio\" name=\"{$this->name}\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
132  "<span>{$option}</span>" .
133  "</label>" .
134  "</span>";
135  }
136  }
137 
138  return $list;
139  }
140 
146  public function __toString()
147  {
148  $marker = $this->htmlMarker();
149  $label = $this->htmlLabel();
150  $list = $this->htmlList();
151  $wrapperAttributes = $this->htmlWrapperAttributes();
152  $errorMessage = $this->htmlErrorMessage();
153  $helpMessage = $this->htmlHelpMessage();
154 
155  if ($this->skin === "select") {
156  // render HTML select
157  $inputAttributes = $this->htmlInputAttributes();
158 
159  return "<p {$wrapperAttributes}>" .
160  "<label>" .
161  "<span class=\"depage-label\">{$label}{$marker}</span>" .
162  "<select name=\"{$this->name}\"{$inputAttributes}>{$list}</select>" .
163  "</label>" .
164  $errorMessage .
165  $helpMessage .
166  "</p>\n";
167  } else {
168  // render HTML radio button list
169  return "<p {$wrapperAttributes}>" .
170  "<span class=\"depage-label\">{$label}{$marker}</span>" .
171  "<span>{$list}</span>" .
172  $errorMessage .
173  $helpMessage .
174  "</p>\n";
175  }
176  }
177 
183  protected function typeCastValue()
184  {
185  $this->value = (string) $this->value;
186  }
187 }
188 
189 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlList($options=null, $value=null)
Renders HTML - option list part of select/radio single element.
Definition: single.php:105
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
input element base class
Definition: input.php:20
setDefaults()
collects initial values across subclasses
Definition: single.php:86
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition: input.php:455
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
$list
Contains list of selectable options.
Definition: single.php:60
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
__toString()
Renders element to HTML.
Definition: single.php:146
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
typeCastValue()
Converts value to element specific type.
Definition: single.php:183
$label
Input element - HTML label.
Definition: input.php:36
__construct($name, $parameters, $form)
single class constructor
Definition: single.php:70
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
HTML-single-choice input type i.e. radio and select.
Definition: single.php:55
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47