Skip to content

Commit

Permalink
Refactoring option extraction into a separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 27, 2010
1 parent 3192e13 commit 1f25bb3
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions cake/libs/view/helpers/form.php
Expand Up @@ -676,7 +676,6 @@ function input($fieldName, $options = array()) {
if (!isset($options['type'])) {
$magicType = true;
$options['type'] = 'text';
$fieldDef = array();
if (isset($options['options'])) {
$options['type'] = 'select';
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
Expand Down Expand Up @@ -743,13 +742,10 @@ function input($fieldName, $options = array()) {
}

$out = '';
$div = true;
$divOptions = array();

if (array_key_exists('div', $options)) {
$div = $options['div'];
unset($options['div']);
}
$div = $this->_extractOption('div', $options, true);
unset($options['div']);

if (!empty($div)) {
$divOptions['class'] = 'input';
Expand All @@ -762,7 +758,7 @@ function input($fieldName, $options = array()) {
if (
isset($this->fieldset[$modelKey]) &&
in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
) {
) {
$divOptions = $this->addClass($divOptions, 'required');
}
if (!isset($divOptions['tag'])) {
Expand All @@ -779,11 +775,7 @@ function input($fieldName, $options = array()) {
if ($options['type'] === 'radio') {
$label = false;
if (isset($options['options'])) {
if (is_array($options['options'])) {
$radioOptions = $options['options'];
} else {
$radioOptions = array($options['options']);
}
$radioOptions = (array)$options['options'];
unset($options['options']);
}
}
Expand Down Expand Up @@ -817,36 +809,24 @@ function input($fieldName, $options = array()) {
$out = $this->label($fieldName, $labelText, $labelAttributes);
}

$error = null;
if (isset($options['error'])) {
$error = $options['error'];
unset($options['error']);
}
$error = $this->_extractOption('error', $options, null);
unset($options['error']);

$selected = $this->_extractOption('selected', $options, null);
unset($options['selected']);

$selected = null;
if (array_key_exists('selected', $options)) {
$selected = $options['selected'];
unset($options['selected']);
}
if (isset($options['rows']) || isset($options['cols'])) {
$options['type'] = 'textarea';
}

$timeFormat = 12;
if (isset($options['timeFormat'])) {
$timeFormat = $options['timeFormat'];
unset($options['timeFormat']);
}

$dateFormat = 'MDY';
if (isset($options['dateFormat'])) {
$dateFormat = $options['dateFormat'];
unset($options['dateFormat']);
}

if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
$options += array('empty' => false);
}
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
$dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
$timeFormat = $this->_extractOption('timeFormat', $options, 12);
unset($options['dateFormat'], $options['timeFormat']);
}

$type = $options['type'];
$before = $options['before'];
Expand Down Expand Up @@ -921,6 +901,22 @@ function input($fieldName, $options = array()) {
return $out;
}

/**
* Extracts a single option from an options array.
*
* @param string $name The name of the option to pull out.
* @param array $options The array of options you want to extract.
* @param mixed $default The default option value
* @return the contents of the option or default
* @access protected
*/
function _extractOption($name, $options, $default = null) {
if (array_key_exists($name, $options)) {
return $options[$name];
}
return $default;
}

/**
* Creates a checkbox input widget.
*
Expand Down

0 comments on commit 1f25bb3

Please sign in to comment.