我需要将一组选项传递给一个函数。现在我正在做一些验证,以验证事情至少是有意义的。如果有更好的方法来做到这一点?事实上,对于最后 4 个选项,我想检查数组是否充满了字符串,而不仅仅是一个数组,但如果我能提供帮助,我不想循环遍历数组。
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
}