Skip to content
Permalink
Browse files

Extract a protected method.

If anyone wants to subclass this, having a separate method will be nice.
  • Loading branch information...
markstory committed Jan 11, 2014
1 parent e1a04ac commit 0c46ccf657b81fc7338b9da6eb04625419013147
Showing with 45 additions and 33 deletions.
  1. +45 −33 src/View/Input/Radio.php
@@ -74,7 +74,6 @@ public function render($data) {
$options = (array)$data['options'];
}
$escape = $data['escape'];
if (!empty($data['empty'])) {
$empty = $data['empty'] === true ? 'empty' : $data['empty'];
$options = ['' => $empty] + $options;
@@ -83,38 +82,7 @@ public function render($data) {
$opts = [];
foreach ($options as $val => $text) {
if (is_int($val) && isset($text['text'], $text['value'])) {
$radio = $text;
$text = $radio['text'];
} else {
$radio = ['value' => $val, 'text' => $text];
}
$radio['name'] = $data['name'];
if (empty($radio['id'])) {
$radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']);
}
if (isset($data['value']) && strval($data['value']) === strval($radio['value'])) {
$radio['checked'] = true;
}
if ($this->_isDisabled($radio, $data['disabled'])) {
$radio['disabled'] = true;
}
$label = $this->_renderLabel($radio, $data['label'], $escape);
$input = $this->_templates->format('radio', [
'name' => $radio['name'],
'value' => $escape ? h($radio['value']) : $radio['value'],
'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
]);
$opts[] = $this->_templates->format('radioContainer', [
'input' => $input,
'label' => $label,
]);
$opts[] = $this->_renderInput($val, $text, $data);
}
return implode('', $opts);
}
@@ -137,6 +105,50 @@ protected function _isDisabled($radio, $disabled) {
return (!is_array($disabled) || in_array((string)$radio['value'], $disabled, !$isNumeric));
}
/**
* Renders a single radio input and label.
*
* @param string|int $val The value of the radio input.
* @param string|array $text The label text, or complex radio type.
* @param array $data Additional options for input generation.
* @return string.
*/
protected function _renderInput($val, $text, $data) {
$escape = $data['escape'];
if (is_int($val) && isset($text['text'], $text['value'])) {
$radio = $text;
$text = $radio['text'];
} else {
$radio = ['value' => $val, 'text' => $text];
}
$radio['name'] = $data['name'];
if (empty($radio['id'])) {
$radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']);
}
if (isset($data['value']) && strval($data['value']) === strval($radio['value'])) {
$radio['checked'] = true;
}
if ($this->_isDisabled($radio, $data['disabled'])) {
$radio['disabled'] = true;
}
$label = $this->_renderLabel($radio, $data['label'], $escape);
$input = $this->_templates->format('radio', [
'name' => $radio['name'],
'value' => $escape ? h($radio['value']) : $radio['value'],
'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
]);
return $this->_templates->format('radioContainer', [
'input' => $input,
'label' => $label,
]);
}
/**
* Renders a label element for a given radio button.
*

0 comments on commit 0c46ccf

Please sign in to comment.
You can’t perform that action at this time.