I'm programming object-oriented, I've been studying design patterns for some time, but I can not develop without using a massive rainfall of ifs.
How to do design patterns to avoid ifs
? I have read a lot of articles on the subject but with PHP I can not evolve in that sense.
For example, I'm doing here a CPF validator, standard IRS. Between ifs
and elses
are more 15 streams. It gets that gigantic and disorganized code, even breaking some commands into sub-functions in the class.
public function is_cpf_strict() {
// Remoção dos caracteres especiais
if (isset($this->var['value'])){
//flag de erro
$erro8 = 0;
$array8 = array();
$cpf8 = trim($this->var['value']);
$array8 = explode(".",$cpf8);
$cpf8 = implode("", $array8);
$array8 = explode("-",$cpf8);
$cpf8 = implode("", $array8);
if (strlen($cpf8)!=11){
$erro8 = 1;
}
// Decompor o CPF no padrão receita
else{
$x=10;
$y=0;
$tot=0;
$s=0;
while($x>=2){
$s = $cpf8[$y] * $x;
$tot = $tot + $s;
$x--;
$y++;
}
//Obter o primeiro dígito
$h = $tot%11;
if ($h>=2){
$hi1 = 11 - $h;
}
else {
$hi1 = 0;
}
// Valida o primeiro dígito e obtém o segundo
if ($hi1 == $cpf8[9]){
$z = 11;
$a = 0;
$tot2 = 0;
$s2 = 0;
while($z>=2){
$s2 = $cpf8[$a]*$z;
$tot2 = $tot2 + $s2;
$z--;
$a++;
}
$h2 = $tot2%11;
if ($h2>=2){
$hi2= 11 - $h2;
}
else{
$hi2 = 0;
}
//Valida segundo dígito
if ($hi2 == $cpf8[10]){
//echo "segundo digito válido. <br>";
}
else{
$erro8 = 1;
}
}
else {
$erro8 = 1;
}
}//FIM VALIDAÇÂO CPF
if ($erro8 == 1){
array_push($this->err["{$this->var['name']}"], "{$this->var['name']} de inválido");
}
}
return $this;
}