Skip to content

Commit

Permalink
Refactor duplicate code into a method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 25, 2014
1 parent 41c3f57 commit a53e690
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions src/View/Helper/FormHelper.php
Expand Up @@ -1843,6 +1843,27 @@ public function multiCheckbox($fieldName, $options, $attributes = []) {
return $hidden . $this->widget('multicheckbox', $attributes);
}

/**
* Helper method for the various single datetime component methods.
*
* @param array $options The options array.
* @param string $keep The option to not disable.
* @return array
*/
protected function _singleDatetime($options, $keep) {
$off = array_diff($this->_datetimeParts, [$keep]);
$off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;

if (isset($options['value'])) {
$options['val'] = $options['value'];
}
return $options;
}

/**
* Returns a SELECT element for days.
*
Expand All @@ -1858,18 +1879,8 @@ public function multiCheckbox($fieldName, $options, $attributes = []) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::day
*/
public function day($fieldName = null, $options = []) {
$off = array_diff($this->_datetimeParts, ['day']);
$off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;
$options = $this->_singleDatetime($options, 'day');

if (isset($options['value'])) {
$options['val'] = $options['value'];
}

// If value is an integer reformat it.
if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 31) {
$options['val'] = [
'year' => date('Y'),
Expand Down Expand Up @@ -1899,18 +1910,8 @@ public function day($fieldName = null, $options = []) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::year
*/
public function year($fieldName, $options = []) {
$off = array_diff($this->_datetimeParts, ['year']);
$off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;
$options = $this->_singleDatetime($options, 'year');

if (isset($options['value'])) {
$options['val'] = $options['value'];
}

// If value is an integer reformat it.
$len = isset($options['val']) ? strlen($options['val']) : 0;
if (isset($options['val']) && $len > 0 && $len < 5) {
$options['val'] = [
Expand Down Expand Up @@ -1940,16 +1941,7 @@ public function year($fieldName, $options = []) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
*/
public function month($fieldName, $options = array()) {
$off = array_diff($this->_datetimeParts, ['month']);
$off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;

if (isset($options['value'])) {
$options['val'] = $options['value'];
}
$options = $this->_singleDatetime($options, 'month');

if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 12) {
$options['val'] = [
Expand Down

0 comments on commit a53e690

Please sign in to comment.