这段代码是否适合策略模式?
public function isValidEmail($email, $organization)
{
switch ($organization) {
case 'USAF':
case 'Army':
case 'USMC':
case 'Navy':
case 'SOCOM':
return (preg_match('/[.]mil$/i', $email) === 1);
break;
case 'Federal Gov.':
return (preg_match('/[.]gov$/i', $email) === 1);
break;
case 'State/Local Gov.':
$regionCollection = Mage::getModel('directory/region')
->getResourceCollection()
->addCountryFilter(array('US'))
->load();
$stateAbbr = array();
// Cycle through state abbreviations for match
foreach ($regionCollection as $region) {
$stateAbbr[] = strtolower($region->getCode());
}
$states = implode('|', $stateAbbr);
return (preg_match("/[\.|@]{1}($states){1}\.us$/i", $email) ===
break;
case 'USCG':
case 'DOD':
case 'Defense Industry':
return true;
break;
default:
return false;
break;
}
// It got past somehow?
return false;
}
我假设您可以有一个简单的接口来定义验证方法,但我对如何处理 switch 语句中返回 true/false w/没有电子邮件逻辑的两个奇怪的球情况有点困惑。
interface ValidInterface
{
public function isValid($email);
}