Permalink
Browse files

Refactor duplicate code into a method.

  • Loading branch information...
1 parent 41c3f57 commit a53e690bb071bc835c6983b5535a2adf684029ec @markstory markstory committed Feb 23, 2014
Showing with 24 additions and 32 deletions.
  1. +24 −32 src/View/Helper/FormHelper.php
@@ -1844,6 +1844,27 @@ public function multiCheckbox($fieldName, $options, $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.
*
* ### Options:
@@ -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'] = [

0 comments on commit a53e690

Please sign in to comment.