i have a php class method which determines whether if a class property have got any value. if it holds any value then it will validate and iterate $this->error class property. here is the class method i am using.
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;
}
i have used lots of if conditions here which i think is not very good, is there any other way i could determine the above condition and rewrite the code in a more better way?