私は、クラスプロパティに値があるかどうかを判断するphpクラスメソッドを持っています。それが何らかの値を保持していれば、$ this-> errorクラスのプロパティを検証して繰り返します。ここで私が使用しているクラスメソッドです。複数のif条件を使用するより良い方法はありますか?
public function validate() {
if(!empty($this->name)) {
if(!preg_match('/^[a-zA-z ]{3,50}$/',$this->name)) {
$this->error['name'] = 'Name should be valid letters and should be between 3 and 25 characters';
}
}
if(!empty($this->email)) {
if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) {
$this->error['invalidEmail'] = 'Invalid email address';
}
if(empty($this->userId) && $this->emailCount($this->email)) {
$this->error['emailExist'] = 'Email already exist';
}
}
if(empty($this->userId) && !empty($this->password)) {
if(strlen($this->password) < 5 || strlen($this->password > 40)) {
$this->error['password'] = 'Password length should be between 5 and 40 characters';
}
}
if(!empty($this->userId) && !empty($this->newPassword)) {
if(strlen($this->newPassword) < 5 || strlen($this->newPassword > 40)) {
$this->error['password'] = 'Password length should be between 5 and 40 characters';
}
}
if(!empty($this->pPhone)) {
if(!preg_match('/^[0-9]{5,10}$/',$this->pPhone)) {
$this->error['invalidpPhone'] = 'Invalid primary phone number';
}
}
if(!empty($this->sPhone)) {
if(!preg_match('/^[0-9]{5,10}$/',$this->sPhone)) {
$this->error['invalidsPhone'] = 'Invalid secondary phone number';
}
}
return (empty($this->error)) ? true : false;
}
私は考えて、ここでの条件は非常に良いではない場合の多くを使用していた、私は上記の条件を決定し、より良い方法でコードを書き換えることができ、他の方法はありますか?
これは私の意見ではより良い選択肢であり、読むのも簡単です。どうもありがとうございました。 –
@IbrahimAzharArmarはcakePHP検証システムを見ています。それはほぼ同じ原則です。 – JohnP
@JonhP、例のデモンストレーションを行っている特定の記事があります。それは私には有益かもしれません。 –