I am currently writing a text file parser for several formats. While the text file is parsed, there are different kinds of operations to do. I am trying to do something clean using OOP. Here is where I am stuck :
abstract class Parser
{
abstract protected function DoSomeStuff($data);
public function Parse($src)
{
if ( $this->GetFormat($src) == 1 )
{
$data =$this->GetSomeDataFromFormat1($src);
DoSomeStuff($data);
}
if ( $this->GetFormat($src) == 2 )
{
$data = $this->GetSomeDataFromFormat2($src);
DoSomeStuff($data);
}
}
}
class DoSomething extends Parser
{
protected function DoSomeStuff($data)
{
// Doing some stuff with data
}
}
class DoSomethingElse extends Parser
{
protected function DoSomeStuff($data)
{
// Doing some other stuff with data
}
}
$ds = new DoSomething();
$ds->Parse(...);
$dse = new DoSomethingElse();
$dse->Parse(...);
As you can see : all the code for all files formats is in class Parser. What can I do to make this cleaner ?
Thanks Antoine