From a53e690bb071bc835c6983b5535a2adf684029ec Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 22 Feb 2014 22:16:45 -0500 Subject: [PATCH] Refactor duplicate code into a method. --- src/View/Helper/FormHelper.php | 56 +++++++++++++++------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index e9c8a706b32..3fc0c2f486d 100755 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -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. * @@ -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'), @@ -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'] = [ @@ -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'] = [