mercredi 25 janvier 2017

Call method in foreach with clean code

I'm using DomDocument to access a HTML page and convert into a Object but every time I need to search nodes with some different tag. I want a method with the logic name as parameter, but each logic have different number of parameters.

public function foo(){
    $this->findTag('bar', $parameters);
}
public function findTag($logic, $parameters){
    foreach ($this->dom->getElementsByTagName($this->tag) as $node) {
        $this->$logic($node, $parameters);
    }
}
public function logic1($node, $foo, $bar){
    //something with $foo and $bar
}
public function logic2($node, $fooBar){
    //something with $fooBar
}

This is a bad approach? How can I make this foreach a unique function always called when needed some info from the DomObject, using a different tag.

Aucun commentaire:

Enregistrer un commentaire