2013-07-02 40 views
0

我需要將一組選項傳遞給一個函數。現在我正在進行一些驗證,以驗證事情至少有意義。如果有更好的方法去做這件事?實際上,對於最後4個選項,我想檢查數組是否充滿了字符串,而不僅僅是一個數組,但如果我可以幫助它的話,它不想循環訪問數組。有沒有更好的方法來處理在PHP中傳遞給函數的選項?

public function edit($id, $title, $options, $raw) { 
    // Update options 
    if (is_array($options)) { 
     if (array_key_exists('showTitleDate', $options)) { 
      if ($options['showTitleDate'] === true) { $this->showTitleDate = true; } 
     } 
     if (array_key_exists('showDetail', $options)) { 
      if ($options['showDetail'] === true) { $this->showDetail = true; } 
     } 
     if (array_key_exists('showDetailDate', $options)) { 
      if ($options['showDetailDate'] === true) { $this->showDetailDate = true; } 
     } 
     if (array_key_exists('showSubdetail', $options)) { 
      if ($options['showSubdetail'] === true) { $this->showSubdetail = true; } 
     } 
     if (array_key_exists('showSubdetailDate', $options)) { 
      if ($options['showSubdetailDate'] === true) { $this->showSubdetailDate = true; } 
     } 
     if (array_key_exists('showAdditional', $options)) { 
      if ($options['showAdditional'] === true) { $this->showAdditional = true; } 
     } 
     if (array_key_exists('showTitleSelect', $options)) { 
      if ($options['showTitleSelect'] === true) { $this->showTitleSelect = true; } 
     } 
     if (array_key_exists('showTopSelect', $options)) { 
      if ($options['showTopSelect'] === true) { $this->showTopSelect = true; } 
     } 
     if (array_key_exists('showMiddleSelect', $options)) { 
      if ($options['showMiddleSelect'] === true) { $this->showMiddleSelect = true; } 
     } 
     if (array_key_exists('showBottomSelect', $options)) { 
      if ($options['showBottomSelect'] === true) { $this->showBottomSelect = true; } 
     } 
     if (array_key_exists('placeholderTitle', $options)) { 
      if (is_string($options['placeholderTitle'])) { $this->placeholderTitle = $options['placeholderTitle']; } 
     } 
     if (array_key_exists('placeholderTitleDate', $options)) { 
      if (is_string($options['placeholderTitleDate'])) { $this->placeholderTitleDate = $options['placeholderTitleDate']; } 
     } 
     if (array_key_exists('placeholderDetail', $options)) { 
      if (is_string($options['placeholderDetail'])) { $this->placeholderDetail = $options['placeholderDetail']; } 
     } 
     if (array_key_exists('placeholderDetailDate', $options)) { 
      if (is_string($options['placeholderDetailDate'])) { $this->placeholderDetailDate = $options['placeholderDetailDate']; } 
     } 
     if (array_key_exists('placeholderSubdetail', $options)) { 
      if (is_string($options['placeholderSubdetail'])) { $this->placeholderSubdetail = $options['placeholderSubdetail']; } 
     } 
     if (array_key_exists('placeholderSubdetailDate', $options)) { 
      if (is_string($options['placeholderSubdetailDate'])) { $this->placeholderSubdetailDate = $options['placeholderSubdetailDate']; } 
     } 
     if (array_key_exists('placeholderAdditional', $options)) { 
      if (is_string($options['placeholderAdditional'])) { $this->placeholderAdditional = $options['placeholderAdditional']; } 
     } 
     if (array_key_exists('optionsTitleSelect', $options)) { 
      if (is_array ($options['optionsTitleSelect'])) { $this->optionsTitleSelect = $options['optionsTitleSelect']; } 
     } 
     if (array_key_exists('optionsTopSelect', $options)) { 
      if (is_array ($options['optionsTopSelect'])) { $this->optionsTopSelect = $options['optionsTopSelect']; } 
     } 
     if (array_key_exists('optionsMiddleSelect', $options)) { 
      if (is_array ($options['optionsMiddleSelect'])) { $this->optionsMiddleSelect = $options['optionsMiddleSelect']; } 
     } 
     if (array_key_exists('optionsBottomSelect', $options)) { 
      if (is_array ($options['optionsBottomSelect'])) { $this->optionsBottomSelect = $options['optionsBottomSelect']; } 
     } 
    } 

    // -- logic code here 
} 
+0

我覺得這個答案可以幫助你http://stackoverflow.com/a/1476937/736967 – Ronnie

回答

2

使所有可用選項映射到他們的驗證功能。 我將標準函數作爲自身函數的示例。

public function edit($id, $title, $options, $raw) { 
    $isString = function($value) { 
     return is_string($value); 
    }; 
    $isArray = function($value) { 
     return is_array($value); 
    }; 
    $avaiableOptions = array(
     'placeholderTitleDate' => $isString, 
     'optionsBottomSelect' => $isArray, 
     ... 
    ); 
    foreach ($options as $key => $val) { 
     if (!array_key_exists($key, $availableOptions) { 
      echo ... no such option - typo? available options are: ... 
     } else { 
      $validationFunc = $availableOptions[$key]; 
      if (!$validationFunc($options[$key])) { 
       echo ... invalid value for option $key 
      } 
     } 
    } 
0

我的PHP是生鏽,但我認爲"indirection"可能有助於在這裏:​​

$all_key = array('showTitleDate', 'showDetail' /*, ... */); 
foreach ($all_key as $k) 
    if (array_key_exists($k, $options) && $options[$k] === true) { 
     $this->$k = true; 
    } 
} 
相關問題